summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--templates/index.html28
-rw-r--r--travnik.py34
-rwxr-xr-xwww/app.py31
3 files changed, 70 insertions, 23 deletions
diff --git a/templates/index.html b/templates/index.html
index b4f0804..ea3c3fe 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -16,7 +16,7 @@
<label for=regex>
regex
</label>
- <input name=regex id=regex placeholder=DVDRip value="{{ request.args.get("regex")|e }}" />
+ <input name=regex id=regex placeholder=DVDRip value="{{ request.args.get("regex") if request.args.get("regex") else "" | e }}" />
razvrsti
<select name=order>
<option {{ "selected" if request.args.get("order") == "naraščajoče" else "" }} value=naraščajoče>naraščajoče</option>
@@ -30,8 +30,32 @@
<option {{ "selected" if request.args.get("sort") == "datotekah" }} value=datotekah>številu datotek</option>
</select>
<input type=submit value=išči />
+ {% if request.args.get("regex") %}
+ <p>število zadetkov: {{ lentorrents }}</p>
+ {% endif %}
+ {% for torrent in found_torrents %}
+ <h2><a href={{ torrent.magnet }}>{{ torrent.name | e }}</a></h2>
+ <p>velikost: {{ (torrent.size/(1024**3)) | round(3) }} GiB | datum najdbe: {{ torrent.found.strftime("%c") }} | število datotek: {{ torrent.files }}</p>
+ <b><ul>
+ {% macro direktorij(dir) %}
+ {% for ime, element in dir.items() %}
+ <li>
+ {{ ime }}
+ {% if element is mapping %}
+ <ul>
+ {{ direktorij(element) }}
+ </ul>
+ {% else %}
+ ({{ (element/(1024**3)) | round(3) }} MiB)
+ {% endif %}
+ </li>
+ {% endfor %}
+ {% endmacro %}
+ </ul></b>
+ {{ direktorij(torrent.tree) }}
+ {% endfor %}
</form>
<hr>
- <p><a href=//ni.šijanec.eu./sijanec/travnik>travnik</a> ima <b>{{ torrentov }}</b> {{ mno(torrentov, ["torrentov", "torrent", "torrenta", "torrente"]) }}. prižigal se je v <b>{{ zagontekst }}</b> v {{ roundstartuptime }} s in porablja {{ rammib }} MiB fizičnega pomnilnika.
+ <p><a href=//ni.šijanec.eu./sijanec/travnik>travnik</a> ima <b>{{ torrentov }}</b> {{ mno(torrentov, ["torrentov", "torrent", "torrenta", "torrente"]) }}. prižgal se je v <b>{{ zagontekst }}</b> v {{ roundstartuptime }} s in porablja {{ rammib }} MiB fizičnega pomnilnika.
</body>
</html>
diff --git a/travnik.py b/travnik.py
index 20586f4..98ef449 100644
--- a/travnik.py
+++ b/travnik.py
@@ -75,24 +75,36 @@ class Torrent():
for z, v in paths_r(self.files):
yield z, v
def matches(self, r):
- if search(r, self.dict.get(b'info').get(b'name'), IGNORECASE):
+ try:
+ decoded = self.dict.get(b'info').get(b'name').decode()
+ except UnicodeDecodeError:
+ decoded = self.dict.get(b'info').get(b'name').decode("iso-8859-2")
+ if search(r, decoded, IGNORECASE):
return True
- for path, size in paths(self):
- if search(r, path, IGNORECASE):
+ for path, size in self.paths():
+ try:
+ decd = b'/'.join(path).decode()
+ except UnicodeDecodeError:
+ decd = b'/'.join(path).decode("iso-8859-2")
+ if search(r, decd, IGNORECASE):
return True
return False
- def matching_files(self, r):
- def matching_files_r(dir, r):
+ def matching_files(self, r, decode=False):
+ def matching_files_r(dirc, r, decode):
files = {}
- for name, content in self.paths:
- if search(r, name, IGNORECASE):
- files[name] = content
+ for name, content in dirc.items():
+ try:
+ decoded = name.decode()
+ except UnicodeDecodeError:
+ decoded = name.decode("iso-8859-2") # TODO we could try detecting the encoding
+ if search(r, decoded, IGNORECASE):
+ files[decoded if decode else name] = content
if type(content) is dict:
- inhalt = matching_files_r(content, r)
+ inhalt = matching_files_r(content, r, decode)
if inhalt:
- files[name] = inhalt
+ files[decoded if decode else name] = inhalt
return files
- return matching_files_r(self.paths, r)
+ return matching_files_r(self.files, r, decode)
def __repr__(self):
return str(self.__dict__)
def __hash__(self):
diff --git a/www/app.py b/www/app.py
index 1ba6af7..1bb512a 100755
--- a/www/app.py
+++ b/www/app.py
@@ -3,7 +3,7 @@ from re import search, IGNORECASE
from time import monotonic
from flask import Flask, render_template, escape, request
from sys import argv, path
-from os import getpid
+from os import getpid, getenv
from psutil import Process
from urllib.parse import quote
from datetime import datetime
@@ -12,24 +12,35 @@ path.append(".")
from travnik import glob, Type
setlocale(LC_ALL, "")
app = Flask("travnik")
-startuptime = -1
zagon = datetime.now()
def mno(quantity, types):
if quantity % 100 == 1:
- return quantity[1]
+ return types[1]
if quantity % 100 == 2:
- return quantity[2]
+ return types[2]
if quantity % 100 == 3:
- return quantity[3]
- return quantity[0]
+ return types[3]
+ return types[0]
@app.route("/")
def index():
- return render_template("index.html")
+ result = []
+ for hash, torrent in torrents.items():
+ if not request.args.get("regex"):
+ break
+ if torrent.matches(request.args.get("regex")):
+ 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'))
+ + (("&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))
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 = {}
- print("zagon uspešen. v", monotonic()-start, "sem indeksiral", len(torrents), "torrentov")
- app.jinja_env.globals.update(mno=mno, zagontekst=zagon.strftime("%c"), torrentov=len(torrents))
+ 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)