Add category resolver

This commit is contained in:
sqozz 2018-04-01 01:14:48 +02:00
parent e351a61964
commit 3c2b49fc74
2 changed files with 34 additions and 16 deletions

View file

@ -15,33 +15,48 @@ LANGUAGES = ['en', 'de']
settings = None
def get_categories():
cats = settings["categories"]
for c in 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"]))
return cats
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 <a href=\"/search?h={}\">{}</a>").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/<filename>")
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():
@ -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()

View file

@ -8,7 +8,7 @@ vim: ts=2 noexpandtab
<link href="{{ url_for("static", filename="css/details.css") }}" rel="stylesheet">
<script src="{{ url_for("static", filename="js/main.js") }}"></script>
<div id="torrent" class="clearfix">
<h4>{{ torrent.category }} » {{ torrent.subcategory}} » {{ torrent.name }}</h4>
<h4>{{ torrent.category_string }} » {{ torrent.subcategory_string }} » {{ torrent.name }}</h4>
<div id="details" class="clearfix">
<div class="detailrow clearfix">
<div>.torrent-file:</div>