/
main.py
executable file
·329 lines (251 loc) · 9.08 KB
/
main.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
from ebaysdk import finding
from collections import Counter
from geopy import geocoders
import logging
cache_zip = {}
class ebaySearcher :
def __init__(self) :
self.api = finding(appid="corbinso-0418-4f1f-9512-0758cbf2744c")
self.zip_counter=0 #counts the number of buyers from this zipcode
self.avg_price=0
self.zip_price=0
self.num_items =0 #the number of results
def search(self,term) :
self.api.execute('findItemsByKeywords', {'keywords': term,
'paginationInput' :
{'entriesPerPage' : '100', 'pageNumber' : '1'},
'itemFilter' : {'name' : 'ListingType',
'value' : ["FixedPrice", "AuctionWithBIN"]}
})
find_output = self.api.response_dict()
items= find_output['searchResult']['item']
pages_to_scan=find_output['paginationOutput']['totalPages']['value']
if pages_to_scan > 15:
pages_to_scan = 14
for x in xrange(2,pages_to_scan) :
self.api.execute('findItemsByKeywords', {'keywords': term,
'paginationInput' : {'entriesPerPage' : '100',
'pageNumber' : str(x)},
'itemFilter' : {'name' : 'ListingType',
'value' : ["FixedPrice", "AuctionWithBIN"]}
})
find_output = self.api.response_dict()
items.extend(find_output['searchResult']['item'])
self.avg_price=0
self.zip_counter=Counter()
self.zip_price=Counter()
self.num_items= len (items)
for i in items :
try :
self.zip_counter[i['postalCode']['value']]+=1;
if i['listingInfo']['buyItNowAvailable']['value'] =="true" :
price = float(i['listingInfo']['buyItNowPrice']['value'])
else :
price = float(i['sellingStatus']['convertedCurrentPrice']['value'])
self.avg_price+=price
self.zip_price[i['postalCode']['value']]+=price;
except KeyError :
self.num_items -=1
self.avg_price/=self.num_items
front_html_b = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Ebay Tracker</title>
<style type="text/css">
#map {
width: 960px;
height: 690px;
}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
/**
* Called on the intiial page load.
*/
function init() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: new google.maps.LatLng(44, -80),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
"""
front_html_m = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Ebay Tracker</title>
<style type="text/css">
#map {
width: 320px;
height: 480px;
}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
/**
* Called on the intiial page load.
*/
function init() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: new google.maps.LatLng(44, -80),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
"""
center_html = """
function circle(x,y,r,c) {
var populationOptions = {
strokeColor: "#FFFF00",
strokeOpacity: 0.1,
strokeWeight: 2,
fillColor: c,
fillOpacity: 0.12,
map: map,
center: new google.maps.LatLng(x, y),
radius: r
};
cityCircle = new google.maps.Circle(populationOptions);
}
}
// Register an event listener to fire once when the page finishes loading.
google.maps.event.addDomListener(window, 'load', init);
</script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
"""
back_html_b = """
]);
var options = {
title: '"""
super_back_b = """',
backgroundColor: '#EBF5F6',
colors: ['#D94F36','#ABBC6B','#87B8BB'],
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="map"></div>
<div id="chart_div" style="width: 400; height: 200px; position:absolute; bottom: 0; right: 0;"></div>
</body>
</html>
"""
back_html_m = """
]);
var options = {
title: '"""
super_back_m = """'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="map"></div>
<div id="chart_div" style="width: 330px; height: 100px; position:absolute; bottom: 0; left: 0;"></div>
</body>
</html>
"""
cache_graph = {}
cache_data = {}
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('hello')
class SearchHandler(webapp2.RequestHandler):
def get(self):
test = self.request.get('q')
if test == None:
test = 'iPad'
if test in cache_data.keys():
self.response.write(front_html_b+cache_graph[test]+center_html+cache_data[test]+back_html_b+test+super_back_b)
return
(additions,data) = getDataForSearch(test)
map_string = ''
for s in additions:
map_string += s
data_string = ''
for d in data:
data_string += d
cache_graph[test] = map_string
cache_data[test] = data_string
self.response.write(front_html_b+map_string+center_html+data_string+back_html_b+test+super_back_b)
class MobileSearchHandler(webapp2.RequestHandler):
def get(self):
test = self.request.get('q')
if test in cache_data.keys():
self.response.write(front_html_m+cache_graph[test]+center_html+cache_data[test]+back_html_m+test+super_back_m)
return
(additions,data) = getDataForSearch(test)
map_string = ''
for s in additions:
map_string += s
data_string = ''
for d in data:
data_string += d
cache_graph[test] = map_string
cache_data[test] = data_string
self.response.write(front_html_m+map_string+center_html+data_string+back_html_m+test+super_back_m)
app = webapp2.WSGIApplication([
('/', MainHandler),
('/hack/', SearchHandler),
('/hackm/', MobileSearchHandler),
], debug=True)
def getCircleString(long,lat,radius,color="FF0F0F"):
string = 'circle('+str(long)+','+str(lat)+','+str(30000*radius)+',"%s");' % color
return string
def getDataString(x,y):
return "['%s',%i]," % (x,y)
searcher=ebaySearcher()
def getDataForSearch(search):
gd = geocoders.GeoNames()
searcher.search(search)
cites=searcher.zip_counter
additions = []
for city in cites :
try:
coords= gd.geocode("US "+str(city),exactly_one=False)[0][1]
additions.append(getCircleString(coords[0] ,coords[1], cites[city]))
except TypeError : pass
city_prices=searcher.zip_price
avg_price=searcher.avg_price
num_items = searcher.num_items
data = ["['City', 'P'],"]
for i, city in enumerate(city_prices.most_common(10)):
if i < 7:
data.append(getDataString(str(city[0]),city[1]/cites[city[0]]))
"""data = [ "['Year', 'Sales'],",
getDataString(2004,1000),
"['2005', 1170],",
"['2006', 660],",
"['2007', 830],",
"['2008', 1230],",
"['2009', 830],",
"['2010', 1230],"]"""
return (additions,data)