2017-05-12 20:51:49 +02:00
|
|
|
import flask
|
2017-05-17 23:56:36 -04:00
|
|
|
from werkzeug.datastructures import ImmutableMultiDict, CombinedMultiDict
|
|
|
|
|
2017-05-12 20:51:49 +02:00
|
|
|
from nyaa import app, db
|
|
|
|
from nyaa import models, forms
|
2017-05-17 23:56:36 -04:00
|
|
|
from nyaa import bencode, backend, utils
|
2017-05-12 20:51:49 +02:00
|
|
|
from nyaa import torrents
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os.path
|
2017-05-17 23:56:36 -04:00
|
|
|
#from orderedset import OrderedSet
|
|
|
|
#from werkzeug import secure_filename
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
# #################################### API HELPERS ####################################
|
2017-05-12 20:51:49 +02:00
|
|
|
|
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
def validate_user(upload_request):
|
|
|
|
auth_info = None
|
2017-05-12 20:51:49 +02:00
|
|
|
try:
|
2017-05-17 23:56:36 -04:00
|
|
|
if 'auth_info' in upload_request.files:
|
|
|
|
auth_info = json.loads(upload_request.files['auth_info'].read().decode('utf-8'))
|
|
|
|
if 'username' not in auth_info.keys() or 'password' not in auth_info.keys():
|
|
|
|
return False, None, None
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
username = auth_info['username']
|
|
|
|
password = auth_info['password']
|
2017-05-12 20:51:49 +02:00
|
|
|
user = models.User.by_username(username)
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
user = models.User.by_email(username)
|
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
if (not user or password != user.password_hash or user.status == models.UserStatusType.INACTIVE):
|
|
|
|
return False, None, None
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
return True, user, None
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
except Exception as e:
|
|
|
|
return False, None, e
|
2017-05-12 20:51:49 +02:00
|
|
|
|
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
def _create_upload_category_choices():
|
|
|
|
''' Turns categories in the database into a list of (id, name)s '''
|
|
|
|
choices = [('', '[Select a category]')]
|
|
|
|
for main_cat in models.MainCategory.query.order_by(models.MainCategory.id):
|
|
|
|
choices.append((main_cat.id_as_string, main_cat.name, True))
|
|
|
|
for sub_cat in main_cat.sub_categories:
|
|
|
|
choices.append((sub_cat.id_as_string, ' - ' + sub_cat.name))
|
|
|
|
return choices
|
2017-05-12 20:51:49 +02:00
|
|
|
|
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
# #################################### API ROUTES ####################################
|
|
|
|
def api_upload(upload_request, user):
|
|
|
|
form_info = None
|
|
|
|
try:
|
|
|
|
form_info = json.loads(upload_request.files['torrent_info'].read().decode('utf-8'))
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
form_info_as_dict = []
|
|
|
|
for k, v in form_info.items():
|
|
|
|
if k in ['is_anonymous', 'is_hidden', 'is_remake', 'is_complete']:
|
|
|
|
if v == 'y':
|
|
|
|
form_info_as_dict.append((k, v))
|
2017-05-12 20:51:49 +02:00
|
|
|
else:
|
2017-05-17 23:56:36 -04:00
|
|
|
form_info_as_dict.append((k, v))
|
|
|
|
form_info = ImmutableMultiDict(form_info_as_dict)
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
# print(repr(form_info))
|
|
|
|
except Exception as e:
|
|
|
|
return flask.make_response(flask.jsonify({"Failure": "Invalid form. See HELP in api_uploader.py"}), 400)
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
try:
|
|
|
|
torrent_file = upload_request.files['torrent_file']
|
|
|
|
torrent_file = ImmutableMultiDict([('torrent_file', torrent_file)])
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
# print(repr(torrent_file))
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
form = forms.UploadForm(CombinedMultiDict((torrent_file, form_info)))
|
|
|
|
form.category.choices = _create_upload_category_choices()
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
if upload_request.method == 'POST' and form.validate():
|
|
|
|
torrent = backend.handle_torrent_upload(form, user, True)
|
2017-05-12 20:51:49 +02:00
|
|
|
|
2017-05-17 23:56:36 -04:00
|
|
|
return flask.make_response(flask.jsonify({"Success": "Request was processed {0}".format(torrent.id)}), 200)
|
2017-05-12 20:51:49 +02:00
|
|
|
else:
|
2017-05-17 23:56:36 -04:00
|
|
|
# print(form.errors)
|
|
|
|
return_error_messages = []
|
|
|
|
for error_name, error_messages in form.errors.items():
|
|
|
|
# print(error_messages)
|
|
|
|
return_error_messages.extend(error_messages)
|
|
|
|
|
|
|
|
return flask.make_response(flask.jsonify({"Failure": return_error_messages}), 400)
|