forked from chu888chu888/Python-SAE-tao.bb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taobb.py
141 lines (109 loc) · 3.4 KB
/
taobb.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
#!/usr/bin/env python
#coding: utf-8
# vim: ai ts=4 sts=4 et sw=4 ft=python
import sys
import sae.const
from bottle import route, run, static_file, request ,abort, redirect, response, error
from base62 import base62_encode, base62_decode
from hashlib import md5
from url_normalize import url_normalize
from SAEKVDBPlugin import SAEKVDBPlugin
from qrcode import make as makeqrcode
from StringIO import StringIO
from bottle_mysql import Plugin as MySQLPlugin
from urlparse import urlsplit
from sqlparams import SQLParams
from untinyurl import untiny
BLACKLIST = ()
try:
import blacklist
BLACKLIST = tuple(blacklist.BLACKLIST)
except:
pass
#MAX = 62 ** 5
MAX = 916132832
kv_plugin = SAEKVDBPlugin()
mysql_plugin = MySQLPlugin(dbuser = sae.const.MYSQL_USER , dbpass = sae.const.MYSQL_PASS, dbname = sae.const.MYSQL_DB, dbhost = sae.const.MYSQL_HOST , dbport = int(sae.const.MYSQL_PORT))
def hashto62(url):
m = md5()
m.update(url)
return int(m.hexdigest(), 16) % MAX
@error(404)
@route('/')
def index(error = None):
return static_file('taobb.html', root='.')
@route('/favicon.ico')
def notfound():
redirect('http://www.taobao.com/favicon.ico', 302)
@route('/<key>', apply=[kv_plugin])
def url(key, kv):
#if request.get_header('Host' , 'tao.bb') != 'tao.bb':
#abort(404, "NOT FOUND")
key = key.strip('/')
if len(request.query) == 0 and len(key) == 5:
url = kv.get(key)
if url:
redirect(url)
abort(404, "NOT FOUND")
@route('/<key>/real', apply=[kv_plugin])
def qrcode(key, kv):
if len(request.query) == 0 and len(key) == 5:
url = kv.get(key)
if url:
return url + "\n"
abort(404, "NOT FOUND")
@route('/<key>/qrcode', apply=[kv_plugin])
@route('/<key>/qrcode.png', apply=[kv_plugin])
def qrcode(key, kv):
if len(request.query) == 0 and len(key) == 5:
url = kv.get(key)
if url:
response.content_type = 'image/png'
img = makeqrcode(url)
output = StringIO()
img.save(output,'PNG')
contents = output.getvalue()
output.close()
return contents
abort(404, "NOT FOUND")
@route('/d/save', method='POST', apply=[kv_plugin, mysql_plugin])
def save(kv, db):
url = request.forms['url']
if not url:
return {'err' : '请输入URL'}
url = url_normalize(url)
if not url:
return {'err' : '请输入有效的 URL'}
surl = urlsplit(url)
if surl.netloc.endswith(BLACKLIST):
return {'err' : '不支持的域名'}
code = hashto62(url)
key = base62_encode(code)
sql = """
INSERT INTO `taobb_urls` (`id`, `key`, `url`, `gmt_create`, `gmt_modified`) VALUES (:id, :key, :url, now(), now())
ON DUPLICATE KEY UPDATE `gmt_modified` = now()
"""
sp = SQLParams('named', 'format')
sql, params = sp.format(sql, {
'id' : code,
'key' : key,
'url' : url,
})
if db.execute(sql, params) and kv.set(key, url):
return {'key':key , 'err' : None}
else:
return {'err': '内部错误'}
@route('/d/long', method='POST')
def longurl():
wanted = request.forms['url']
longurl = None
if wanted and len(wanted) < 25:
if not wanted.startswith('http://'):
wanted = 'http://' + wanted
longurl = untiny(wanted)
return { 'wanted':wanted, 'long': longurl}
if __name__ == '__main__':
debug = False
if len(sys.argv) > 0:
debug = True
run(host='localhost', port=8008, debug=debug)