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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#!/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
from users import listen_to_users
import json
import sys
class StdOutListener(StreamListener):
"""A listener handles tweets are the received from the stream."""
def __init__(self, api, memory, tempo):
"""Build the Python object."""
super().__init__()
self._api = api
self._memory = memory
self._tempo = tempo
self._previous = None
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:
if not ('id' in decoded['retweeted_status']):
print(decoded)
sid, username, content = self.get_status_info(decoded['retweeted_status'])
else:
if not ('id' in decoded):
print(decoded)
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:
if self._memory.is_original_content(content):
try:
if self._tempo:
if self._previous != None:
self._api.create_favorite(self._previous)
self._previous = sid
else:
self._api.create_favorite(sid)
# Save even pending statuses to remember them when looking for original content
self._memory.save_liked_status(sid, username, content)
print('@%s: "%s" (id=%d)' % (username, content, sid))
print(' -> https://twitter.com/%s/status/%d' % (username, sid))
except tweepy.error.TweepError:
pass
else:
print('Already seen "%s"' % content)
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, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
#memory = LikeMemory(api)
if len(sys.argv) > 1 and sys.argv[1] == '--purge':
memory.purge_old_status()
else:
listen_to_users(auth, api)
#listener = StdOutListener(api, memory, True)
#stream = Stream(auth, listener)
#stream.filter(track=hashtags.split(' '))
|