Implemented inspection mode by appending + on a short url.

Also a clean, html free resolver is now available by appending ?resolve
on a short url.
This commit is contained in:
sqozz 2016-02-12 18:01:19 +01:00
parent fb1f17a49c
commit 96cce19ca5

View file

@ -8,17 +8,26 @@ app = Flask(__name__)
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
@app.route('/<shortLink>', methods=['GET', 'POST']) @app.route('/<shortLink>', methods=['GET', 'POST'])
def short(shortLink=""): def short(shortLink=""):
print("resolve" in request.args)
if request.method == "GET": if request.method == "GET":
if shortLink: if shortLink:
noauto = shortLink[-1] == "+"
if noauto: shortLink = shortLink[:-1]
conn = sqlite3.connect("data/links.sqlite") conn = sqlite3.connect("data/links.sqlite")
c = conn.cursor() c = conn.cursor()
result = c.execute('SELECT * FROM links WHERE shortLink=?', (shortLink, )).fetchone() result = c.execute('SELECT longLink FROM links WHERE shortLink=?', (shortLink, )).fetchone()
conn.close() conn.close()
if result: if result:
url = result[1] url = result[0]
parsedUrl = urlparse(url) parsedUrl = urlparse(url)
if parsedUrl.scheme == "": if parsedUrl.scheme == "":
url = "http://" + url url = "http://" + url
if "resolve" in request.args:
return url
else:
if noauto:
return "<a href=" + url + ">" + url + "</a>"
else:
return redirect(url, code=301) # Redirect to long URL saved in the database return redirect(url, code=301) # Redirect to long URL saved in the database
else: else:
return render_template("index.html", name=shortLink, message="Enter long URL for "+ request.url_root + shortLink+":", message_type="info") # Custom link page return render_template("index.html", name=shortLink, message="Enter long URL for "+ request.url_root + shortLink+":", message_type="info") # Custom link page