-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoto_url.py
354 lines (263 loc) · 8.53 KB
/
goto_url.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import cgi
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from data import UrlHash, UrlHit, Location, CountryHits, CountryUrlHits, CityHits, CityUrlHits, RegionHits, RegionUrlHits, IpHits, IpUrlHits
import logging
import random
class GotoUrl(webapp.RequestHandler):
def getGeoIPCode(self, ipaddr):
memcache_key = "geo_info_%s" % ipaddr
data = memcache.get(memcache_key)
if data is not None:
return data
geo_info = None
try:
#fetch_response = urlfetch.fetch(http://geoip.wtanaka.com/cc/%s' % ipaddr)
fetch_response = urlfetch.fetch("http://freegeoip.appspot.com/csv/%s" % ipaddr)
if fetch_response.status_code == 200:
geo_info = fetch_response.content
except urlfetch.Error, e:
pass
if geo_info:
memcache.set(memcache_key, geo_info)
return geo_info
def get_url_hash(self, hash, update_hits = True):
url_hashes = db.GqlQuery("select * from UrlHash where hash = :h", h=hash)
url = None
for url_hash in url_hashes:
return url_hash
def add_hit(self, url_hash, ip, uagent, loc):
url_hit = UrlHit()
url_hit.url = url_hash.url
url_hit.hash = url_hash.hash
url_hit.ip = ip
url_hit.uagent = uagent
url_hit.loc_cc = loc.cc
url_hit.loc_cn = loc.cn
url_hit.loc_rc = loc.rc
url_hit.loc_rn = loc.rn
url_hit.loc_ct = loc.ct
url_hit.loc_zc = loc.zc
url_hit.loc_ll = loc.ll
url_hit.put()
def get_rand_ip(self):
return ".".join([str(random.randrange(100, 102)) for x in range(4) ])
def get_location(self, ip):
try:
geo_info = self.getGeoIPCode(ip).split(",")
except:
geo_info = ['', '', '', '', '', '', '', '', '', '']
loc = Location()
loc.cc = geo_info[2]
loc.cn = geo_info[3]
loc.rc = geo_info[4]
loc.rn = geo_info[5]
loc.ct = geo_info[6]
loc.zc = geo_info[7]
loc.ll = geo_info[8] + "," + geo_info[9]
return loc
def get_country_hits(self, country):
chits_list = db.GqlQuery("select * from CountryHits where country = :c", c=country)
chits = None
for chits in chits_list:
return chits
def get_country_url_hits(self, country, hash):
cuhits_list = db.GqlQuery("select * from CountryUrlHits where country = :c and hash = :h", c=country, h=hash)
cuhits = None
for cuhits in cuhits_list:
return cuhits
def update_country_stats(self, obj, country):
if obj is None:
chits = None
else:
chits = db.get(obj.key())
if chits is None:
chits = CountryHits()
chits.country = country
chits.hits = 1
else:
chits.hits += 1
chits.put()
def update_country_url_stats(self, obj, country, url_hash):
if obj is None:
cuhits = None
else:
cuhits = db.get(obj.key())
if cuhits is None:
cuhits = CountryUrlHits()
cuhits.country = country
cuhits.url = url_hash.url
cuhits.hash = url_hash.hash
cuhits.hits = 1
else:
cuhits.hits += 1
cuhits.put()
def get_city_hits(self, city):
chits_list = db.GqlQuery("select * from CityHits where city = :c", c=city)
chits = None
for chits in chits_list:
return chits
def get_city_url_hits(self, city, hash):
cuhits_list = db.GqlQuery("select * from CityUrlHits where city = :c and hash = :h", c=city, h=hash)
cuhits = None
for cuhits in cuhits_list:
return cuhits
def update_city_stats(self, obj, city, country):
if obj is None:
chits = None
else:
chits = db.get(obj.key())
if chits is None:
chits = CityHits()
chits.city = city
chits.country = country
chits.hits = 1
else:
chits.hits += 1
chits.put()
def update_city_url_stats(self, obj, city, country, url_hash):
if obj is None:
cuhits = None
else:
cuhits = db.get(obj.key())
if cuhits is None:
cuhits = CityUrlHits()
cuhits.city = city
cuhits.country = country
cuhits.url = url_hash.url
cuhits.hash = url_hash.hash
cuhits.hits = 1
else:
cuhits.hits += 1
cuhits.put()
def get_region_hits(self, region):
rhits_list = db.GqlQuery("select * from RegionHits where region = :r", r=region)
rhits = None
for rhits in rhits_list:
return rhits
def get_region_url_hits(self, region, hash):
ruhits_list = db.GqlQuery("select * from RegionUrlHits where region = :r and hash = :h", r=region, h=hash)
ruhits = None
for ruhits in ruhits_list:
return ruhits
def update_region_stats(self, obj, region, country):
if obj is None:
rhits = None
else:
rhits = db.get(obj.key())
if rhits is None:
rhits = RegionHits()
rhits.region = region
rhits.country = country
rhits.hits = 1
else:
rhits.hits += 1
rhits.put()
def update_region_url_stats(self, obj, region, country, url_hash):
if obj is None:
ruhits = None
else:
ruhits = db.get(obj.key())
if ruhits is None:
ruhits = RegionUrlHits()
ruhits.region = region
ruhits.country = country
ruhits.url = url_hash.url
ruhits.hash = url_hash.hash
ruhits.hits = 1
else:
ruhits.hits += 1
ruhits.put()
def get_ip_hits(self, ip):
ihits_list = db.GqlQuery("select * from IpHits where ip = :i", i=ip)
ihits = None
for ihits in ihits_list:
return ihits
def get_ip_url_hits(self, ip, hash):
iuhits_list = db.GqlQuery("select * from IpUrlHits where ip = :i and hash = :h", i=ip, h=hash)
iuhits = None
for iuhits in iuhits_list:
return iuhits
def update_ip_stats(self, obj, ip, city, region, country):
if obj is None:
ihits = None
else:
ihits = db.get(obj.key())
if ihits is None:
ihits = IpHits()
ihits.ip = ip
ihits.city = city
ihits.region = region
ihits.country = country
ihits.hits = 1
else:
ihits.hits += 1
ihits.put()
def update_ip_url_stats(self, obj, ip, city, region, country, url_hash):
if obj is None:
iuhits = None
else:
iuhits = db.get(obj.key())
if iuhits is None:
iuhits = IpUrlHits()
iuhits.ip = ip
iuhits.city = city
iuhits.region = region
iuhits.country = country
iuhits.url = url_hash.url
iuhits.hash = url_hash.hash
iuhits.hits = 1
else:
iuhits.hits += 1
iuhits.put()
def update_stats(self, url_hash):
ip = self.request.remote_addr
uagent = self.request.headers['User-Agent']
#small hack for local testing
#if ip == "127.0.0.1":
# ip = self.get_rand_ip();
location = self.get_location(ip)
db.run_in_transaction(self.add_hit, url_hash, ip, uagent, location)
country = location.cn
city = location.ct
region = location.rn
db.run_in_transaction(self.update_country_stats, self.get_country_hits(country), country)
db.run_in_transaction(self.update_country_url_stats, self.get_country_url_hits(country, url_hash.hash), country, url_hash)
db.run_in_transaction(self.update_city_stats, self.get_city_hits(city), city, country)
db.run_in_transaction(self.update_city_url_stats, self.get_city_url_hits(city, url_hash.hash), city, country, url_hash)
db.run_in_transaction(self.update_region_stats, self.get_region_hits(region), region, country)
db.run_in_transaction(self.update_region_url_stats, self.get_region_url_hits(region, url_hash.hash), region, country, url_hash)
db.run_in_transaction(self.update_ip_stats, self.get_ip_hits(ip), ip, city, region, country)
db.run_in_transaction(self.update_ip_url_stats, self.get_ip_url_hits(ip, url_hash.hash), ip, city, region, country, url_hash)
db.run_in_transaction(self.update_total_hits, url_hash)
def update_total_hits(self, url_hash):
url_hash.hits += 1
url_hash.put()
def is_bot(self, request):
user_agent = str(request.headers['User-Agent']).lower()
bot_words = [
"bot", "urllib", "crawler", "topsy", "voyager", "js-kit", "oneriot", "twitturly",
"longurl", "java/", "httpclient", "ruby/", "appengine-google", "ookull", "untiny",
"baidu", "metauri", "ytndemo", "twitspider", "zend_http_client", "twitmatic", "twubs",
"embedly", "dmoz", "yahoo! slurp"
]
is_a_bot = False
for word in bot_words:
if user_agent.find(word) >= 0:
is_a_bot = True
break
return is_a_bot
def get(self):
hash = cgi.escape(self.request.query_string)
url_hash = self.get_url_hash(hash)
if url_hash:
if not self.is_bot(self.request):
self.update_stats(url_hash)
self.redirect(url_hash.url, permanent=True)
else:
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write("Url not found")
return