forked from narenaryan/Pyster
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
166 lines (144 loc) · 5.5 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
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
# -*- coding: utf-8 -*-
# Copyright (c) 2016 by Ecreall under licence AGPL terms
# avalaible on http://www.gnu.org/licenses/agpl.html
# Source: https://github.com/narenaryan/Pyster
# licence: AGPL
# author: Amen Souissi
import sqlite3
import string
from flask import Flask, request, render_template, redirect, jsonify
from flask.ext.cors import CORS, cross_origin
from sqlite3 import OperationalError
from urllib.parse import urlparse
#host = 'http://localhost:5000/'
host = 'http://6li.eu/'
BASE = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
BASE.extend(list(string.ascii_lowercase))
BASE.extend(list(string.ascii_uppercase))
BASE_LEN = len(BASE)
#Assuming urls.db is in your app root folder
app = Flask(__name__)
cors = CORS(app, resources={r"/": {"origins": "*"}})
def get_base_next(char):
if char == '':
return False, '0'
char_index = BASE.index(char)
char_index += 1
return (False, BASE[char_index]) if \
char_index < BASE_LEN else (True, '0')
def next_id(id_=None):
new_id = id_
if id_ is None:
new_id = '0'
else:
index = -1
to_inc = new_id[index]
final, next = get_base_next(to_inc)
new_id = new_id[:index] + next
index -= 1
len_id = len(new_id)
while index+6 >= 0 and final:
if index+len_id >= 0:
to_inc = new_id[index]
final, next = get_base_next(to_inc)
new_id = new_id[:index] + next + new_id[index+1:]
else:
to_inc = ''
final, next = get_base_next(to_inc)
new_id = next + new_id[index+1:]
index -= 1
return new_id
def table_check():
create_table = """
CREATE TABLE WEB_URL(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
NUM TEXT NOT NULL UNIQUE,
URL TEXT NOT NULL UNIQUE
);
"""
with sqlite3.connect('var/urls.db') as conn:
cursor = conn.cursor()
try:
cursor.execute(create_table)
except OperationalError:
pass
@app.route('/', methods=['GET', 'POST'])
@cross_origin(origin='localhost',headers=['Content-Type','Authorization'])
def home():
method = request.method
with sqlite3.connect('var/urls.db') as conn:
try:
cursor = conn.cursor()
rows_query = """
SELECT NUM, max(ID) FROM WEB_URL"""
result_cursor = cursor.execute(rows_query)
result_fetch = result_cursor.fetchone()
last_num = result_fetch[0]
number_of_rows = result_fetch[1]
number_of_rows = 0 if number_of_rows is None else number_of_rows
if (method == 'GET' and request.args.get('url', None)) or \
method == 'POST':
original_url = request.args.get('url') if \
method == 'GET' else request.form.get('url')
if original_url:
if urlparse(original_url).scheme == '':
original_url = 'http://' + original_url
exist_row = """
SELECT NUM FROM WEB_URL
WHERE URL='{url}'
""".format(url=original_url)
result_cursor = cursor.execute(exist_row)
result_fetch = result_cursor.fetchone()
if result_fetch:
new_num = result_fetch[0]
else:
new_num = next_id(last_num)
insert_row = """
INSERT INTO WEB_URL (URL, NUM)
VALUES ('{url}', '{num}')
""".format(url=original_url, num=new_num)
cursor.execute(insert_row)
number_of_rows += 1
encoded_string = new_num
if method == 'GET':
return jsonify(**{'short_url': host + encoded_string,
'code': 'SUCCESS',
'original_url': original_url})
else:
return render_template(
'home.html', short_url=host + encoded_string,
number_of_rows=number_of_rows)
return render_template('home.html', number_of_rows=number_of_rows)
except Exception as error:
if method == 'GET':
return jsonify(**{'code': 'ERROR',
'error': str(error),
'original_url': original_url
})
else:
return render_template(
'home.html',
number_of_rows=number_of_rows,
error=True)
@app.route('/<short_url>')
def redirect_short_url(short_url):
decoded_string = short_url
with sqlite3.connect('var/urls.db') as conn:
cursor = conn.cursor()
select_row = """
SELECT URL FROM WEB_URL
WHERE NUM='{num}'
""".format(num=decoded_string)
result_cursor = cursor.execute(select_row)
try:
return redirect(result_cursor.fetchone()[0])
except Exception:
pass
return render_template(
'home.html',
error=True)
if __name__ == '__main__':
# This code checks whether database table is created or not
table_check()
# app.run(debug=True)
app.run(host='0.0.0.0')