-
Notifications
You must be signed in to change notification settings - Fork 5
/
tweetsearching.py
51 lines (43 loc) · 1.22 KB
/
tweetsearching.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
import twitter
import twitter__login
import json
import pickle
import time
import random
import sys
import csv
t = twitter__login.login()
boston_res = t.search.tweets(q="patriots",geocode="42.350425,-71.026611,50mi",count=100)
baltimore_res = t.search.tweets(q="patriots",geocode="39.291797,-76.59668,50mi",count=100)
reslist = []
tweetfields = set()
for r in boston_res['statuses']:
if len(reslist)<5000:
print len(reslist)
r['searchloc'] = 'boston'
reslist.append(r)
tweetfields = tweetfields.union(r.keys())
else:
break
for r in baltimore_res['statuses']:
if len(reslist)<5000:
print len(reslist)
r['searchloc'] = 'baltimore'
reslist.append(r)
tweetfields = tweetfields.union(r.keys())
else:
break
fname = '/users/astorer/Work/presentations/twitter/nfl_tweets.csv'
f = open(fname,'w')
#dw = csv.DictWriter(f,fieldnames=list(tweetfields))
dw = csv.DictWriter(f,fieldnames=[u"text","searchloc"])
dw.writeheader()
for r in reslist:
subd = {}
for k in dw.fieldnames:
if isinstance(r[k],unicode):
subd[k] = r[k].encode('utf8')
else:
subd[k] = r[k]
dw.writerow(subd)
f.close()