-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitterlistener.py~
62 lines (51 loc) · 2.06 KB
/
twitterlistener.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
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import sys, json, shelve
import parsetest
from pyspatialite import dbapi2 as spatialdb
## security shit (change to your own)
consumer_key = '1nrA8Nqe4awDmy5c4D5fg'
consumer_secret = 'WvtbdEYvcuXiNGhBVa4TOjiLwTEF5Xra0FrEdgA'
access_token = '183620373-VxCczsD8En3OH8K0s8OebXRFPd7MS54Npr1JZaJ6'
access_token_secret = 'psP1iNbGhxqH8aFD1TKvEcHzmLPWi6Inp9g6i81LoA'
# this is to make it so i dont need to look it up because i forget the syntax
def runSql(inDb, sqlCommand, params):
"""takes sql command and executes it in a transaction. read sqlite3 documentation """
conn = sqlite3.Connection(inDb)
cur = conn.cursor()
cur.execute(sqlCommand, params)
conn.commit()
conn.close()
# found this in the tweepy examples and changed it slightly to dump straight to db
class StdOutListener(StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def __init__(self, conn=None,cur=None):
self.conn = conn
self.cur = cur
def on_data(self, data):
try:
data = json.loads(r'{0}'.format(data))
parsetest.run(data,self.cur, self.conn)
except Exception as e:
print 'error here', e,
finally:
self.conn.commit()
self.conn.close()
def on_error(self, status):
print 'error', status
if __name__ == '__main__':
# in database name
inDb = sys.argv[1]
# terms to look for
keywords = sys.argv[2:] # example ['obama','romney','ryan','biden','independent','republican','democrat']
#open a shelve (stupid python key value store)
conn = spatialdb.Connection(inDb)
cur = conn.cursor()
l = StdOutListener(conn, cur)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track=keywords, locations =[-124,24,-66,49])#united states bbox