-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetw.py
53 lines (43 loc) · 1.53 KB
/
etw.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
from flask import Flask, render_template, request, redirect, abort
from hashids import Hashids
import sqlite3
app = Flask(__name__)
hashids = Hashids(salt="why so salty")
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# Retrieve redirect destination
dest = request.form.get("destination")
# Connect to databse and create cursor
conn = sqlite3.connect('test.db')
db_cursor = conn.cursor()
# Insert new link into databse, and commit change
db_cursor.execute("INSERT INTO links (dest) VALUES (?)", (dest,))
conn.commit()
# Build hash id from link's DB id number
link_id = db_cursor.lastrowid
link_hash = hashids.encode(link_id)
# Add hash id to link and update
db_cursor.execute("UPDATE links SET short=? WHERE id=?",(link_hash,link_id))
conn.commit()
conn.close()
short_link = "/" + link_hash
return render_template('index.html', short_link=short_link)
else:
return render_template('index.html')
@app.route('/<link_hash>')
def redir(link_hash):
# Decode the shortlink and find the item in the db!
try:
link_id = hashids.decode(link_hash)[0]
print("Incoming hash:",link_hash,"has been decoded to id:",link_id)
conn = sqlite3.connect('test.db')
db_cursor = conn.cursor()
dest_link = db_cursor.execute("SELECT * FROM links WHERE id=?",(link_id,)).fetchone()[1]
print("Short link found! Redirecting to: ", dest_link)
return redirect(dest_link, code=308,)
except Exception:
abort(404)