-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·100 lines (88 loc) · 3.35 KB
/
code.py
File metadata and controls
executable file
·100 lines (88 loc) · 3.35 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
import simplejson
render = web.template.render('/home/production/snipper/templates/')
urls = (
'/', 'index',
'/geturl/', 'geturl',
'/geturl', 'geturl',
'/([a-zA-Z0-9]+)/', 'redirect',
'/([a-zA-Z0-9]+)', 'redirect',
'/([a-zA-Z0-9]+)/stats/', 'stats',
'/([a-zA-Z0-9]+)/stats', 'stats',
)
APP_URL = 'http://snipper.in/'
app = web.application(urls, locals())
db = web.database(dbn='postgres', user='production', pw='', db='snipper')
def baseN(num,b):
return ((num == 0) and "0" ) or ( baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])
def processurl(url):
if url.lower().startswith("http://") or url.lower().startswith("https://") or url.lower().startswith("ftp://"):
return url
return "http://"+url
class index:
def GET(self):
return render.index('0',"")
def POST(self):
params = web.input(url=None)
if not params.url:
web.ctx.status = '404 Not Found'
return render.notfound("")
url = processurl(params.url)
data = db.select('links',dict(link=url),where = 'link=$link')
for d in data:
stuff = d
if len(data)!=0:
return render.index('1',APP_URL+stuff.hash)
in_id = db.insert('links',link=url)
hash = baseN(int(in_id),36)
db.update('links',where="id=%d" % (in_id,),hash=hash)
return render.index('1',APP_URL+hash)
class geturl:
def GET(self):
params = web.input(url=None,type=None)
if not params.url:
web.ctx.status = '404 Not Found'
return render.notfound("")
if params.type=='json':
web.header('Content-Type', 'application/json')
url = processurl(params.url)
data = db.select('links',dict(link=url),where = 'link=$link')
for d in data:
stuff=d
if len(data)!=0:
return APP_URL + stuff.hash
in_id = db.insert('links',link=url)
hash = baseN(int(in_id),36)
db.update('links',where="id=%d" % (in_id,),hash=hash)
if params.type=='json':
return simplejson.dumps(APP_URL + hash)
return APP_URL + hash
class redirect:
def GET(self,hash):
data = db.select('links',dict(hash=hash),where = 'hash=$hash')
for d in data:
stuff = d
if len(data)==0:
web.ctx.status = '404 Not Found'
return render.notfound("")
referer = web.ctx.env.get('HTTP_REFERER', '')
ip = web.ctx.env.get('REMOTE_ADDR','')
browser = web.ctx.env.get('HTTP_USER_AGENT','')
db.insert('logs',link=d.id,ip=ip,browser=browser,redirect=referer)
raise web.seeother(d.link)
class stats:
def GET(self,hash):
data = db.select('links',dict(hash=hash),where = 'hash=$hash')
if len(data)==0:
web.ctx.status = '404 Not Found'
return render.notfound("")
data = db.query("select count(*) as count from logs,links where logs.link=links.id and links.hash='%s'" % (hash,))
for d in data:
stuff = d
last5 = db.query("select logs.ip as ip,logs.created_at as created_at from logs,links where logs.link=links.id and links.hash='%s' limit 5" % (hash,))
return render.stats(d.count,last5)
if __name__ == "__main__":
app.run()
application = web.application(urls, globals()).wsgifunc()