我试图运行一个python脚本,用tweepy进行简单的twitter搜索。我希望能够通过Supercollider的OSC消息触发/关闭搜索功能。最终目标是让OSC触发器停止并重新启动搜索功能。
目前,似乎脚本无法同时收听Twitter流和OSC消息。我一直试图将tweepy基本streamlistener代码和pyOSC basic_receive代码结合起来。
这是我想出的:
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import threading
import OSC
import time, threading
auth/api
#from basic_receive.py
receive_address = '127.0.0.1', 9000
s = OSC.OSCServer(receive_address)
def printing_handler(addr,tags,stuff,source):
global word
word = stuff[0]
print word
return
s.addMsgHandler("/print", printing_handler)
st = threading.Thread( target = s.serve_forever )
st.start()
try:
while 1:
time.sleep(1)
except KeyboardInterrupt :
print "\nClosing OSCServer."
s.close()
print "Waiting for Server-thread to finish"
st.join() ##!!!
print "Done"
#from Tweepy
class StdOutListener(StreamListener):
global word
def __init__(self, api=None):
super(StdOutListener, self).__init__()
self.num_tweets = 0
def on_status(self, status):
text = status.text
print text
self.num_tweets = self.num_tweets + 1
if self.num_tweets > 1000:
return False
elif word == 'haha':
return False
else:
return True
def on_error(self, status):
print(status)
return True
def track():
stream = Stream(auth, listener)
stream.filter(track = ['food', 'love'])
return
if __name__ == '__main__':
listener = StdOutListener()
track()
printing_handler()
答案 0 :(得分:0)
看起来tweepy流只打印到stdout。如果是这种情况,您还需要将该流/标准输出发送到端口9000.所以您也想尝试集成basic_send。
您可以设置套接字连接:Python. Redirect stdout to a socket
或者你可以设置一个客户端:
def on_status(self, status):
text = status.text
client = OSC.OSCClient()
msg = OSC.OSCMessage()
msg.setAddress("/the/osc/address")
msg.append(text)
client.sendto(msg, ('127.0.0.1', 9000))
然后你可以在tweepy流监听器中设置另一个OSC服务器或基本接收来控制该类,在另一个端口上设置监听器。
听起来像一个有趣的艺术项目。