From 0e5348a2a6c4d5cf8b1d83aca5e6e92a7e6afd8f Mon Sep 17 00:00:00 2001 From: kyamiko Date: Mon, 22 May 2017 22:41:52 -0400 Subject: [PATCH] Requested changes for api info --- nyaa/api_handler.py | 20 +++++++++++++------- utils/api_info_v2.py | 30 ++++-------------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/nyaa/api_handler.py b/nyaa/api_handler.py index d0e8909c..a370ed75 100644 --- a/nyaa/api_handler.py +++ b/nyaa/api_handler.py @@ -284,21 +284,24 @@ def ghetto_import(): @basic_auth_user @api_require_user def v2_api_info(torrent_id_or_hash): - torrent = None - viewer = flask.g.user + torrent_id_or_hash = torrent_id_or_hash.lower().strip() matchID = re.match(ID_PATTERN, torrent_id_or_hash) matchHASH = re.match(INFO_HASH_PATTERN, torrent_id_or_hash) + torrent = None + if matchID: - torrent = models.Torrent.by_id(torrent_id_or_hash) + torrent = models.Torrent.by_id(int(torrent_id_or_hash)) elif matchHASH: - # Convert the string representation of a torrent hash into binary + # Convert the string representation of a torrent hash back into a binary representation a2b_hash = binascii.unhexlify(torrent_id_or_hash) torrent = models.Torrent.by_info_hash(a2b_hash) else: return flask.jsonify({'errors': ['Query was not a valid id or hash.']}), 400 + viewer = flask.g.user + if not torrent: return flask.jsonify({'errors': ['Query was not a valid id or hash.']}), 400 @@ -306,13 +309,13 @@ def v2_api_info(torrent_id_or_hash): if torrent.deleted and not (viewer and viewer.is_superadmin): return flask.jsonify({'errors': ['Query was not a valid id or hash.']}), 400 - submitter = 'anonymous' + submitter = None if not torrent.anonymous and torrent.user: submitter = torrent.user.username if torrent.user and (viewer == torrent.user or viewer.is_moderator): submitter = torrent.user.username - files = '' + files = {} if torrent.filelist: files = json.loads(torrent.filelist.filelist_blob.decode('utf-8')) @@ -322,10 +325,13 @@ def v2_api_info(torrent_id_or_hash): 'url': flask.url_for('view_torrent', torrent_id=torrent.id, _external=True), 'id': torrent.id, 'name': torrent.display_name, - 'hash': torrent.info_hash.hex(), + 'hash_b32': torrent.info_hash_as_b32, #as used in magnet uri + 'hash_hex': torrent.info_hash.hex(), #as shown in torrent client 'magnet': torrent.magnet_uri, 'main_category': torrent.main_category.name, + 'main_category_id': torrent.main_category.id, 'sub_category': torrent.sub_category.name, + 'sub_category_id': torrent.sub_category.id, 'information': torrent.information, 'description': torrent.description, 'stats': {'seeders': torrent.stats.seed_count, 'leechers': torrent.stats.leech_count, 'downloads': torrent.stats.download_count}, diff --git a/utils/api_info_v2.py b/utils/api_info_v2.py index f0c798a7..6daba9ab 100644 --- a/utils/api_info_v2.py +++ b/utils/api_info_v2.py @@ -28,13 +28,7 @@ conn_group.add_argument('-p', '--password', help='Password') conn_group.add_argument('--host', help='Select another api host (for debugging purposes)') -resp_group = parser.add_argument_group('Response options') - -resp_group.add_argument('--raw', default=False, action='store_true', help='Print only raw response (JSON)') - -info_group = parser.add_argument_group('Info options') - -info_group.add_argument('-q', '--query', required=True, help='Torrent by id or hash Required.') +parser.add_argument('-q', '--query', required=True, help='Torrent by id or hash Required.') if __name__ == '__main__': args = parser.parse_args() @@ -49,7 +43,7 @@ matchID = re.match(ID_PATTERN, api_query) matchHASH = re.match(INFO_HASH_PATTERN, api_query) - if (not matchID) and (not matchHASH): + if not (matchID or matchHASH): raise Exception('Query was not a valid id or valid hash.') @@ -65,21 +59,5 @@ # Go! r = requests.get(api_info_url, auth=auth) - response = None - - if args.raw: - print(r.text) - else: - try: - response = r.json() - except ValueError: - print('Bad response:') - print(r.text) - exit(1) - - errors = response.get('errors') - if errors: - print('Query failed', errors) - exit(1) - else: - print(response) + + print(r.text)