TorrentIndexer/indexer.py

75 lines
2.4 KiB
Python
Raw Normal View History

2015-02-05 01:02:32 +01:00
#!/usr/bin/python3
#/* vim:set ts=2 set noexpandtab */
import json, uuid, hashlib
2015-02-06 22:03:42 +01:00
from flask import Flask, render_template, url_for, request
from werkzeug import secure_filename
app = Flask(__name__)
strings = None
settings = None
2015-02-05 01:02:32 +01:00
@app.route("/")
def index():
return render_template("search.html", language="english", categories=settings["categories"], strings=strings)
@app.route("/categories")
def categorys():
return render_template("categories.html", categories=settings["categories"])
@app.route("/create", methods=['GET','POST'])
def create():
if request.method == "GET":
return render_template("create.html", language="english", categories=settings["categories"], strings=strings, errors=None)
elif request.method == "POST":
errors = createNewTorrent(request)
if errors == None:
return "It's allright"
else:
return render_template("create.html", language="english", categories=settings["categories"], strings=strings, errors=errors)
2015-02-06 22:03:42 +01:00
@app.route("/search", methods=['GET'])
def search():
return render_template("result.html", results=request.args.get("q", ""))
def init():
global strings
global settings
with open("strings.json") as stringsJson:
strings = json.load(stringsJson)
with open("settings.json") as settingsJson:
settings = json.load(settingsJson)
def getLocalString(language, descriptor):
global strings
if language in strings.keys():
if descriptor in strings[language].keys():
return strings[language][descriptor]
else:
return descriptor
else:
return descriptor
def createNewTorrent(reuqest):
uploadfile = request.files["torrentFile"]
filename = secure_filename(uploadfile.filename)
h = hashlib.sha256()
h.update((str(uuid.uuid4()) + filename).encode())
safeFilename = h.hexdigest()
uploadfile.save("torrentFiles/" + safeFilename)
print("=== CREATE NEW TORRENT FILE ===")
print( "Name: " + request.form["name"] )
print( "Torrent file: " + safeFilename )
print( "Category: " + request.form["category"] )
print( "Subcategory: " + request.form["subcategory"] )
print( "Description: " + request.form["description"] )
# TODO: Process inputdate from the form and save it to the (until now) non-existing DB
return ["Error1", "Error2"]
2015-02-06 22:03:42 +01:00
if __name__ == "__main__":
init()
app.jinja_env.globals.update(getLocalString=getLocalString)
app.jinja_env.globals.update(json=json)
app.jinja_env.globals.update(sorted=sorted)
app.run(debug=True)