2015-02-05 01:02:32 +01:00
#!/usr/bin/python3
2015-02-12 22:06:31 +01:00
#/* vim:set ts=2 set noexpandtab */
2015-02-19 00:50:18 +01:00
import json , uuid , hashlib , sqlite3 , base64
2015-02-06 22:03:42 +01:00
from flask import Flask , render_template , url_for , request
2015-02-15 03:48:39 +01:00
from werkzeug import secure_filename
2015-02-05 22:17:33 +01:00
app = Flask ( __name__ )
2015-02-15 03:48:39 +01:00
strings = None
settings = None
2015-02-05 01:02:32 +01:00
2015-02-05 22:17:33 +01:00
@app.route ( " / " )
def index ( ) :
2015-02-15 03:48:39 +01:00
return render_template ( " search.html " , language = " english " , categories = settings [ " categories " ] , strings = strings )
2015-02-05 22:17:33 +01:00
2015-02-15 03:48:39 +01:00
@app.route ( " /categories " )
2015-02-05 22:17:33 +01:00
def categorys ( ) :
2015-02-15 03:48:39 +01:00
return render_template ( " categories.html " , categories = settings [ " categories " ] )
2015-02-05 22:17:33 +01:00
2015-02-15 03:48:39 +01:00
@app.route ( " /create " , methods = [ ' GET ' , ' POST ' ] )
2015-02-05 22:38:39 +01:00
def create ( ) :
2015-02-15 03:48:39 +01:00
if request . method == " GET " :
return render_template ( " create.html " , language = " english " , categories = settings [ " categories " ] , strings = strings , errors = None )
elif request . method == " POST " :
2015-02-15 04:51:27 +01:00
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-05 22:38:39 +01:00
2015-02-06 22:03:42 +01:00
@app.route ( " /search " , methods = [ ' GET ' ] )
def search ( ) :
2015-02-15 03:48:39 +01:00
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 )
2015-02-19 00:50:18 +01:00
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); ' )
c . execute ( ' CREATE TABLE IF NOT EXISTS language_mapping (fileid TEXT NOT NULL, language TEXT NOT NULL); ' )
connection . commit ( )
connection . close ( )
2015-02-15 03:48:39 +01:00
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
2015-02-15 04:51:27 +01:00
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 " ] )
2015-02-19 00:50:18 +01:00
#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 " ]
languages = [ ]
newTFile = TorrentFile ( safeFilename , name , category , subcategory , description , languages )
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
languages = [ ]
def __init__ ( self , fileid = fileid , name = name , category = category , subcategory = subcategory , description = description , languages = languages ) :
self . fileid = fileid
self . name = name
self . category = category
self . subcategory = subcategory
self . description = description
self . languages = languages
def writeToDb ( self , cursor ) :
c = cursor
b64description = base64 . b64encode ( self . description . encode ( ) )
c . execute ( " INSERT INTO torrents(fileid, name, category, subcategory, description) VALUES(:fileid, :name, :category, :subcategory, :description) " , { ' fileid ' : self . fileid , ' name ' : self . name , ' category ' : self . category , ' subcategory ' : self . subcategory , ' description ' : b64description } )
for language in self . languages :
c . execute ( " INSERT INTO language_mapping(fileid, language) " , { " fileid " : self . fileid , " language " : language } )
2015-02-06 22:03:42 +01:00
2015-02-05 22:17:33 +01:00
if __name__ == " __main__ " :
2015-02-15 03:48:39 +01:00
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 )