/
app.py
192 lines (143 loc) · 5.18 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import sqlite3
import string
import urllib2
from math import floor
from sqlite3 import OperationalError
from urlparse import urlparse
from BeautifulSoup import BeautifulSoup
from flask import Flask, request, render_template, redirect, flash
host = 'http://localhost:5000/'
app = Flask(__name__)
app.database = 'example.db'
app.secret_key = 'super secret key'
def createTable():
create_table = '''CREATE TABLE IF NOT EXISTS WEB_URL (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url string NOT NULL UNIQUE,
short string,
hits INT,
t TIMESTAMP
DEFAULT CURRENT_TIMESTAMP,
title string
); '''
with sqlite3.connect('example.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 + string.lowercase + string.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 + string.lowercase + string.uppercase
limit = len(num)
res = 0
for i in xrange(limit):
res = b * res + base.find(num[i])
return res
def getTitle(url):
try:
soup = BeautifulSoup(urllib2.urlopen(url))
return soup.title.string
except:
return None
def validateURL(url):
parse = urlparse(url)
if parse.scheme and parse.netloc:
return True
return False
####################################################################################################################################################################
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
original_url = request.form.get('url')
if urlparse(original_url).scheme == '':
original_url = 'http://' + original_url
if original_url == "http://":
return render_template('home.html',
error="Sorry, can't be shorter than this")
with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
x = conn.execute("SELECT count(*) FROM WEB_URL;")
sort_str = x.fetchall()[0][0]
title = getTitle(original_url)
sort = int(sort_str) + 1
encoded_string = host + toBase62(sort)
try:
cursor.execute(
'INSERT INTO WEB_URL (url, short, hits, title) VALUES (?, ?, ?, ?) ',
[original_url, encoded_string, 0, title])
except:
cursor.execute("SELECT short FROM WEB_URL WHERE url = ?",
(original_url, ))
res = cursor.fetchall()[0][0]
return render_template('home.html', short_url=res)
return render_template('home.html', short_url=encoded_string)
return render_template('home.html')
@app.route('/<short_url>')
def redirect_short_url(short_url):
decoded_string = toBase10(short_url)
with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
cursor.execute("UPDATE WEB_URL SET hits=(hits+1) WHERE id = ?",
(decoded_string, ))
cursor.execute("SELECT url FROM WEB_URL WHERE id = ?",
(decoded_string, ))
try:
orignal_url = cursor.fetchall()[0][0]
except:
return render_template('home.html',
error="This short URL doesn't exist")
return redirect(orignal_url)
@app.route('/search', methods=['GET', 'POST'])
def search():
error = None
if request.method == 'POST':
searchStr = request.form.get('search')
with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
try:
cursor.execute("SELECT url FROM WEB_URL WHERE title LIKE ? ",
('%' + searchStr + '%', ))
except:
pass
url = [i[0] for i in cursor.fetchall()]
if not url and searchStr:
flash("Nope, nothing to see here :(")
if searchStr:
return render_template('search.html', url=url)
flash("The field can't be empty :\\")
return render_template('search.html', error=error)
@app.route('/hits', methods=['GET', 'POST'])
def hits():
if request.method == 'POST':
searchStr = request.form.get('search')
if searchStr[:4] != 'http':
searchStr = 'http://' + searchStr
with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
try:
cursor.execute("SELECT hits FROM WEB_URL WHERE short = ? ",
(searchStr, ))
except:
pass
try:
hits = cursor.fetchall()[0][0]
print hits
except:
return render_template('hits.html', error="URL not available")
return render_template('hits.html', url=hits)
return render_template('hits.html')
if __name__ == '__main__':
createTable()
app.run(debug=True)