Finish coding reg

This commit is contained in:
Tanner Collin 2020-05-03 23:02:56 +00:00
parent a7ddce7144
commit a363b3f4cd

View File

@ -1,61 +1,69 @@
# t0txt - txt.t0.vc # t0reg - reg.t0.vc
# MIT License # MIT License
import random import random
import shelve
import string import string
from flask import abort, Flask, request, redirect from flask import abort, Flask, request, redirect
DB = 'data/t0txt' PORT = 5005
PORT = 5002 URL = 'https://reg.t0.vc'
URL = 'https://txt.t0.vc' POST = 'reg'
POST = 'txt'
def help(): registers = {}
def help(nid):
form = ( form = (
'<form action="{0}" method="POST" accept-charset="UTF-8">' '<form action="{0}" method="POST" accept-charset="UTF-8">'
'<input name="web" type="hidden" value="true">' '<input name="web" type="hidden" value="true">'
'<textarea name="{1}" cols="60" rows="18"></textarea>' '<input name="{1}">'
'<br><button type="submit">Submit</button></form>'.format(URL, POST) '<br><button type="submit">Submit</button></form>'.format(URL, nid)
) )
return """ return """
<pre> <pre>
txt.t0.vc reg.t0.vc
NAME NAME
t0txt: command line pastebin. t0reg: command line key-value registers
USAGE USAGE
&lt;command&gt; | curl -F '{0}=&lt;-' {1} &lt;command&gt; | curl -F '{0}=&lt;-' {1}
or upload from the web: or upload from the web:
{2} {2}
The key {0} was randomly chosen for you.
DESCRIPTION DESCRIPTION
I got sick of sprunge.us always going down, so I built this This lets you POST data to a key of your choice.
You can then GET the data at that key.
This is useful for moving data / files between servers.
All the data is stored temporarily.
Make your key random if you don't want people guessing.
EXAMPLES EXAMPLES
~$ cat yourfile | curl -F '{0}=&lt;-' {1} ~$ echo "hello world" | curl -F '{0}=&lt;-' {1}
{1}/MOJV ~$ curl {1}/{0}
~$ firefox {1}/MOJV hello world
Add this to your .bashrc: Add this to your .bashrc:
alias {0}=" \\ alias push="curl -F '{0}=<-' {1}"
sed -r 's/\x1B\[([0-9]{{1,2}}(;[0-9]{{1,2}})?)?[m|K]//g' \\ alias pull="curl {1}/{0}"
| curl -F '{0}=<-' {1}"
Now you can pipe directly into {0}! Sed removes colours. Now you can pipe directly into push!
Get the data back with pull.
To move a file:
~$ cat kitten.jpg | base64 | push
~$ pull | base64 -d > kitten.jpg
SOURCE CODE SOURCE CODE
https://txt.t0.vc/GPBV https://txt.t0.vc/SBSU
https://github.com/tannercollin/t0txt
SEE ALSO SEE ALSO
https://txt.t0.vc
https://pic.t0.vc https://pic.t0.vc
https://url.t0.vc https://url.t0.vc
http://github.com/rupa/sprunge </pre>""".format(nid, URL, form)
</pre>""".format(POST, URL, form)
def new_id(): def new_id():
return ''.join(random.choice(string.ascii_uppercase) for _ in range(4)) return ''.join(random.choice(string.ascii_uppercase) for _ in range(4))
@ -64,35 +72,27 @@ flask_app = Flask(__name__)
@flask_app.route('/', methods=['GET']) @flask_app.route('/', methods=['GET'])
def index(): def index():
return '<html><body>{}</body></html>'.format(help()) return '<html><body>{}</body></html>'.format(help(new_id()))
@flask_app.route('/', methods=['POST']) @flask_app.route('/', methods=['POST'])
def new(): def new():
try: try:
with shelve.open(DB) as db: pairs = [x for x in request.form.items() if x[0] != 'web']
nid = new_id() for key, value in pairs:
while nid in db: print('Adding:', key)
nid = new_id() registers[key] = value
txt = request.form['txt']
if not txt: raise
db[nid] = txt
print('Adding note {}:\n{}'.format(nid, txt))
url = '{}/{}'.format(URL, nid)
if 'web' in request.form: if 'web' in request.form:
return redirect(url) return redirect(URL + '/' + key)
else: else:
return url + '\n' return '', 200
except: except:
abort(400) abort(400)
@flask_app.route('/<nid>', methods=['GET']) @flask_app.route('/<rid>', methods=['GET'])
def get(nid): def get(rid):
try: try:
with shelve.open(DB) as db: return registers[rid], 200, {'Content-Type': 'text/plain; charset=utf-8'}
return db[nid] + '\n', 200, {'Content-Type': 'text/plain; charset=utf-8'}
except: except:
abort(404) abort(404)