From 551d9e1ccec2811b261d83201c790aff04d1d81d Mon Sep 17 00:00:00 2001 From: sqozz Date: Wed, 10 Feb 2016 07:57:22 +0100 Subject: [PATCH] Initial partial working commit --- .gitignore | 2 ++ shortener.py | 52 ++++++++++++++++++++++++++++++++++++++++ static/css/index.css | 56 ++++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 21 +++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 .gitignore create mode 100755 shortener.py create mode 100644 static/css/index.css create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc989f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +links.sqlite diff --git a/shortener.py b/shortener.py new file mode 100755 index 0000000..b39891c --- /dev/null +++ b/shortener.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +from flask import Flask, render_template, url_for, request, redirect +import sqlite3, random, string, time, hashlib, base64 +app = Flask(__name__) + +#@app.route('/') +#def root(): +# return render_template("index.html", name=shortLink, message="Enter long URL for "+ request.url_root + shortLink+":", message_type="info") + +@app.route('/', methods=['GET', 'POST']) +@app.route('/', methods=['GET', 'POST']) +def short(shortLink=""): + if request.method == "GET": + conn = sqlite3.connect("links.sqlite") + c = conn.cursor() + result = c.execute('SELECT * FROM links WHERE shortLink=?', (shortLink, )).fetchone() + if result: + return redirect(result[1], code=302) # Redirect to long URL saved in the database + else: + return render_template("index.html", name=shortLink, message="Enter long URL for "+ request.url_root + shortLink+":", message_type="info") # Does the user wish to create a personel short link? + elif request.method == "POST": # Someone submitted a new link to short + conn = sqlite3.connect("links.sqlite") + c = conn.cursor() + wishId = request.form["wishId"] + longUrl = request.form["url"] + if not wishId: + hashUrl = hashlib.sha256(longUrl.encode()).hexdigest() + base64Url = base64.b64encode(hashUrl.encode()).decode() + databaseId = base64Url[:4] + else: + databaseId = wishId + + c.execute('INSERT INTO links VALUES (?, ?, ?, ?, ?)', (databaseId, longUrl, int(time.time()), request.remote_addr, "default" )) + conn.commit() + conn.close() + return redirect(longUrl, code=302) # TODO: Give the user a nice site where he can see his short URL + +def generateId(): + + return ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(4)) + +def initDB(): + conn = sqlite3.connect("links.sqlite") + c = conn.cursor() + c.execute('''CREATE TABLE IF NOT EXISTS links (shortLink UNIQUE NOT NULL, longLink, timestamp, ip, redirectMethod);''') + conn.commit() + conn.close() + print("DB init") + +if __name__ == '__main__': + initDB() + app.run(debug=True) diff --git a/static/css/index.css b/static/css/index.css new file mode 100644 index 0000000..07bf5f5 --- /dev/null +++ b/static/css/index.css @@ -0,0 +1,56 @@ +html, body { + height: 100%; + width: 100%; + margin: 0px; +} + +body { + display: flex; + justify-content: center; + align-items: center; +} + +#main { + width: 40%; +} + +#url_input { + border: 1px solid rgb(200, 200, 200); + height: 35px; + margin: 0px; + padding-left: 10px; + flex: 1; +} + +#short_button { + border: 1px solid rgb(200, 200, 200); + height: 35px; + margin: 0px; + margin-left: 10px; +} + +#short_form { + font-family: "monospace"; + font-size: 30px; + display: flex; + justify-content: center; + align-items: center; +} + +#url_text { + margin-right: 10px; +} + +#message { + text-align: center; + margin-bottom: 5px; + word-wrap: break-word; +} + +.error { + color: red; +} + +.info { + color: #848400; +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..8e9acff --- /dev/null +++ b/templates/index.html @@ -0,0 +1,21 @@ + + + + + geekify.de + + + +
+
+ {{message}} +
+
+ URL: + + + +
+
+ +