-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlistener.py
More file actions
43 lines (34 loc) · 1.18 KB
/
listener.py
File metadata and controls
43 lines (34 loc) · 1.18 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
32
33
34
35
36
37
38
39
40
41
42
43
from tweepy import StreamListener
import json, time, sys, csv
class SListener(StreamListener):
def __init__(self, api = None, fprefix = 'streamer'):
self.api = api or API()
self.counter = 0
self.tweets = open('Sanders.csv', 'w')
csv_writer = csv.writer(self.tweets)
def on_data(self, data):
if 'in_reply_to_status' in data:
self.on_status(data)
elif 'warning' in data:
warning = json.loads(data)['warnings']
print (warning['message'])
return false
def on_status(self, status):
data = json.loads(status)
if data['text'] != None and data['lang'] == 'en':
self.tweets.write(data['text'])
self.counter += 1
if self.counter >= 5000:
print('Done collecting')
self.tweets.close()
return
def on_limit(self, track):
sys.stderr.write(track + "\n")
return
def on_error(self, status_code):
sys.stderr.write('Error: ' + str(status_code) + "\n")
return False
def on_timeout(self):
sys.stderr.write("Timeout, sleeping for 60 seconds...\n")
time.sleep(60)
return