-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyword_streamer.py
95 lines (74 loc) · 3.27 KB
/
keyword_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
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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 28 15:08:20 2014
@author: aitor
"""
import datetime
import json
import gzip
import time
import sys
import tweepy
from tweepy import StreamListener, Stream
credentials = json.load(open('credentials.json', 'r'))
CONSUMER_KEY = credentials['consumer_key']
CONSUMER_SECRET = credentials['consumer_secret']
OAUTH_TOKEN = credentials['oauth_token']
OAUTH_TOKEN_SECRET = credentials['oauth_secret']
tweets = []
initial_time = time.time()
class StdOutListener(StreamListener):
def on_data(self, raw_data):
global tweets, initial_time
elapsed_time = time.time () - initial_time #elapsed secons
#save the status every 30 mins
if elapsed_time >= 60 * 30:
now = datetime.datetime.now()
file_name = './tweets/tweets-%s-%s-%s-%s-%s.txt.gz' % (now.month, now.day, now.hour, now.minute, now.second)
print '(%s-%s %s:%s:%s) %s' % (now.month, now.day, now.hour, now.minute, now.second, 'Saving file')
with gzip.open(file_name, 'w') as f:
for tweet in tweets:
f.write(json.dumps(tweet) + '\n')
tweets = []
initial_time = time.time()
try:
data = json.loads(raw_data)
tweets.append(data)
except:
now = datetime.datetime.now()
print '(%s-%s %s:%s:%s) Invalid JSON Data %s' % (now.month, now.day, now.hour, now.minute, now.second, raw_data)
sys.stdout.flush()
return True
def on_error(self, status_code):
now = datetime.datetime.now()
print '(%s-%s %s:%s:%s)Got an error with status code: %s' % (now.month, now.day, now.hour, now.minute, now.second, status_code)
sys.stdout.flush()
#sleep 5 mins if an error occurs
time.sleep(5 * 60)
return True # To continue listening
def on_timeout(self):
now = datetime.datetime.now()
print '(%s-%s %s:%s:%s) %s' % (now.month, now.day, now.hour, now.minute, now.second, 'Timeout')
sys.stdout.flush()
return True # To continue listening
if __name__ == '__main__':
now = datetime.datetime.now()
print '(%s-%s %s:%s:%s) %s' % (now.month, now.day, now.hour, now.minute, now.second, 'Starting')
sys.stdout.flush()
keywords = json.load(open('keywords.json', 'r'))
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
listener = StdOutListener()
stream = Stream(auth, listener)
while True:
try:
# https://dev.twitter.com/streaming/reference/post/statuses/filter
stream.filter(track=keywords)
# With stream.filter(follow=IDs) to follow accounts
except Exception as e:
print '(%s-%s %s:%s:%s) %s' % (now.month, now.day, now.hour, now.minute, now.second, "Error streaming")
print '(%s-%s %s:%s:%s) %s' % (now.month, now.day, now.hour, now.minute, now.second, e.message)
sys.stdout.flush()
time.sleep(1 * 60)
now = datetime.datetime.now()
print '(%s-%s %s:%s:%s) %s' % (now.month, now.day, now.hour, now.minute, now.second, 'Done')