From 06cf1470050a0d8242167f8b1aa5c3858476de78 Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Thu, 1 Oct 2020 16:10:48 -0700 Subject: [PATCH] Code example, first pass. --- .gitignore | 5 +++++ Pipfile | 11 +++++++++++ Pipfile.lock | 20 +++++++++++++++++++ README.md | 11 +++++++++++ templates/error.html | 10 ++++++++++ tinyurl.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+) create mode 100644 .gitignore create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 README.md create mode 100644 templates/error.html create mode 100644 tinyurl.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a857ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.#* +*~ +*# +*.aux +*.pyc diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..5d44a48 --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.python.org/simple" +verify_ssl = true +name = "pypi" + +[packages] + +[dev-packages] + +[requires] +python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..4cee413 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,20 @@ +{ + "_meta": { + "hash": { + "sha256": "e2a8a78582d100dc86a0694f5ad982ca341a6b861fd871c6306733562f9e16cc" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.8" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.python.org/simple", + "verify_ssl": true + } + ] + }, + "default": {}, + "develop": {} +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..27ccb76 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# TinyURL + +## Getting started + +``` +git checkout +cd tinyurl +pipenv shell +python ./tinyurl.py +``` + diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..f02b4eb --- /dev/null +++ b/templates/error.html @@ -0,0 +1,10 @@ + + + + + Error {{code}} + + +

Error {{code}}

+ + diff --git a/tinyurl.py b/tinyurl.py new file mode 100644 index 0000000..6fe0b26 --- /dev/null +++ b/tinyurl.py @@ -0,0 +1,47 @@ +from flask import Flask, json, redirect, request, render_template, jsonify +import short_url + +app = Flask(__name__) + +found = {} +database = {} + + +@app.route('/', methods=['POST']) +def post_tinyurl(): + body = request.get_json(silent=True, force=True) + if body is None or 'url' not in body: + return (render_template("error.html", code=400), 400) + + url = body['url'] + if url in found: + return jsonify({ + 'url': url, + 'short_url_code': '/' + found[url] + }) + + counter = len(database) + 1 + newurl = short_url.encode_url(counter) + database[newurl] = url + found[url] = newurl + + return jsonify({ + 'url': url, + 'short_url_code': '/' + newurl + }) + + +@app.route('/', methods=["GET"]) +def get_tinyurl(url): + if url not in database: + return render_template('error.html', code=404), 404 + return redirect(database[url], 301) + + +@ app.errorhandler(404) +def not_found(error): + return render_template('error.html', code=404), 404 + + +if __name__ == '__main__': + app.run(debug=True)