-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen_streamer.py
51 lines (40 loc) · 1.41 KB
/
screen_streamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import tweepy
from config import settings
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
retweeted = False
tweet = status.text
original_tweet = None
if hasattr(status, 'retweeted_status'):
retweeted = True
try:
original_tweet = status.retweeted_status.extended_tweet["full_text"] # noqa
except AttributeError:
original_tweet = status.retweeted_status.text
else:
try:
tweet = status.extended_tweet["full_text"]
except AttributeError:
tweet = status.text
try:
screen_name = status.user.screen_name
except AttributeError:
screen_name = None
if original_tweet:
output_tweet = original_tweet
else:
output_tweet = tweet
print(screen_name, '-', retweeted, '-', output_tweet)
print('-'*120)
def on_error(self, status_code):
if status_code == 420:
return False
auth = tweepy.OAuthHandler(
settings.TWITTER_APP_KEY,
settings.TWITTER_APP_SECRET
)
auth.set_access_token(settings.TWITTER_KEY, settings.TWITTER_SECRET)
api = tweepy.API(auth)
stream_listener = StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=stream_listener, tweet_mode='extended') # noqa
stream.filter(track=['KamalaHarris'], is_async=True)