2015-05-07 57 views
0

即時通訊嘗試運行一個簡單的Twitter搜索tweepy的Python腳本。我希望能夠通過Supercollider的OSC消息啓動/關閉搜索功能。最終目標是讓OSC觸發器停止並重新啓動搜索功能。重置Tweepy跟蹤與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

它看起來像tweepy流僅打印到標準輸出。如果是這種情況,您還需要將該流/標準輸出發送到端口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)) 

然後你可以設置其他OSC服務器或基本的tweepy流偵聽器內得到控制該類,在另一個端口上設置偵聽器。

聽起來像一個有趣的藝術項目。

+0

謝謝jm_____它絕對需要更多的工作!會讓你更新:) –