-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
116 lines (92 loc) · 2.9 KB
/
main.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
from flask import Flask, request, render_template, redirect
from math import floor
from sqlite3 import OperationalError
import string
import sqlite3
try:
from urllib.parse import urlparse # Python 3
str_encode = str.encode
except ImportError:
from urlparse import urlparse # Python 2
str_encode = str
try:
from string import ascii_lowercase
from string import ascii_uppercase
except ImportError:
from string import lowercase as ascii_lowercase
from string import uppercase as ascii_uppercase
import base64
# Assuming urls.db is in your app root folder
app = Flask(__name__)
host = 'http://localhost:5000/'
def table_check():
create_table = """
CREATE TABLE WEB_URL(
ID INT PRIMARY KEY AUTOINCREMENT,
URL TEXT NOT NULL
);
"""
with sqlite3.connect('urls.db') as conn:
cursor = conn.cursor()
try:
cursor.execute(create_table)
except OperationalError:
pass
def toBase62(num, b=62):
if b <= 0 or b > 62:
return 0
base = string.digits + ascii_lowercase + ascii_uppercase
r = num % b
res = base[r]
q = floor(num / b)
while q:
r = q % b
q = floor(q / b)
res = base[int(r)] + res
return res
def toBase10(num, b=62):
base = string.digits + ascii_lowercase + ascii_uppercase
limit = len(num)
res = 0
for i in range(limit):
res = b * res + base.find(num[i])
return res
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
original_url = str_encode(request.form.get('url'))
if urlparse(original_url).scheme == '':
url = 'http://' + original_url
else:
url = original_url
with sqlite3.connect('urls.db') as conn:
cursor = conn.cursor()
res = cursor.execute(
'INSERT INTO WEB_URL (URL) VALUES (?)',
[base64.urlsafe_b64encode(url)]
)
conn.commit()
encoded_string = toBase62(res.lastrowid)
return render_template('home.html', short_url=host + encoded_string)
return render_template('home.html')
@app.route('/<short_url>')
def redirect_short_url(short_url):
decoded = toBase10(short_url)
url = host # fallback if no URL is found
try:
with sqlite3.connect('urls.db') as conn:
cursor = conn.cursor()
res = cursor.execute('SELECT URL FROM WEB_URL WHERE ID=?', [decoded])
try:
short = res.fetchone()
if short is not None:
url = base64.urlsafe_b64decode(short[0])
except Exception as e:
print(e)
return redirect(url)
except OverflowError as e:
print(str(e))
if __name__ == '__main__':
# This code checks whether database table is created or not
table_check()
app.run(debug=True)