-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
33 lines (33 loc) · 1.2 KB
/
main.py
File metadata and controls
33 lines (33 loc) · 1.2 KB
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
from flask import Flask, redirect, request, render_template, url_for
app = Flask(__name__)
import random
import sqlite3
sql = sqlite3.connect('urls.sqlite', check_same_thread=False)
db = sql.cursor()
@app.route('/post', methods=["POST"])
def POSTURL():
if request.method == "POST":
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
random_id = ''.join([random.choice(chars) for i in range(9)])
db.execute("INSERT INTO urls (id, url) VALUES (?, ?)", (random_id, request.form['url']))
sql.commit()
return redirect(url_for('index', uri_id=random_id))
@app.route('/', methods=["GET"])
def index():
uri_id = request.args.get('uri_id')
db.execute('''CREATE TABLE IF NOT EXISTS urls (id, url)''')
data =db.execute("SELECT * FROM urls WHERE id = ?", (uri_id,)).fetchone()
sql.commit()
if request.method == "GET":
if uri_id:
return render_template('index.html', data=data)
return render_template('index.html', data=data)
@app.route('/s/<uri_id>')
def GETURL(uri_id):
data = db.execute("SELECT * FROM urls WHERE id = ?", (uri_id,)).fetchone()
sql.commit()
if data != None:
return redirect(data[1])
return redirect("/")
if __name__ == '__main__':
app.run(debug=True)