]> projects.mako.cc - twitter-api-cdsw-solutions/blob - twitter-stream-raw1.py
Fixed key error in some tweets
[twitter-api-cdsw-solutions] / twitter-stream-raw1.py
1 import json
2 import tweepy
3 from twitter_authentication import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
4
5 auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
6 auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
7
8 api = tweepy.API(auth, parser=tweepy.parsers.RawParser())
9
10 @classmethod                    
11 def parse(cls, api, raw):
12     status = cls.first_parse(api, raw)
13     setattr(status, 'json', json.dumps(raw))
14     return status
15
16 tweepy.models.Status.first_parse = tweepy.models.Status.parse
17 tweepy.models.Status.parse = parse
18
19 class StreamListener(tweepy.StreamListener):
20     def on_status(self, tweet):
21         print(tweet.json)
22
23     def on_error(self, status_code):
24         print('Error: ' + repr(status_code))
25         return False
26
27 l = StreamListener()
28 streamer = tweepy.Stream(auth=auth, listener=l)
29
30 streamer.sample()

Benjamin Mako Hill || Want to submit a patch?