/
short.py
82 lines (59 loc) · 1.73 KB
/
short.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
# encoding: utf-8
import base62
import urllib
import unicodedata
from google.appengine.ext import db
from google.appengine.api import memcache
class Url(db.Model):
url = db.StringProperty()
class UrlHits(db.Model):
url_ref = db.ReferenceProperty(Url)
#hits = db.IntegerProperty(default = 0)
date = db.DateTimeProperty(auto_now_add=True)
referer = db.StringProperty()
ip_address = db.StringProperty()
user_agent = db.StringProperty()
def set_url(url_string, request):
encoded = urllib.quote_plus(urllib.unquote(url_string))
url = Url.all().filter('url =', encoded).get()
if url is not None:
new_id_num = url.key().id()
else:
handmade_key = db.Key.from_path('Url', 1)
new_ids = db.allocate_ids(handmade_key, 1)
new_id_num = int(new_ids[0])
new_key = db.Key.from_path('Url', new_id_num)
url = Url(key = new_key)
url.url = encoded
url.put()
__hit(url, request)
code = base62.encode(new_id_num)
if memcache.get('cacheurls:%s' % code) is None:
memcache.add('cacheurls:%s' % code, url, 60*60*12)
return code
def __hit(url, request):
hit = UrlHits(url_ref = url)
if request is not None:
hit.referer = request.referer
hit.ip_address = request.remote_addr
hit.user_agent = request.headers['user-agent']
hit.put()
def get_url(url_code, request):
url = memcache.get('cacheurls:%s' % url_code)
if url is None:
url_id = base62.decode(url_code)
url = Url.get_by_id(url_id)
if url is None:
return None
'''
hit = UrlHits.all().filter('url_ref =', url.key()).get()
if hit is not None:
hit.hits += 1
hit.put()
'''
__hit(url, request)
try:
url_str = unicodedata.normalize('NFKD', urllib.unquote(url.url)).encode('ascii','ignore')
except Exception:
url_str = urllib.unquote(url.url)
return url_str