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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
from auth import *
from config import hashtags, white_kwds
from db import LikeMemory
import json
import sys
class StdOutListener(StreamListener):
"""A listener handles tweets are the received from the stream."""
def __init__(self, api, memory):
"""Build the Python object."""
super().__init__()
self._api = api
self._memory = memory
self._white = [ s.lower() for s in white_kwds.split(' ') ]
def get_status_info(self, data):
"""Parse status data to get information about its author and content."""
# Do not rely on https://dev.twitter.com/overview/api/tweets
# as the specs seem outdated...
sid = data['id']
username = data['user']['screen_name']
if 'extended_tweet' in data:
content = data['extended_tweet']['full_text']
else:
content = data['text']
content = content.replace('\n', '')
return sid, username, content
def on_data(self, data):
"""Receive Tweets matching the given hashtags."""
decoded = json.loads(data)
if 'retweeted_status' in decoded:
sid, username, content = self.get_status_info(decoded['retweeted_status'])
else:
sid, username, content = self.get_status_info(decoded)
like = False
words = content.split(' ')
for kwd in self._white:
for w in words:
if w.lower() == kwd:
like = True
break
if like:
break
if like:
try:
self._api.create_favorite(sid)
self._memory.save_liked_status(sid, username)
print('@%s: "%s" (id=%d)' % (username, content, sid))
print(' -> https://twitter.com/%s/status/%d' % (username, sid))
except tweepy.error.TweepError:
pass
else:
print('Reject "%s"' % content)
return True
def on_error(self, code):
"""Handle errors."""
print('Error:', code)
if code == 420:
#returning False in on_data disconnects the stream
return False
if __name__ == '__main__':
"""Start of the script."""
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
memory = LikeMemory(api)
if len(sys.argv) > 1 and sys.argv[1] == '--purge':
memory.purge_old_status()
else:
listener = StdOutListener(api, memory)
stream = Stream(auth, listener)
stream.filter(track=hashtags.split(' '))
|