-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_eq.py
209 lines (186 loc) · 7.82 KB
/
twitter_eq.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from datetime import datetime
import json
import logging
import os
from urllib.parse import quote_plus
from prometheus_client import Counter
import twitter
TWITTER_QUERIES = Counter('twitter_queries', '# of queries made to Twitter')
SCENARIOS = {
'base': {
'oldest_id': 'newest_id',
'newest_id': 'newest_id',
'last_success': 'newest_id',
'success': 'true'
},
'current_fail_last_success': {
'oldest_id': 'oldest_id',
'newest_id': 'previous_newest_id',
'last_success': 'newest_id',
'success': 'false'
},
'current_fail_last_fail': {
'oldest_id': 'oldest_id',
'newest_id': 'previous_newest_id',
'last_success': 'last_success',
'success': 'false'
},
'current_success_last_success': {
'oldest_id': 'oldest_id',
'newest_id': 'newest_id',
'last_success': 'newest_id',
'success': 'true'
},
'current_success_last_fail': {
'oldest_id': 'oldest_id',
'newest_id': 'last_success',
'last_success': 'previous_newest_id',
'success': 'true'
}
}
class Twitter(object):
def __init__(self):
self.client = None
self.auth()
def auth(self):
"""
Connects to Twitter
"""
self.client = twitter.Api(
consumer_key=os.getenv('TWITTER_CONSUMER_KEY'),
consumer_secret=os.getenv('TWITTER_CONSUMER_SECRET'),
access_token_key=os.getenv('TWITTER_ACCESS_TOKEN_KEY'),
access_token_secret=os.getenv('TWITTER_ACCESS_TOKEN_SECRET'),
application_only_auth=True
)
class Search(object):
"""
This class searches Twitter hashtags for a given string and stores the results in Redis
Args:
redis_client (redis object): Redis DB client
twitter_client (python-twitter object): Twitter API client
search_term (str): Term we are querying twitter for
"""
def __init__(self, redis_client, twitter_client, search_term):
self.redis_client = redis_client
self.twitter_client = twitter_client
self.search_term = search_term
self.previous_newest_id = None
self.previous_oldest_id = None
self.last_success = None
self.success = None
self.newest_id = None
self.oldest_id = None
self.scenario = None
self.new_term_state = {}
self.execution_time = None
self.results = None
self.query_string = None
def set_query_string(self):
"""
Sets the query string for the Twitter client search
"""
self.query_string = f'q=%23{quote_plus(self.search_term)}&result_type=recent&count=100'
if self.previous_newest_id:
if self.previous_oldest_id <= self.previous_newest_id or self.success:
self.query_string += f'&since_id={self.previous_newest_id}'
else:
self.query_string += f'&max_id={self.previous_oldest_id}'
self.query_string += f'&since_id={self.previous_newest_id}'
def set_execution_time(self):
# We store execution time in Redis so that we can ensure we don't exceed the Twitter API rate limits
self.execution_time = datetime.utcnow().timestamp()
def execute_query(self):
self.results = self.twitter_client.GetSearch(raw_query=self.query_string)
def incr_query_counters(self):
TWITTER_QUERIES.inc()
self.redis_client.rpush('query_counter', self.execution_time)
def set_newest_id(self):
if self.results:
self.newest_id = getattr(self.results[0], 'id')
def set_oldest_id(self):
if self.results:
self.oldest_id = getattr(self.results[-1], 'id')
def parse_term_state_datum(self, datum):
"""
Data retrieved from Redis is of byte type and we need to converted it to integer or None
"""
if datum:
if datum != b'null':
datum = int(datum.decode('utf-8'))
else:
datum = None
return datum
def get_term_state(self):
term_state = self.redis_client.hmget(self.search_term, 'newest_id', 'oldest_id', 'last_success', 'success')
self.previous_newest_id, self.previous_oldest_id, self.last_success, self.success = term_state
def parse_term_state(self):
self.previous_newest_id = self.parse_term_state_datum(self.previous_newest_id)
self.previous_oldest_id = self.parse_term_state_datum(self.previous_oldest_id)
self.last_success = self.parse_term_state_datum(self.last_success)
if not self.success:
self.success = True
else:
self.success = True if self.success == 'true' else False
def set_scenario(self):
"""
Based on the Tweets we found in our query, we need to acknowledge which scenario our current search state is in
"""
if not self.previous_newest_id or not self.oldest_id:
self.scenario = 'base'
elif (self.oldest_id > self.previous_newest_id) and self.success:
self.scenario = 'current_fail_last_success'
elif (self.oldest_id > self.previous_newest_id) and not self.success:
self.scenario = 'current_fail_last_fail'
elif (self.oldest_id <= self.previous_newest_id) and self.success:
self.scenario = 'current_success_last_success'
elif (self.oldest_id <= self.previous_newest_id) and not self.success:
self.scenario = 'current_success_last_fail'
def set_term_state(self):
for key, value in SCENARIOS[self.scenario].items():
if key == 'success':
self.new_term_state[key] = value
else:
new_value = getattr(self, value)
if not new_value:
new_value = 'null'
self.new_term_state[key] = new_value
self.redis_client.hmset(self.search_term, self.new_term_state)
def set_score(self):
"""
This function sets the score for our search term in Redis
If we found all the recently-posted Tweets in this query
Set the score to 99 . . . 99. This ensures that this term is pulled last from the queue
If we did not find all the recently-posted Tweets,
Set the score to the int of the last fully-collected period for this term (self.last_success)
This ensures that the search terms we are farthest behind on are queried first.
"""
if self.success:
# TODO: Is this hack okay?
self.redis_client.hset(self.search_term, 'score', 9999999999999999999)
else:
if not self.last_success:
self.redis_client.hset(self.search_term, 'score', 9999999999999999999)
else:
self.redis_client.hset(self.search_term, 'score', self.last_success)
self.redis_client.sadd('search_terms', self.search_term)
def store_results(self):
"""
Pushes each result onto a Redis queue
TODO: Why iterate and do this one by one. Can I push it all as a list?
"""
for result in self.results:
result = result.AsDict()
result['search_term'] = self.search_term
self.redis_client.rpush('tweets', json.dumps(result, ensure_ascii=True, sort_keys=True))
def log_state(self):
"""
This function logs the important attributes of the object
"""
bad_vars = ['results', 'redis_client', 'twitter_client']
class_vars = [attr for attr in dir(self) if
not callable(getattr(self, attr)) and not attr.startswith("__") and attr not in bad_vars]
class_data = {}
for class_var in class_vars:
class_data[class_var] = getattr(self, class_var)
logging.info(f'Logging search object data for execution at {self.execution_time}', extra=class_data)