-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpigsearch.py
299 lines (249 loc) · 9.22 KB
/
pigsearch.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# All API search queries should be constructed through this file.
# This is essential a wrapper for our specific data set
from pysolr import Solr
from datetime import datetime, timedelta
import time
import json
# column_name data_type
# date_time date
# username text
# tweet text
# geoloc location
# hashtags text
# fb_weight int
# fb_assoc text
team_abbrev = [
'buf',
'mia',
'ne',
'nyj',
'bal',
'cin',
'cle',
'pit',
'hou',
'ind',
'jac',
'ten',
'den',
'kc',
'oak',
'sd',
'dal',
'nyg',
'phi',
'was',
'chi',
'det',
'gb',
'min',
'atl',
'car',
'no',
'tb',
'ari',
'stl',
'sf',
'sea'
]
# these times are in pacific time
weeks = [
"2012-10-21T10:00:00",
"2012-10-28T10:00:00",
"2012-11-4T10:00:00",
"2012-11-11T10:00:00",
"2012-11-18T10:00:00",
"2012-11-25T10:00:00",
"2012-12-2T10:00:00",
]
class FootballIndex:
def __init__(self):
self.conn = Solr('http://127.0.0.1:8080/solr/')
# returns the top 10 tweets
# and the totalHits in the query
def search(self, query,column='tweet'):
results = self.conn.search(column + ":" + query, wt='python', sort="fb_weight desc")
data = []
tweets = {}
for result in results:
data.append(result)
tweets["tweets"] = data
tweets['totalHits'] = results.hits
return tweets
def getTopTweetsForTeamWeek(self, team, date):
startdate = datetime.strptime(date,'%Y-%m-%dT%H:%M:%S') + timedelta(seconds=60*60*8)
enddate = startdate + timedelta(seconds=60*60*13) # 13 hrs later
fq1 = 'fb_assoc:' + team
fq2 = ' date_time:[' + startdate.isoformat() + 'Z TO ' + enddate.isoformat() + 'Z]'
results = self.conn.search("fb_weight:[2 TO *]", fq=[fq1,fq2],
wt='python', sort="fb_weight desc")
data = []
tweets = {}
for result in results:
data.append(result)
return data
def getTopTenTweetsForTeams(self):
tweets = {}
for team in team_abbrev:
fq = 'fb_assoc:' + team
results = self.conn.search('fb_weight:[2 TO *]',fq=fq,wt='python',sort='fb_weight desc')
array = []
for result in results:
print team
print result
array.append({'tweet':result['tweet'],
'username':result['screen_name'],
'fb_weight':result['fb_weight']})
tweets[team] = array
return tweets
def getTweetsPerMinute(self, date='2012-10-21T10:00:00', column='football'):
result = {}
startdate = datetime.strptime(date,'%Y-%m-%dT%H:%M:%S') + timedelta(seconds=60*60*8)
enddate = startdate + timedelta(seconds=60*60*13) # 13 hrs later
params = { 'facet.field' : ['fb_assoc'],
'facet.range' : ['date_time'],
'facet.range.start' : startdate.isoformat() + 'Z',
'facet.range.end' : enddate.isoformat() + 'Z',
'facet.range.gap':'+60000MILLISECOND',
'facet.mincount' : '1',
'facet.sort' : 'index'
}
counts = {}
if column == 'all':
dataset = self.conn.search("*:*",facet='on',
** params)
print dataset.hits
counts = dataset.facets['facet_ranges']['date_time']['counts']
result = convertToDict(counts)
elif column == 'football':
dataset = self.conn.search("fb_weight:[2 TO *]",facet='on',
** params)
for data in dataset:
print data
print dataset.hits
print dataset.facets['facet_fields']
counts = dataset.facets['facet_ranges']['date_time']['counts']
result = convertToDict(counts)
return result
# returns a dictionary of teamname:count
def getAllTweetsPerTeam(self):
params = { 'facet.field' : ['fb_assoc'],
'facet.sort' : 'index'
}
dataset = self.conn.search("fb_weight:[2 TO *]",facet='on',
** params)
return convertToDict(dataset.facets['facet_fields']['fb_assoc'])
# Get tweets per second for a given team on a given day.
# Input:
# team - official abbreviated teamname
# date - %Y-%m-%dT%H:%M:%S format 2012-10-21T10:00:00
# returns a dictionary of teamname:count
def getTweetsPerMinutePerTeam(self, team, date):
result = {}
startdate = datetime.strptime(date,'%Y-%m-%dT%H:%M:%S') + timedelta(seconds=60*60*8)
enddate = startdate + timedelta(seconds=60*60*13) # 13 hrs later
params = {'facet.range' : ['date_time'],
'facet.range.start' : startdate.isoformat() + 'Z',
'facet.range.end' : enddate.isoformat() + 'Z',
'facet.range.gap':'+60000MILLISECOND',
# 'facet.mincount' : '1',
'facet.sort' : 'index'
}
dataset = self.conn.search("fb_weight:[2 TO *]",fq='fb_assoc:'+team,
facet='on', ** params)
print dataset.hits
counts = dataset.facets['facet_ranges']['date_time']['counts']
# print counts
result = convertToDict(counts)
return result
def getFootballTweetsPerWeek(self):
result = {}
startdate = datetime.strptime(weeks[0],'%Y-%m-%dT%H:%M:%S') + timedelta(seconds=60*60*8)
enddate = datetime.strptime(weeks[-1],'%Y-%m-%dT%H:%M:%S') + timedelta(seconds=60*60*13) # 13 hrs later
params = {'facet.range' : ['date_time'],
'facet.range.start' : startdate.isoformat() + 'Z',
'facet.range.end' : enddate.isoformat() + 'Z',
'facet.range.gap':'+1DAY',
# 'facet.mincount' : '1',
'facet.sort' : 'index'
}
dataset = self.conn.search("fb_weight:[2 TO *]",
facet='on', ** params)
print dataset.hits
counts = dataset.facets['facet_ranges']['date_time']['counts']
result = convertToDict(counts)
return result
def getNonFootballTweetsPerWeek(self):
result = {}
startdate = datetime.strptime(weeks[0],'%Y-%m-%dT%H:%M:%S') + timedelta(seconds=60*60*8)
enddate = datetime.strptime(weeks[-1],'%Y-%m-%dT%H:%M:%S') + timedelta(seconds=60*60*13) # 13 hrs later
params = {'facet.range' : ['date_time'],
'facet.range.start' : startdate.isoformat() + 'Z',
'facet.range.end' : enddate.isoformat() + 'Z',
'facet.range.gap':'+1DAY',
# 'facet.mincount' : '1',
'facet.sort' : 'index'
}
dataset = self.conn.search("fb_weight:[* TO 1]",
facet='on', ** params)
print dataset.hits
counts = dataset.facets['facet_ranges']['date_time']['counts']
result = convertToDict(counts)
return result
def outputAllTweets():
index = FootballIndex()
results = {}
results['nonfootball'] = index.getNonFootballTweetsPerWeek()
results['football'] = index.getFootballTweetsPerWeek()
f = open("allFootballTweetsAndNon.json",'w')
print results
f.write(json.dumps(results,indent=4))
return
def outputScript():
index = FootballIndex()# write out multiple json files
output = {}
# tweets per second per team
for team in team_abbrev:
f = open("tweetsPerMinute_" + team + ".json","w")
for week, i in zip(weeks, range(7,14)):
print team
print week
print i
tweets = index.getTweetsPerMinutePerTeam(team,week)
output[i] = tweets
# print output
f.write(json.dumps(output,indent=4))
return
def outputAllTopTen():
index = FootballIndex()
data = index.getTopTenTweetsForTeams()
f = open('all_top_ten_tweets.json','w')
f.write(json.dumps(data,indent=4))
return
def outputTopTenTweets():
index = FootballIndex()
for team in team_abbrev:
for week, i in zip(weeks, range(7,14)):
f = open("top_ten_wk_" + str(i) + "_" + team + ".json", 'w')
results = index.getTopTweetsForTeamWeek(team,week)
array = []
for result in results:
print team
print result
array.append({'tweet':result['tweet'],
'username':result['screen_name'],
'fb_weight':result['fb_weight']})
output = {team:array}
f.write(json.dumps(output,indent=4))
return
# Helper
def convertToDict(countList):
result = []
for t, value in zip(countList[::2],countList[1::2]):
result.append({'x':convertToEpochTime(t), 'y':value})
# print result
return result
def convertToEpochTime(t):
# t is in gmt; must conver to pst
timestamp = datetime.strptime(t,'%Y-%m-%dT%H:%M:%SZ') - timedelta(seconds=60*60*8)
return int(time.mktime(timestamp.timetuple()))