Skip to content

Commit

Permalink
Cleaned up api/info
Browse files Browse the repository at this point in the history
  • Loading branch information
kyamiko committed May 31, 2017
1 parent 783bf0d commit 807eb9b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
11 changes: 6 additions & 5 deletions nyaa/api_handler.py
Expand Up @@ -325,8 +325,9 @@ 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_b32': torrent.info_hash_as_b32, #as used in magnet uri
'hash_hex': torrent.info_hash_as_hex, #.hex(), #as shown in torrent client
'creation_date': torrent.created_time.strftime('%Y-%m-%d %H:%M UTC'),
'hash_b32': torrent.info_hash_as_b32, # as used in magnet uri
'hash_hex': torrent.info_hash_as_hex, # .hex(), #as shown in torrent client
'magnet': torrent.magnet_uri,
'main_category': torrent.main_category.name,
'main_category_id': torrent.main_category.id,
Expand All @@ -338,9 +339,9 @@ def v2_api_info(torrent_id_or_hash):
'filesize': torrent.filesize,
'files': files,
# reduce torrent flags to True/False
'is_trusted': True if torrent.trusted else False,
'is_complete': True if torrent.complete else False,
'is_remake': True if torrent.remake else False
'is_trusted': torrent.trusted,
'is_complete': torrent.complete,
'is_remake': torrent.remake
}

return flask.jsonify(torrent_metadata), 200
15 changes: 14 additions & 1 deletion utils/api_info_v2.py
Expand Up @@ -33,6 +33,13 @@

parser.add_argument('--raw', default=False, action='store_true', help='Print only raw response (JSON)')

def easy_file_size(filesize):
for prefix in ['B','KiB','MiB','GiB','TiB']:
if filesize < 1024.0:
return '{0:.1f} {1}'.format(filesize, prefix)
filesize = filesize / 1024.0
return '{0:.1f} {1}'.format(filesize, prefix)

if __name__ == '__main__':
args = parser.parse_args()

Expand Down Expand Up @@ -79,4 +86,10 @@
print('Info request failed:', errors)
exit(1)
else:
print("Torrent #{} '{}' uploaded by '{}' ({} bytes) {} / {}\n{}".format(rj['id'], rj['name'], rj['submitter'], rj['filesize'], rj['main_category'], rj['sub_category'], rj['magnet']))
rj['filesize'] = easy_file_size(rj['filesize'])
rj['is_trusted'] = 'Yes' if rj['is_trusted'] else 'No'
rj['is_complete'] = 'Yes' if rj['is_complete'] else 'No'
rj['is_remake'] = 'Yes' if rj['is_remake'] else 'No'
print("Torrent #{} '{}' uploaded by '{}' ({}) (Created on: {}) ({} - {}) (Trusted: {}, Complete: {}, Remake: {})\n{}".format(
rj['id'], rj['name'], rj['submitter'], rj['filesize'], rj['creation_date'], rj['main_category'], rj['sub_category'],
rj['is_trusted'], rj['is_complete'], rj['is_remake'], rj['magnet']))

0 comments on commit 807eb9b

Please sign in to comment.