-
Notifications
You must be signed in to change notification settings - Fork 0
/
shorten.py
126 lines (75 loc) · 2.81 KB
/
shorten.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
#!/usr/bin/env python
#
# Copyright 2011 Aamir Khan
#
#
import redis
import json
import tornado.web
import tornado.ioloop
import urllib
import urlparse
import datetime
import re
import tornado.httpserver
import tornado.options
import os.path
from tornado.options import define, options
define("config")
define("port", type=int)
#redisServer = redis.StrictRedis(host='localhost',port=6379 ,db=0)
class ShortenApplication(tornado.web.Application):
def __init__(self):
#self.redisServer = redis.StrictRedis(host='localhost',port=6379 ,db=0)
settings = {
}
tornado.web.Application.__init__(self, [
tornado.web.url(r"/s/([^/]+)", Shorten, name="Index"),
tornado.web.url(r"/([a-zA-Z0-9]+)", Expand, name="Index"),
tornado.web.url(r"/", MainHandler ,name = "Index"),
], **settings)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.redisServer = redis.StrictRedis(host='localhost',port=6379 ,db=0)
self.write("Index Page..To be Updated with content")
class Shorten(tornado.web.RequestHandler):
def get(self,id):
self.redisServer = redis.StrictRedis(host='localhost',port=6379 ,db=0)
self.write(self.shorten(id))
def shorten(self, long_url=None):
url_hash = '%x' % self.redisServer.incr('next.url.id') #hex value of counter
#next we store the long_url against this hashed key
self.redisServer.set('url:%s:id' % url_hash, long_url)
#push this short url in global list of URLs
self.redisServer.lpush('global:urls',url_hash)
return long_url + url_hash
class Expand(tornado.web.RequestHandler):
def get(self, id):
self.redisServer = redis.StrictRedis(host='localhost',port=6379 ,db=0)
long_url = self.expand(id)
self.write("Redirecting to "+long_url)
self.redirect(long_url)
def expand(self, shorten_url=None):
long_url = self.redisServer.get('url:%s:id' % shorten_url)
return long_url
#class Visit(tornado.web.RequestHandler):
# def get(self, id):
# self.redisServer = redis.StrictRedis(host='localhost',port=6379 ,db=0)
# self.write(self.shorten(id))
# def visit(self, shorten_url=None, ip_addr=None,agent=None,referrer=None):
#create an object of Visitor type
# visitor = Visitor(ip_addr, agent, referrer)
#push the visitor object in visitor list of this shorten URL in json encoded form
# self.redisServer.push('visitors:%s' % shorten_url, json.dumps(visitor))
# return self.redisServer.incr('clicks:%s:url' % shorten_url)
def main():
tornado.options.parse_command_line()
if options.config:
tornado.options.parse_config_file(options.config)
else:
path = os.path.join(os.path.dirname(__file__), "settings.py")
tornado.options.parse_config_file(path)
ShortenApplication().listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()