From 3c2b49fc7443ae3b1ab5a1a08ad064faec2efce6 Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 1 Apr 2018 01:14:48 +0200 Subject: [PATCH 1/5] Add category resolver --- indexer.py | 48 +++++++++++++++++++++++++++++------------- templates/details.html | 2 +- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/indexer.py b/indexer.py index ee516d5..ce4f97b 100644 --- a/indexer.py +++ b/indexer.py @@ -15,33 +15,48 @@ LANGUAGES = ['en', 'de'] settings = None -def get_categories(): - cats = settings["categories"] - for c in cats: - c["label"] = str(lazy_gettext(c["label"])) - for s in c["subcategories"]: - s["label"] = str(lazy_gettext(s["label"])) - return cats + +class Categories(): + def __init__(self): + + self.categories = settings["categories"] + for c in self.categories: + c["label"] = str(lazy_gettext(c["label"])) + for s in c["subcategories"]: + s["label"] = str(lazy_gettext(s["label"])) + + def find(self, category, subcategory = None): + cat_name = "" + sub_name = "" + for cat in self.categories: + if cat["id"] == category: + cat_name = cat["label"] + if subcategory != None: + for sub in cat["subcategories"]: + if sub["id"] == subcategory: + sub_name = sub["label"] + return (cat_name, sub_name) + @app.route("/") def index(): - return render_template("search.html", categories=get_categories()) + return render_template("search.html", categories=categories.categories) @app.route("/categories") def categorys(): - return render_template("categories.html", categories=get_categories()) + return render_template("categories.html", categories=categories.categories) @app.route("/create", methods=['GET','POST']) def create(): if request.method == "GET": - return render_template("create.html", categories=get_categories(), errors=None) + return render_template("create.html", categories=categories.categories, errors=None) elif request.method == "POST": newTorrent = createNewTorrent(request) if len(newTorrent.errors) == 0: message = _("Successfully created torrent {}").format(newTorrent.fileid, newTorrent.fileid[:-20]) - return render_template("create.html", categories=get_categories(), messages=[message]) + return render_template("create.html", categories=categories.categories, messages=[message]) else: - return render_template("create.html", categories=get_categories(), errors=newTorrent.errors) + return render_template("create.html", categories=categories.categories, errors=newTorrent.errors) @app.route("/download/") def download(filename): @@ -86,14 +101,14 @@ def search(): r = row[0:2] + (size(float(row[2])) , ) + row[3:] results.append(r) - return render_template("result.html", results=results, categories=get_categories()) + return render_template("result.html", results=results, categories=categories.categories) @app.route("/details", methods=['GET']) def details(): info_hash = request.args["h"] tf = TorrentFile(fileid=info_hash) tf.fromDb() - return render_template("details.html", categories=get_categories(), torrent=tf) + return render_template("details.html", categories=categories.categories, torrent=tf) def scrapeAll(): TRACKER_URL = "http://tracker.lootbox.cf:6969/" @@ -106,6 +121,8 @@ def init(): with open("settings.json") as settingsJson: settings = json.load(settingsJson) initDb() + global categories + categories = Categories() #scrapeAll() def initDb(): @@ -142,7 +159,7 @@ def createNewTorrent(reuqest): if a in [b'udp://tracker.lootbox.cf:6969/announce', b'udp://tracker.lootbox.cf:6969/announce/',b'udp://tracker.lootbox.cf/announce',b'udp://tracker.lootbox.cf/announce/']: is_ours = True break - + if not is_ours: newTFile.errors = ["Rejecting torrent {}, as it does not use our tracker".format(info_hash, info_hash[:-20])] return newTFile @@ -253,6 +270,7 @@ class TorrentFile(): self.name = (base64.b64decode(res["name"])).decode() self.category = res["category"] self.subcategory = res["subcategory"] + self.category_string, self.subcategory_string = categories.find(int(self.category), int(self.subcategory)) self.description = (base64.b64decode(res["description"])).decode() self.audioquality_description = (base64.b64decode(res["audioquality_description"])).decode() self.videoquality_description = (base64.b64decode(res["videoquality_description"])).decode() diff --git a/templates/details.html b/templates/details.html index 1efcb6a..e4d82b2 100644 --- a/templates/details.html +++ b/templates/details.html @@ -8,7 +8,7 @@ vim: ts=2 noexpandtab
-

{{ torrent.category }} » {{ torrent.subcategory}} » {{ torrent.name }}

+

{{ torrent.category_string }} » {{ torrent.subcategory_string }} » {{ torrent.name }}

.torrent-file:
From 46ad8bddad41cb4a7002e841991aa94adc25405b Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 1 Apr 2018 01:29:57 +0200 Subject: [PATCH 2/5] Add clickable categories --- templates/details.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/details.html b/templates/details.html index e4d82b2..b7aff67 100644 --- a/templates/details.html +++ b/templates/details.html @@ -8,7 +8,7 @@ vim: ts=2 noexpandtab
-

{{ torrent.category_string }} » {{ torrent.subcategory_string }} » {{ torrent.name }}

+

{{ torrent.category_string }} » {{ torrent.subcategory_string }} » {{ torrent.name }}

.torrent-file:
From befc9db8770ee071456e5fcd568f6609c9a3efb8 Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 1 Apr 2018 01:35:01 +0200 Subject: [PATCH 3/5] Add config for valid tracker --- indexer.py | 5 +++-- settings.json | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/indexer.py b/indexer.py index ce4f97b..3352631 100644 --- a/indexer.py +++ b/indexer.py @@ -111,7 +111,7 @@ def details(): return render_template("details.html", categories=categories.categories, torrent=tf) def scrapeAll(): - TRACKER_URL = "http://tracker.lootbox.cf:6969/" + TRACKER_URL = "" statedump = requests.get(TRACKER_URL + "stats" + "?mode=statedump") return @@ -156,7 +156,8 @@ def createNewTorrent(reuqest): is_ours = False for a in announce: - if a in [b'udp://tracker.lootbox.cf:6969/announce', b'udp://tracker.lootbox.cf:6969/announce/',b'udp://tracker.lootbox.cf/announce',b'udp://tracker.lootbox.cf/announce/']: + a = a.decode("utf-8", "ignore") + if a in settings["valid_tracker"]: is_ours = True break diff --git a/settings.json b/settings.json index 8463f41..6997ccb 100644 --- a/settings.json +++ b/settings.json @@ -184,6 +184,11 @@ } ] } + ], + + "valid_tracker" : [ + "udp://tracker.lootbox.cf:6969/announce", + "udp://tracker.lootbox.cf:6969/announce/" ] } From 6c66eeb86cabe793507ffa383e27b6b8c53b9c52 Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 1 Apr 2018 01:36:11 +0200 Subject: [PATCH 4/5] Disable debug mode --- indexer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indexer.py b/indexer.py index 3352631..a04c343 100644 --- a/indexer.py +++ b/indexer.py @@ -285,6 +285,6 @@ if __name__ == "__main__": init() app.jinja_env.globals.update(json=json) app.jinja_env.globals.update(sorted=sorted) - app.run(debug=True, host="127.0.0.1") + app.run(debug=False, host="127.0.0.1") # vim: set ts=2 sts=2 sw=2 noexpandtab: From 51c9a55968651b3aade9d435c6d5d9a89d5e6e4e Mon Sep 17 00:00:00 2001 From: sqozz Date: Sun, 1 Apr 2018 02:12:40 +0200 Subject: [PATCH 5/5] Fix decoding of empty values --- indexer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/indexer.py b/indexer.py index a04c343..066113c 100644 --- a/indexer.py +++ b/indexer.py @@ -210,13 +210,13 @@ class Metadata(): return self.fileid = fileid self.bcoded = bencoder.decode(torrent) - self.created_by = self.bcoded.get(b'created by', b"") - self.creation_date = self.bcoded.get(b'creation date', b"") + self.created_by = self.bcoded.get(b'created by', b"").decode("utf-8", "ignore") + self.creation_date = self.bcoded.get(b'creation date', b"").decode("utf-8", "ignore") self.announce_url = self.bcoded.get(b'info', dict()).get(b'', "") self.source = self.bcoded.get(b'info', dict()).get(b'source', b"") self.torrentsize = ((len(self.bcoded.get(b'info', dict()).get(b'pieces', "")) / 20) * self.bcoded.get(b'info', dict()).get(b'piece length')) self.torrentsize_human = size(self.torrentsize) - self.name = self.bcoded.get(b'info', dict()).get(b'name', b"") + self.name = self.bcoded.get(b'info', dict()).get(b'name', b"").decode("utf-8", "ignore") self.private = self.bcoded.get(b'info', dict()).get(b'private', b"") def writeToDb(self, cursor):