blck

ephemeral pastebin/url shortener
git clone https://git.parazyd.org/blck
Log | Files | Refs | README | LICENSE

commit f0df513684d99c3acb8c070e8fbb45b9bf7a402c
parent f2b0200b30168f05341eec71975e0bfbb5a07424
Author: parazyd <parazyd@dyne.org>
Date:   Tue,  3 Oct 2017 11:05:44 +0200

pylint fixes

Diffstat:
MREADME.md | 2+-
Mblck.py | 50++++++++++++++++++++++++++------------------------
2 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/README.md b/README.md @@ -12,7 +12,7 @@ get `python-flask` and execute `blck.py`. by default it starts on `localhost:5000`, but you can configure it at the bottom of the script. to choose whether the app runs as a pastebin or url shortener, just -change the `pastebin` variable inside `blck.py` +change the `PASTEBIN` variable inside `blck.py` nginx diff --git a/blck.py b/blck.py @@ -1,46 +1,50 @@ #!/usr/bin/env python3 # copyleft (c) 2017 - parazyd # see LICENSE file for details +""" +main blck module +""" -import flask import random import os import string +import flask -app = flask.Flask(__name__) - -pastebin = False +PASTEBIN = False +APP = flask.Flask(__name__) -@app.route("/", methods=['GET', 'POST']) +@APP.route("/", methods=['GET', 'POST']) def main(): + """ main routine """ try: - url = flask.request.form['url'] - return s(url) + return short(flask.request.form['url']) except: - return flask.render_template("index.html", pastebin=pastebin) + return flask.render_template("index.html", pastebin=PASTEBIN) -@app.route("/<urlshort>") -def u(urlshort): +@APP.route("/<urlshort>") +def urlget(urlshort): + """ returns a paste if it exists """ try: - with open('uris/' + urlshort, 'r') as f: - realurl = f.readline() + with open('uris/' + urlshort, 'r') as paste: + realurl = paste.readline() os.remove('uris/' + urlshort) - except: + except FileNotFoundError: return "could not find paste\n" cliagents = ['curl', 'Wget'] if flask.request.headers.get('User-Agent').split('/')[0] not in cliagents \ - and not pastebin: + and not PASTEBIN: return flask.redirect(realurl.rstrip('\n'), code=301) - else: - return realurl + return realurl -def s(url): - if not pastebin: + +def short(url): + """ pasting logic """ + if not PASTEBIN: # taken from django import re regex = re.compile( @@ -58,11 +62,8 @@ def s(url): return "invalid paste\n" urlshort = genid() - try: - with open('uris/' + urlshort, 'w') as f: - f.write(url + '\n') - except: - return "could not save paste\n" + with open('uris/' + urlshort, 'w') as paste: + paste.write(url + '\n') if flask.request.headers.get('X-Forwarded-Proto') == 'https': return flask.request.url_root.replace('http://', 'https://') \ @@ -72,8 +73,9 @@ def s(url): def genid(size=4, chars=string.ascii_uppercase + string.ascii_lowercase): + """ returns a random id for a paste """ return ''.join(random.choice(chars) for i in range(size)) if __name__ == '__main__': - app.run(host="127.0.0.1", port=5000) + APP.run(host="127.0.0.1", port=5000)