-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
31 lines (25 loc) · 1.08 KB
/
service.py
File metadata and controls
31 lines (25 loc) · 1.08 KB
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
import datetime
import logging
from time import sleep
from nameko.rpc import rpc
from tweepy import OAuthHandler, Stream
from config import ACCESS_TOKEN, ACCESS_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET
from twitter_collector import TwitterStreamer
class DataCollector(object):
name = "collector"
@rpc
def collect(self, duration, query, translate):
end_time = datetime.datetime.utcnow() + datetime.timedelta(minutes=duration)
logger.info("end_time: {}, query: {}".format(end_time, query, translate))
if datetime.datetime.utcnow() < end_time:
while datetime.datetime.utcnow() < end_time:
try:
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
twitterStream = Stream(
auth, TwitterStreamer(query, end_time, translate)
)
twitterStream.filter(track=query)
except Exception as e:
sleep(5)
logging.error(e)