-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweet_from_AK.py
83 lines (67 loc) · 2.46 KB
/
Tweet_from_AK.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
#!/usr/bin/env python
from tweepy import *
import time, tweepy, sys
import os, urllib
import simplejson,json
import csv, string
OAUTH_TOKEN = ''
OAUTH_SECRET = ''
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_SECRET)
api = tweepy.API(auth)
class SListener(StreamListener):
def __init__(self, api = None, fprefix = 'streamer'):
self.api = api
self.counter = 0
self.fprefix = fprefix
self.output = open('data_AK/' + fprefix + '.'
+ time.strftime('%Y%m%d-%H%M%S') + '.json', 'w')
self.delout = open('data_AK/delete.txt', 'a')
def on_data(self, data):
if json.loads(data).has_key('coordinates') and json.loads(data)['coordinates'] :
if 'in_reply_to_status' in data:
self.on_status(data)
elif 'delete' in data:
delete = json.loads(data)['delete']['status']
if self.on_delete(delete['id'], delete['user_id']) is False:
return False
elif 'limit' in data:
if self.on_limit(json.loads(data)['limit']['track']) is False:
return False
elif 'warning' in data:
warning = json.loads(data)['warnings']
print warning['message']
return False
def on_status(self, status):
self.output.write(status + "\n")
self.counter += 1
if self.counter >= 20000:
self.output.close()
self.output = open('../data_AK/' + self.fprefix + '.'
+ time.strftime('%Y%m%d-%H%M%S') + '.json', 'w')
self.counter = 0
return
def on_delete(self, status_id, user_id):
self.delout.write( str(status_id) + "\n")
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
def main():
print 'Streaming started'
while True:
try:
stream = Stream(auth, SListener())
stream.filter(track=['a','e','i','o','u'], locations=[-173.008060,53.293648,-142.070559,71.671710])
except:
time.sleep(10)
main()