Initial partial working commit

This commit is contained in:
sqozz 2016-02-10 07:57:22 +01:00
commit 551d9e1cce
4 changed files with 131 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.swp
links.sqlite

52
shortener.py Executable file
View File

@ -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('/<shortLink>', 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)

56
static/css/index.css Normal file
View File

@ -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;
}

21
templates/index.html Normal file
View File

@ -0,0 +1,21 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>geekify.de</title>
<link rel="stylesheet" href="static/css/index.css">
</head>
<body>
<div id="main">
<div id="message" class="{{message_type}}">
{{message}}
</div>
<form action="/" method="post" id="short_form">
<span id="url_text">URL:</span>
<input type="input" name="url" id="url_input">
<input type="hidden" name="wishId" value="{{name}}">
<input type="submit" value="Short!" id="short_button">
</form>
</div>
</body>
</html>