forked from rajathsh/url-shortener
/
urlshortener.py
65 lines (53 loc) · 1.57 KB
/
urlshortener.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
import random
import json
import redis
import string
import bottle
from bottle import run, route, request, response, post, redirect
from redis import Redis
redis = Redis()
#generate 6 digit alpha-numeric string
def alphanum_code():
code = ''
for i in range(6):
code += random.choice(string.lowercase + string.uppercase + string.digits)
return code
# POST /shorten_url endpoint with json payload
@route('/shorten_url', method='POST')
def shorten_url():
params = request.json
custom_short_code = params['custom_short_code']
long_url = params['long_url']
if (not long_url):
return 'Error: URL not specified'
if (not custom_short_code):
custom_short_code = alphanum_code()
if redis.get(custom_short_code) is not None:
return json.dumps(
{'success' : 'false',
'short_code': custom_short_code
},
indent=4)
else:
redis.set(custom_short_code, long_url)
return json.dumps(
{'success' : 'true',
'short_code': custom_short_code
},
indent=4)
# redirect /{custom_short_code} endpoint to a long url
@route('/<custom_short_code>')
def redirect(custom_short_code):
long_url = redis.get(custom_short_code)
if long_url is not None:
bottle.redirect(long_url, 301)
else:
return json.dumps(
{'success' : 'false',
'short_code': custom_short_code
},
indent=4)
if __name__ == '__main__':
# To run the server, type-in $ python server.py
bottle.debug(True) # display traceback
run(host='localhost', port=8080, reloader=True)