class CustomStreamListener(tweepy.StreamListener): def __init__(self): tweepy.StreamListener.__init__(self) self.mid_list = [] self.ti = TweetInterface() def save_to_mongo(self,tweet): tweet = json.loads(tweet.json) if tweet['coordinates'] is None: return tweet['_id'] = tweet['id'] self.ti.saveDocument(tweet) def on_status(self, status): print 'get' try: print "%s\t%s\t%s\t%s\t%s" % (status.text, status.author.screen_name, status.created_at, status.source, status.coordinates['coordinates'] ) self.save_to_mongo(status) except Exception, e: print >> sys.stderr, 'Encountered Exception:', e pass def on_error(self, status_code): print >> sys.stderr, 'Encountered error with status code:', status_code return True # Don't kill the stream def on_timeout(self): print >> sys.stderr, 'Timeout...' return True # Don't kill the stream
class CustomStreamListener(tweepy.StreamListener): def __init__(self, db=TwitterConfig.tweet_db, collection=TwitterConfig.tweet_collection): tweepy.StreamListener.__init__(self) self.mid_list = [] self.ti = TweetInterface(db=db, collection=collection) def save_to_mongo(self,tweet): tweet = json.loads(tweet.json) tweet['_id'] = tweet['id'] self.ti.saveDocument(tweet, must_have_geo_tag=False) def on_status(self, status): print 'get' try: print "%s\t%s\t%s\t%s" % (status.text, status.author.screen_name, status.created_at, status.source, ) self.save_to_mongo(status) except Exception, e: print >> sys.stderr, 'Encountered Exception:', e pass def on_error(self, status_code): print >> sys.stderr, 'Encountered error with status code:', status_code return True # Don't kill the stream def on_timeout(self): print >> sys.stderr, 'Timeout...' return True # Don't kill the stream