This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nnnnnnaas.py
60 lines (43 loc) · 2.04 KB
/
nnnnnnaas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import json
from random import choice
from dicttoxml import dicttoxml
from flask import Flask, request, Response, jsonify, render_template
from redis import Redis
app = Flask(__name__)
MAX_LENGTH = 500000
FORMATS = {"xml": lambda ret: Response(dicttoxml(ret), mimetype="application/xml"),
"json": lambda ret: jsonify(response=ret),
"txt": lambda ret: Response("\n\n".join(["".join(x) for x in ret]), mimetype="text/plain"),
"html": lambda ret: render_template("gimme.html", ret=ret, choice=choice)}
TEXTS = {"nano": ["NANO"], "hakase": ["HAKASE"], "adore": ["NANO", "HAKASE"], "asie": ["ASIE"], "hakasie": ["HAKASIE"],
"tap": ["*taps asie's head*", "dlaczego mogę ją tapować po łebku?"]}
count_cache = Redis(db=int(os.getenv("REDIS_DB", 0)))
def repeater(texts: list, paragraphs: int = 1, repeats: int = 6) -> list:
if sum([len(x) for x in texts]) * paragraphs * repeats > MAX_LENGTH:
return [["baka hentai"]]
count_cache.incr("nnnnnnaas:"+json.dumps(texts), paragraphs*repeats)
ret = []
for _ in range(paragraphs):
for text in texts:
ret.append([text] * repeats)
return ret
@app.route("/")
def index() -> Response:
return render_template("landing.html", formats=sorted(FORMATS.keys()), texts=sorted(TEXTS.keys()), limit=MAX_LENGTH)
@app.route("/api/data.json")
def api_json() -> Response:
return jsonify(formats=list(FORMATS), texts=list(TEXTS))
def gimme(texts: list, target: str) -> Response:
ret = repeater(texts=texts, paragraphs=request.values.get("paragraphs", 1, type=int),
repeats=request.values.get("repeats", 6, type=int))
try:
return FORMATS[target](ret)
except KeyError:
return Response("Format not recognized", mimetype="text/plain")
for text in TEXTS:
for target in FORMATS:
app.add_url_rule("/%s.%s" % (text, target), view_func=gimme, defaults=dict(target=target, texts=TEXTS[text]),
methods=["GET", "POST"])
if __name__ == '__main__':
app.run(debug=True)