From: Benjamin Mako Hill Date: Mon, 26 Jan 2015 21:09:25 +0000 (-0800) Subject: added a version that gives us raw json from the streaming API X-Git-Url: https://projects.mako.cc/source/twitter-api-cdsw/commitdiff_plain/5f84907a072eb4dd4b58dce2634f24ede6181eb8?hp=30dd074a58c070a2ac07650a5eef9c13c3e773a9 added a version that gives us raw json from the streaming API --- diff --git a/twitter-stream-raw1.py b/twitter-stream-raw1.py new file mode 100644 index 0000000..734a59f --- /dev/null +++ b/twitter-stream-raw1.py @@ -0,0 +1,30 @@ +import json +import tweepy +from twitter_authentication import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET + +auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) +auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) + +api = tweepy.API(auth, parser=tweepy.parsers.RawParser) + +@classmethod +def parse(cls, api, raw): + status = cls.first_parse(api, raw) + setattr(status, 'json', json.dumps(raw)) + return status + +tweepy.models.Status.first_parse = tweepy.models.Status.parse +tweepy.models.Status.parse = parse + +class StreamListener(tweepy.StreamListener): + def on_status(self, tweet): + print tweet.json + + def on_error(self, status_code): + print 'Error: ' + repr(status_code) + return False + +l = StreamListener() +streamer = tweepy.Stream(auth=auth, listener=l) + +streamer.sample()