def startProducing(self): send = make_send(self) class Consumer(TwistedTwitterStream.TweetReceiver): def connectionFailed(this, why): log(self, 'connection failed (%s)' % why) def tweetReceived(this, data): if 'delete' not in data: send(dict(tweet=data)) TwistedTwitterStream.sample(TWITTER_USER, TWITTER_PASSWORD, Consumer())
def refresh(klass,terms): log.msg(klass, ": refresh stream") dispenser = klass(terms) # when there is an instance currently running, we'll have to make sure # it disconnects. however, to make sure we don't lose too much tweets, # only disconnect it when the first tweet arrives on the new channel if len(klass.instances) > 0: dispenser.onConnectionCallback = klass.instances[-1].teardown klass.instances.append(dispenser) # create query string (always less than 200 tracking words) qs = klass.query_string(terms) # create a stream for the new dispenser usr, pwd = os.environ['TWUSER'], os.environ['TWPASS'] factory = TwistedTwitterStream._TwitterStreamFactory(dispenser) factory.make_header(usr, pwd, "POST", "/1/statuses/filter.json", qs) dispenser.connector = reactor.connectTCP("stream.twitter.com", 80, factory) dispenser.factory = factory dispenser.term_channel_map = klass.create_term_channel_map(terms) dispenser.term_set = set(dispenser.term_channel_map.keys()) log.msg(klass,dispenser.term_channel_map) log.msg(klass,dispenser.term_set) return dispenser
def startProducing(self): send = make_send(self) topics = [] track = [] for terms, tag in self.node['_topics']: topics.append((tag, [ x.lower().split() for x in terms ])) track.extend( x.split(None, 1)[0] for x in terms ) class Consumer(TwistedTwitterStream.TweetReceiver): def connectionFailed(this, why): log(self, 'connection failed (%s)' % why) def tweetReceived(this, data): if 'delete' not in data: text = data['text'].lower() for tag, kws in topics: if any( all( w in text for w in kw ) for kw in kws ): send(dict(tweet=data, word=tag)) TwistedTwitterStream.filter(TWITTER_USER, TWITTER_PASSWORD, Consumer(), track=track)
#!/usr/bin/env python # coding: utf-8 # # follow is a list of user IDs, not screen names # use something like this to get user IDs: # # screen_name = "BarackObama" # fd = urllib.urlopen("http://twitter.com/users/show/%s.json" % screen_name) # doc = simplejson.loads(fd.read()) # fd.close() # print doc.get("id") import TwistedTwitterStream from twisted.internet import reactor class consumer(TwistedTwitterStream.TweetReceiver): def connectionMade(self): print "connected..." def connectionFailed(self, why): print "cannot connect:", why reactor.stop() def tweetReceived(self, tweet): print "new tweet:", repr(tweet) if __name__ == "__main__": TwistedTwitterStream.filter("username", "password", consumer(), follow=["101010", "202020", "303030", "404040"]) reactor.run()
#!/usr/bin/env python # coding: utf-8 import TwistedTwitterStream from twisted.internet import reactor class consumer(TwistedTwitterStream.TweetReceiver): def connectionMade(self): print "connected..." def connectionFailed(self, why): print "cannot connect:", why reactor.stop() def tweetReceived(self, tweet): print "new tweet:", repr(tweet) if __name__ == "__main__": #TwistedTwitterStream.firehose("username", "password", consumer()) #TwistedTwitterStream.retweet("username", "password", consumer()) TwistedTwitterStream.sample("username", "password", consumer()) reactor.run()
import TwistedTwitterStream from twisted.internet import reactor class consumer(TwistedTwitterStream.TweetReceiver): def connectionMade(self): print "connected..." def connectionFailed(self, why): print "cannot connect:", why reactor.stop() def tweetReceived(self, tweet): print "new tweet:", repr(tweet) if __name__ == "__main__": TwistedTwitterStream.filter("username", "password", consumer(), follow=["101010", "202020", "303030", "404040"]) reactor.run() ########NEW FILE######## __FILENAME__ = sample #!/usr/bin/env python # coding: utf-8 import TwistedTwitterStream from twisted.internet import reactor class consumer(TwistedTwitterStream.TweetReceiver): def connectionMade(self): print "connected..." def connectionFailed(self, why):
#!/usr/bin/env python # coding: utf-8 import TwistedTwitterStream from twisted.internet import reactor class consumer(TwistedTwitterStream.TweetReceiver): def connectionMade(self): print "connected..." def connectionFailed(self, why): print "cannot connect:", why reactor.stop() def tweetReceived(self, tweet): print "new tweet:", repr(tweet) # you may call self.disconnect() anytime to # permanently disconnect from the Twitter Stream if __name__ == "__main__": TwistedTwitterStream.filter( "username", "password", consumer(), track=["football", "soccer", "world cup", "palmeiras"]) reactor.run()
# # screen_name = "BarackObama" # fd = urllib.urlopen("http://twitter.com/users/show/%s.json" % screen_name) # doc = simplejson.loads(fd.read()) # fd.close() # print doc.get("id") import TwistedTwitterStream from twisted.internet import reactor class consumer(TwistedTwitterStream.TweetReceiver): def connectionMade(self): print "connected..." def connectionFailed(self, why): print "cannot connect:", why reactor.stop() def tweetReceived(self, tweet): print "new tweet:", repr(tweet) if __name__ == "__main__": TwistedTwitterStream.filter( "username", "password", consumer(), follow=["101010", "202020", "303030", "404040"]) reactor.run()
class consumer(TwistedTwitterStream.TweetReceiver): def connectionMade(self): print "connected..." def connectionFailed(self, why): print "cannot connect:", why reactor.stop() def tweetReceived(self, tweet): print "new tweet:", repr(tweet) form = urllib.urlencode({"value": tweet}) d = client.getPage( "http://localhost:8888/q/twitter", method="POST", postdata=form, headers={"Content-Type": "application/x-www-form-urlencoded"}, ) def err(w): print "E: ", w d.addErrback(err) if __name__ == "__main__": TwistedTwitterStream.filter("username", "pass", consumer(), track=["fail"]) reactor.run()
#!/usr/bin/env python # coding: utf-8 import TwistedTwitterStream from twisted.internet import reactor class consumer(TwistedTwitterStream.TweetReceiver): def connectionMade(self): print "connected..." def connectionFailed(self, why): print "cannot connect:", why reactor.stop() def tweetReceived(self, tweet): print "new tweet:", repr(tweet) # you may call self.disconnect() anytime to # permanently disconnect from the Twitter Stream if __name__ == "__main__": TwistedTwitterStream.filter("username", "password", consumer(), track=["football", "soccer", "world cup", "palmeiras"]) reactor.run()
#!/usr/bin/env python # coding: utf-8 import TwistedTwitterStream from twisted.internet import reactor import oauth class consumer(TwistedTwitterStream.TweetReceiver): def connectionMade(self): print "connected..." def connectionFailed(self, why): print "cannot connect:", why reactor.stop() def tweetReceived(self, tweet): print "new tweet:", repr(tweet) if __name__ == "__main__": #TwistedTwitterStream.firehose("username", "password", consumer()) #TwistedTwitterStream.retweet("username", "password", consumer()) OAconsumer = oauth.OAuthConsumer('uu', 'bb') token = oauth.OAuthToken('xx', 'zz') TwistedTwitterStream.sample("username", "password", consumer(), OAconsumer=OAconsumer, token=token) reactor.run()