diff options
Diffstat (limited to '')
-rwxr-xr-x | www/app.py | 17 |
1 files changed, 13 insertions, 4 deletions
@@ -8,6 +8,8 @@ from psutil import Process from urllib.parse import quote from datetime import datetime from locale import setlocale, LC_ALL +if getenv("PROFILE"): + from cProfile import Profile path.append(".") from travnik import glob, Type setlocale(LC_ALL, "") @@ -23,26 +25,33 @@ def mno(quantity, types): return types[0] @app.route("/") def index(): + if getenv("PROFILE"): + pr = Profile() + pr.enable() result = [] for hash, torrent in torrents.items(): if not request.args.get("regex"): break if len(result) >= 100: break - if torrent.matches(request.args.get("regex")): + if torrent.matches(request.args.get("regex"), True if getenv("CACHE") else False): try: decodedname = torrent.dict.get(b'info').get(b'name').decode() except UnicodeDecodeError: decodedname = torrent.dict.get(b'info').get(b'name').decode("iso-8859-2") - this = {"tree": torrent.matching_files(request.args.get("regex") if sum(1 for name, size in torrent.paths()) > 10 else "", True), "files": sum(1 for name, size in torrent.paths()), "found": datetime.fromtimestamp(torrent.dict.get(b'creation date')), "size": sum(size for name, size in torrent.paths()), "name": decodedname, "magnet": "magnet:?dn=" + quote(torrent.dict.get(b'info').get(b'name')) + this = {"ip": torrent.dict.get(b'source').get(b'ip').decode(), "tree": torrent.matching_files(request.args.get("regex") if sum(1 for name, size in torrent.paths()) > 10 else "", True), "files": sum(1 for name, size in torrent.paths()), "found": datetime.fromtimestamp(torrent.dict.get(b'creation date')), "size": sum(size for name, size in torrent.paths()), "name": decodedname, "magnet": "magnet:?dn=" + quote(torrent.dict.get(b'info').get(b'name')) + (("&xt=urn:btih:" + torrent.sha1.hex()) if torrent.type == Type.V1 or torrent.type == Type.HYBRID else "") + (("&xt=urn:btmh:1220" + torrent.sha256.hex()) if torrent.type == Type.V2 or torrent.type == Type.HYBRID else "")} result.append(this) result = sorted(result, reverse=(request.args.get("order") == "padajoče"), key=lambda x:x["found"].timestamp() if request.args.get("sort") == "datumu" else x["files" if request.args.get("sort") == "datotekah" else "size" if request.args.get("sort") == "velikosti" else "crash"]) - return render_template("index.html", found_torrents=result, lentorrents=len(result)) + out = render_template("index.html", found_torrents=result, lentorrents=len(result)) + if pr is not None: + pr.disable() + pr.dump_stats(getenv("PROFILE")) + return out if __name__ == "__main__": print("zaganjam travnik", argv[0], "... zagon traja dolgo časa (~5 min za ~40k torrentov. za delovanje je potrebnih ~300 MiB RAM RES za ~40k torrentov. sharding je WIP.") start = monotonic() torrents = glob(getenv("TORRENTS") if getenv("TORRENTS") else ".") app.jinja_env.globals.update(mno=mno, zagontekst=zagon.strftime("%c"), torrentov=len(torrents), rammib=round(Process(getpid()).memory_info().rss/(1024*1024)), roundstartuptime=round(monotonic()-start)) app.jinja_env.add_extension('jinja2.ext.loopcontrols') - app.run(host="::", port=8080, debug=True) + app.run(host="::", port=8080, debug=False if getenv("NDEBUG") else True) |