blck

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

commit 4943c447132c4b60fa3145a6c361979ad0c2b5b7
parent 39625041fc4b648ee3df086c88c8f5fa88215ebf
Author: parazyd <parazyd@dyne.org>
Date:   Mon, 20 Mar 2017 04:12:56 +0100

contain all in /

Diffstat:
MREADME.md | 6+++---
Mblck.py | 45++++++++++++++++-----------------------------
Mtemplates/index.html | 2+-
3 files changed, 20 insertions(+), 33 deletions(-)

diff --git a/README.md b/README.md @@ -7,8 +7,8 @@ a one-click url shortener. installation ------------ -get `python-flask` and execute `blck.py`. by default it starts on port -5000, but you can use the `-p` switch to specify a port. +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. usage @@ -17,7 +17,7 @@ usage either use the website, or curl: ``` -curl -F 'url=http://blck.cf' http://blck.cf/s +curl -F 'url=https://github.com/parazyd/blck.cf' http://blck.cf ``` how does it work? diff --git a/blck.py b/blck.py @@ -7,21 +7,22 @@ import random import re import os import string -import sys app = flask.Flask(__name__) -@app.route("/") +@app.route("/", methods=['GET', 'POST']) def main(): - return flask.render_template("index.html") - + try: + url = flask.request.form['url'] + return s(url) + except: + return flask.render_template("index.html") -@app.route("/u/<urlshort>") +@app.route("/<urlshort>") def u(urlshort): try: - f = open('uris/' + urlshort, 'r') - realurl = f.readline() - f.close() + with open('uris/' + urlshort, 'r') as f: + realurl = f.readline() os.remove('uris/' + urlshort) except: return "could not find url\n" @@ -31,16 +32,8 @@ def u(urlshort): else: return realurl -@app.route("/s", methods=['POST']) -def s(): - url = flask.request.form['url'] - - if not url: - return "invalid data\n" - - if len(url) > 1024: - return "url too long\n" +def s(url): ## taken from django regex = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// @@ -50,28 +43,22 @@ def s(): r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) - if not regex.match(url): + if not url or len(url) > 1024 or not regex.match(url): return "invalid url\n" urlshort = genid() try: - f = open('uris/' + urlshort, 'w') - f.write(url + '\n') - f.close() + with open('uris/' + urlshort, 'w') as f: + f.write(url + '\n') except: return "could not save url\n" - return flask.request.url_root + 'u/' + urlshort + '\n' + return flask.request.url_root + urlshort + '\n' def genid(size=4, chars=string.ascii_uppercase + string.ascii_lowercase): return ''.join(random.choice(chars) for i in range(size)) -if __name__ == "__main__": - try: - if sys.argv[1] == '-p': - _port = sys.argv2 - except: - _port = 5000 - app.run(host="127.0.0.1", port=int(_port)) +if __name__ == "__main__": + app.run(host="127.0.0.1", port=5000) diff --git a/templates/index.html b/templates/index.html @@ -10,7 +10,7 @@ <h1>blck.cf</h1> <p>Create a one-click expiring link</p> <div class="form"> - <form method="post" action="/s"> + <form method="post" action="/"> <input type="url" name="url" id="url" class="inputbox" placeholder="http://blck.cf" required autofocus></input> <input type="submit" class="button"></input> </form>