Skip to content

Commit

Permalink
Requested changes for api info
Browse files Browse the repository at this point in the history
  • Loading branch information
kyamiko committed May 29, 2017
1 parent 3ee5780 commit 0e5348a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
20 changes: 13 additions & 7 deletions nyaa/api_handler.py
Expand Up @@ -284,35 +284,38 @@ 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

# Only allow admins see deleted torrents
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'))

Expand All @@ -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},
Expand Down
30 changes: 4 additions & 26 deletions utils/api_info_v2.py
Expand Up @@ -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()
Expand All @@ -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.')


Expand All @@ -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)

0 comments on commit 0e5348a

Please sign in to comment.