/
ulkng.py
142 lines (115 loc) · 3.55 KB
/
ulkng.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
# -*- coding: utf8 -*-
import hashlib
from redis import ConnectionPool
from redis.client import Redis
from utils import render_template, check_token, notfound
from settings import REDIS_HOST, REDIS_PORT, REDIS_DB, URL_HASH_NAME, COUNT_HASH_NAME, LOG_HASH_NAME, TOKEN_HASH_NAME, DEBUG
import web
from web import form
web.config.debug = DEBUG
redis_pool = ConnectionPool(
host=REDIS_HOST,
port=REDIS_PORT,
db=REDIS_DB
)
urls = (
'/', 'Index',
'/favicon.ico', 'NotFound',
'/add/', 'Add',
'/all/', 'ViewAll',
'/(.*)/\+', 'ViewDetails',
'/(.*)/', 'Redirect',
'/(.*)', 'Redirect',
)
url_form = form.Form(
form.Textbox("url"),
)
class Index:
def GET(self):
r = Redis(connection_pool=redis_pool)
count = 0
for key in r.hgetall(URL_HASH_NAME):
count += 1
return render_template('index.html', count=count, section_class='index', user_id=None)
class ViewAll:
def GET(self):
r = Redis(connection_pool=redis_pool)
user_id = check_token(r)
all_keys = r.hgetall(URL_HASH_NAME)
url_list = []
for key in all_keys:
url_list.append(
(
all_keys[key].replace('http://', '').replace('https://', ''),
key, r.hget(COUNT_HASH_NAME, key) or 0,
r.hget(LOG_HASH_NAME, key) or ''
),
)
return render_template('list.html', user_id=user_id, list=url_list, is_all=True)
class Add:
def GET(self):
r = Redis(connection_pool=redis_pool)
user_id = check_token(r)
form = url_form()
return render_template('add.html',
form=form,
user_id=user_id,
is_add=True,
)
def POST(self):
r = Redis(connection_pool=redis_pool)
user_id = check_token(r)
form = url_form()
if not form.validates():
return render_template('add.html',
form=form,
user_id=user_id,
is_add=True,
)
url = form['url'].value
token = hashlib.sha1()
token.update(url.replace('http(s)?://', '').strip())
key = token.hexdigest()[:6]
print key + url
if not r.hget(URL_HASH_NAME, key):
r.hset(URL_HASH_NAME, key, url)
r.hset(COUNT_HASH_NAME, key, 0)
if user_id[1]:
r.hset(LOG_HASH_NAME, key, r.hget(TOKEN_HASH_NAME, web.input().token))
if user_id[1] == '':
raise web.seeother('/%s/+' % key)
else:
raise web.seeother('/%s/+?token=%s' % (key, user_id[1]))
class Redirect:
def GET(self, key):
r = Redis(connection_pool=redis_pool)
url = r.hget(URL_HASH_NAME, key)
if url:
r.hincrby(COUNT_HASH_NAME, key)
raise web.seeother(url)
else:
raise notfound()
class ViewDetails:
def GET(self, key):
r = Redis(connection_pool=redis_pool)
user_id = check_token(r)
url = r.hget(URL_HASH_NAME, key)
if url:
count = r.hget(COUNT_HASH_NAME, key)
return render_template('details.html',
user_id=user_id,
key=key,
url=url,
count=count,
section_class='index'
)
else:
raise notfound()
class NotFound:
def GET(self):
raise notfound()
app = web.application(urls, globals())
application = app.wsgifunc()
if __name__ == "__main__":
print 'DEBUG=%s' % DEBUG
app.run()