TorrentIndexer/indexer.py

119 lines
4.7 KiB
Python

#!/usr/bin/python3
#/* vim:set ts=2 set noexpandtab */
import json, uuid, hashlib, sqlite3, base64
from flask import Flask, render_template, url_for, request
from werkzeug import secure_filename
app = Flask(__name__)
strings = None
settings = None
@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)
@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)
initDb()
def initDb():
connection = sqlite3.connect("torrentdb.sqlite")
c = connection.cursor()
c.execute('CREATE TABLE IF NOT EXISTS torrents (fileid TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL, category TEXT NOT NULL, subcategory TEXT NOT NULL, description TEXT NOT NULL, audioquality_description TEXT NOT NULL, videoquality_description TEXT NOT NULL);')
connection.commit()
connection.close()
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: Validate the input serverside before writing it to the database
name = request.form["name"]
category = request.form["category"]
subcategory = request.form["subcategory"]
description = request.form["description"]
audioquality_description = request.form["audioquality_description"]
videoquality_description = request.form["videoquality_description"]
newTFile = TorrentFile(safeFilename, name, category, subcategory, description, audioquality_description, videoquality_description)
connection = sqlite3.connect("torrentdb.sqlite")
newTFile.writeToDb(connection.cursor())
connection.commit()
connection.close()
return ["Error1"]
class TorrentFile():
fileid = None
name = None
category = None
subcategory = None
description = None
audioquality_description = None
videoquality_description = None
def __init__(self, fileid=fileid, name=name, category=category, subcategory=subcategory, description=description, audioquality_description=audioquality_description, videoquality_description=videoquality_description):
self.fileid = fileid
self.name = name
self.category = category
self.subcategory = subcategory
self.description = description
self.audioquality_description = audioquality_description
self.videoquality_description = videoquality_description
def writeToDb(self, cursor):
c = cursor
b64description = base64.b64encode(self.description.encode())
b64audioquality_description = base64.b64encode(self.audioquality_description.encode())
b64videoquality_description = base64.b64encode(self.videoquality_description.encode())
c.execute("INSERT INTO torrents(fileid, name, category, subcategory, description, audioquality_description, videoquality_description) VALUES(:fileid, :name, :category, :subcategory, :description, :audioquality_description, :videoquality_description)", { 'fileid' : self.fileid, 'name' : self.name, 'category' : self.category, 'subcategory' : self.subcategory, 'description' : b64description , 'audioquality_description' : b64audioquality_description, 'videoquality_description' : b64videoquality_description})
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)