mirror of
https://github.com/simon987/nyaa.git
synced 2025-12-14 15:49:02 +00:00
Remove v1 upload api and update notice
This commit is contained in:
@@ -6,6 +6,9 @@ from nyaa import models, forms
|
||||
from nyaa import bencode, backend, utils
|
||||
from nyaa import torrents
|
||||
|
||||
# For _create_upload_category_choices
|
||||
from nyaa import routes
|
||||
|
||||
import functools
|
||||
import json
|
||||
import os.path
|
||||
@@ -42,87 +45,7 @@ def api_require_user(f):
|
||||
return decorator
|
||||
|
||||
|
||||
def validate_user(upload_request):
|
||||
auth_info = None
|
||||
try:
|
||||
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
|
||||
|
||||
username = auth_info['username']
|
||||
password = auth_info['password']
|
||||
user = models.User.by_username(username)
|
||||
|
||||
if not user:
|
||||
user = models.User.by_email(username)
|
||||
|
||||
if not user or password != user.password_hash or \
|
||||
user.status == models.UserStatusType.INACTIVE:
|
||||
return False, None, None
|
||||
|
||||
return True, user, None
|
||||
else:
|
||||
return False, None, None
|
||||
|
||||
except Exception as e:
|
||||
return False, None, e
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
# #################################### 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'))
|
||||
|
||||
form_info_as_dict = []
|
||||
for k, v in form_info.items():
|
||||
if k in ['is_anonymous', 'is_hidden', 'is_remake', 'is_complete', 'is_trusted']:
|
||||
if v:
|
||||
form_info_as_dict.append((k, v))
|
||||
else:
|
||||
form_info_as_dict.append((k, v))
|
||||
# Hack for while v1 is still being used: default trusted to true
|
||||
if not [x for x in form_info_as_dict if x[0] == 'is_trusted']:
|
||||
form_info_as_dict.append(('is_trusted', True))
|
||||
form_info = ImmutableMultiDict(form_info_as_dict)
|
||||
except Exception as e:
|
||||
return flask.make_response(flask.jsonify(
|
||||
{'Failure': ['Invalid data. See HELP in api_uploader.py']}), 400)
|
||||
|
||||
try:
|
||||
torrent_file = upload_request.files['torrent_file']
|
||||
torrent_file = ImmutableMultiDict([('torrent_file', torrent_file)])
|
||||
except Exception as e:
|
||||
return flask.make_response(flask.jsonify(
|
||||
{'Failure': ['No torrent file was attached.']}), 400)
|
||||
|
||||
form = forms.UploadForm(CombinedMultiDict((torrent_file, form_info)), meta={'csrf': False})
|
||||
form.category.choices = _create_upload_category_choices()
|
||||
|
||||
if upload_request.method == 'POST' and form.validate():
|
||||
torrent = backend.handle_torrent_upload(form, user, True)
|
||||
|
||||
return flask.make_response(flask.jsonify({'Success': int('{0}'.format(torrent.id))}), 200)
|
||||
else:
|
||||
return_error_messages = []
|
||||
for error_name, error_messages in form.errors.items():
|
||||
return_error_messages.extend(error_messages)
|
||||
|
||||
return flask.make_response(flask.jsonify({'Failure': return_error_messages}), 400)
|
||||
|
||||
# V2 below
|
||||
|
||||
|
||||
# Map UploadForm fields to API keys
|
||||
UPLOAD_API_FORM_KEYMAP = {
|
||||
@@ -150,6 +73,7 @@ UPLOAD_API_DEFAULTS = {
|
||||
}
|
||||
|
||||
|
||||
@api_blueprint.route('/upload', methods=['POST'])
|
||||
@api_blueprint.route('/v2/upload', methods=['POST'])
|
||||
@basic_auth_user
|
||||
@api_require_user
|
||||
@@ -170,7 +94,7 @@ def v2_api_upload():
|
||||
|
||||
# Flask-WTF (very helpfully!!) automatically grabs the request form, so force a None formdata
|
||||
upload_form = forms.UploadForm(None, data=mapped_dict, meta={'csrf': False})
|
||||
upload_form.category.choices = _create_upload_category_choices()
|
||||
upload_form.category.choices = routes._create_upload_category_choices()
|
||||
|
||||
if upload_form.validate():
|
||||
torrent = backend.handle_torrent_upload(upload_form, flask.g.user)
|
||||
|
||||
@@ -133,9 +133,6 @@ def get_category_id_map():
|
||||
# Routes start here #
|
||||
|
||||
|
||||
app.register_blueprint(api_handler.api_blueprint, url_prefix='/api')
|
||||
|
||||
|
||||
def chain_get(source, *args):
|
||||
''' Tries to return values from source by the given keys.
|
||||
Returns None if none match.
|
||||
@@ -767,10 +764,5 @@ def site_help():
|
||||
|
||||
|
||||
# #################################### API ROUTES ####################################
|
||||
@app.route('/api/upload', methods=['POST'])
|
||||
def api_upload():
|
||||
is_valid_user, user, debug = api_handler.validate_user(flask.request)
|
||||
if not is_valid_user:
|
||||
return flask.make_response(flask.jsonify({"Failure": "Invalid username or password."}), 400)
|
||||
api_response = api_handler.api_upload(flask.request, user)
|
||||
return api_response
|
||||
|
||||
app.register_blueprint(api_handler.api_blueprint, url_prefix='/api')
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
{% block body %}
|
||||
|
||||
<div class="alert alert-info">
|
||||
<p><strong>5/21 Update:</strong> We've updated our upload API to v2 (v1 still works). See documentation <b><a href="https://github.com/nyaadevs/nyaa/blob/master/utils/api_uploader_v2.py">here</a></b>.</p>
|
||||
<p><strong>5/17 Update:</strong> We've added faster and more accurate search! In addition to your typical keyword search in both English and other languages, you can also now use powerful operators
|
||||
<p><strong>2017-05-22 Update:</strong> We've updated our upload API to v2 (v1 <b>is now disabled!</b>). See documentation <b><a href="https://github.com/nyaadevs/nyaa/blob/master/utils/api_uploader_v2.py">here</a></b>.</p>
|
||||
<p><strong>2017-05-17 Update:</strong> We've added faster and more accurate search! In addition to your typical keyword search in both English and other languages, you can also now use powerful operators
|
||||
like <kbd>clockwork planet -horrible</kbd> or <kbd>commie|horrible|cartel yowamushi</kbd> to search. For all supported operators, please click <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html#_simple_query_string_syntax">here</a>. More features are coming soon!</p><br>
|
||||
<p>We welcome you to provide feedback at <a href="irc://irc.rizon.net/nyaa-dev">#nyaa-dev@irc.rizon.net</a></p>
|
||||
<p>Our GitHub: <a href="https://github.com/nyaadevs" target="_blank">https://github.com/nyaadevs</a> - creating <a href="https://github.com/nyaadevs/nyaa/issues">issues</a> for features and faults is recommendable!</p>
|
||||
|
||||
Reference in New Issue
Block a user