/
app.py
78 lines (56 loc) · 2 KB
/
app.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
import sys
import string
import random
from urllib.parse import urlparse
from flask import Flask, request, jsonify, redirect
from werkzeug.routing import Rule
from database import db_session, init_db, init_engine
from models import Url
app = Flask(__name__)
def short_url_exists(url):
if not url:
return True
return Url.query.filter(Url.short_url == url).count() != 0
def get_random_short_url():
"""Generates a random string of 7 ascii letters and digits
Can provide in the order or 10^12 unique strings
"""
pool = string.ascii_letters + string.digits
return ''.join(random.choice(pool) for _ in range(7))
def get_new_short_url():
"""Generate random urls until a new one is generated"""
url = None
while short_url_exists(url):
url = get_random_short_url()
return url
@app.route('/', methods=['POST'])
def shorthen():
req_url = request.get_json(force=True)['url']
scheme = urlparse(req_url).scheme
if not scheme: # check if url contains http://
req_url = 'http://' + req_url
url = Url.query.filter(Url.url == req_url).first()
if not url:
short_url = get_new_short_url()
db_session.add(Url(req_url, short_url))
db_session.commit()
else:
short_url = url.short_url # I'm fucking sorry
return jsonify(url=req_url, short_url=app.config['API_URL'] + short_url)
@app.route('/<short_url>', methods=['GET'])
def get_shorten_url(short_url):
short_url = Url.query.filter(Url.short_url == short_url).first()
if not short_url:
return 'The requested url does not exist.', 404
else:
url = short_url.url # I'm sorry again
return redirect(url, code=302)
if __name__ == "__main__":
app.config.from_pyfile('config/default_config.py')
if len(sys.argv) == 2:
conf = sys.argv[1]
print('Loading additional config %s...', conf)
app.config.from_pyfile('config/' + conf + '_config.py')
init_engine(app.config['DATABASE_URI'])
init_db()
app.run()