0

im trying to run a python script that does a simple twitter search with tweepy. i want to be able to trigger the search function on/off through OSC messages from Supercollider. The ultimate goal is to have an OSC trigger stop and restart the search function.

Currently, it seems like the script is not able to listen to both the twitter stream and the OSC messages. i've been trying to combine the tweepy basic streamlistener code and the pyOSC basic_receive codes.

Here's what i came up with :

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()
Joel Ong
  • 3
  • 1

1 Answers1

0

It looks like the tweepy stream is only printing to stdout. If that is the case you would also want to send that stream/stdout to port 9000. So you would want to try and integrate a basic_send as well.

You could setup a socket connection: Python. Redirect stdout to a socket

Or you could setup a client:

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))

And then you could setup another OSC server or basic receive within the tweepy stream listener to control that class, setting up the listener on a different port.

Sounds like a fun art project.

Community
  • 1
  • 1
jmunsch
  • 22,771
  • 11
  • 93
  • 114