commit 09ca9b421c1354540a76f92d13702662f734298c Author: Tanner Collin Date: Wed Aug 7 21:00:54 2019 +0000 Move t0pic to its own git repo diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..10bad72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,108 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# Editor +*.swp +*.swo + +# DB +db.sqlite3 + +data/* diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/t0pic.py b/t0pic.py new file mode 100644 index 0000000..8dfc1c5 --- /dev/null +++ b/t0pic.py @@ -0,0 +1,103 @@ +# t0pic - pic.t0.vc +# MIT License + +import random +import string + +from flask import abort, Flask, request, redirect +from pathlib import Path +from PIL import Image + +PICS = Path('data') +MAX_SIZE = 1920 +PORT = 5003 +URL = 'https://pic.t0.vc' +POST = 'pic' + +def help(): + form = ( + '
' + '' + '' + '

'.format(URL, POST) + ) + return """ +
+                        pic.t0.vc
+NAME
+    t0pic: command line image host.
+
+USAGE
+    <image output> | curl -F '{0}=@/dev/stdin' {1}
+    or upload from the web:
+
+{2}
+
+DESCRIPTION
+    I got sick of imgur not working on mobile, so I built this
+    Images are resized to a max dimension of {3} px
+    Don't use this for anything serious
+
+EXAMPLES
+    ~$ cat kitten.jpg | curl -F '{0}=@/dev/stdin' {1}
+       {1}/YXKV.jpg
+    ~$ firefox {1}/YXKV.jpg
+
+    Add this to your .bashrc:
+
+    alias {0}="curl -F '{0}=@/dev/stdin' {1}"
+
+    Now you can pipe directly into {0}!
+
+SOURCE CODE
+    https://txt.t0.vc/CQQE
+    nginx config: https://txt.t0.vc/ZKEH
+
+SEE ALSO
+    https://txt.t0.vc
+    https://url.t0.vc
+
""".format(POST, URL, form, MAX_SIZE) + + +def new_id(): + return ''.join(random.choice(string.ascii_uppercase) for _ in range(4)) + +flask_app = Flask(__name__) + +@flask_app.route('/', methods=['GET']) +def index(): + return '{}'.format(help()) + +@flask_app.route('/', methods=['POST']) +def new(): + try: + nid = new_id() + while nid in [p.stem for p in PICS.iterdir()]: + nid = new_id() + + pic = request.files['pic'] + if not pic: raise + pic = Image.open(pic) + + if pic.format == 'PNG': + ext = '.png' + elif pic.format == 'JPEG': + ext = '.jpg' + else: + raise + + filename = nid + ext + pic.thumbnail([MAX_SIZE, MAX_SIZE], Image.ANTIALIAS) + pic.save(str(PICS.joinpath(filename))) + + print('Adding pic {}'.format(nid)) + + url = '{}/{}'.format(URL, filename) + if 'web' in request.form: + return redirect(url) + else: + return url + '\n' + except: + abort(400) + +flask_app.run(port=PORT)