1
1
from __future__ import annotations
2
2
3
- import json
4
3
import logging
5
4
from asyncio .exceptions import TimeoutError as AsyncTimeoutError
6
5
from binascii import hexlify , unhexlify
7
- from copy import deepcopy
8
6
from ssl import SSLError
9
- from typing import TYPE_CHECKING
7
+ from typing import TYPE_CHECKING , TypedDict
10
8
11
9
import libtorrent as lt
12
10
from aiohttp import (
46
44
logger = logging .getLogger (__name__ )
47
45
48
46
47
+ class JSONMiniFileInfo (TypedDict ):
48
+ """
49
+ A minimal JSON dict to describe file info.
50
+ """
51
+
52
+ index : int
53
+ name : str
54
+ size : int
55
+
56
+
49
57
def recursive_unicode (obj : Iterable , ignore_errors : bool = False ) -> Iterable :
50
58
"""
51
59
Converts any bytes within a data structure to unicode strings. Bytes are assumed to be UTF-8 encoded text.
@@ -102,6 +110,14 @@ def __init__(self, download_manager: DownloadManager) -> None:
102
110
self .app .add_routes ([web .get ("" , self .get_torrent_info ),
103
111
web .put ("" , self .get_torrent_info_from_file )])
104
112
113
+ def get_files (self , tdef : TorrentDef ) -> list [JSONMiniFileInfo ]:
114
+ """
115
+ Get a list of files from the given torrent definition.
116
+ """
117
+ remapped_indices = tdef .get_file_indices ()
118
+ return [{"index" : remapped_indices [i ], "name" : str (f [0 ]), "size" : f [1 ]}
119
+ for i , f in enumerate (tdef .get_files_with_length ())]
120
+
105
121
@docs (
106
122
tags = ["Libtorrent" ],
107
123
summary = "Return metainfo from a torrent found at a provided URI." ,
@@ -269,13 +285,8 @@ async def get_torrent_info(self, request: Request) -> RESTResponse: # noqa: C90
269
285
metainfo_download = metainfo_lookup .download if metainfo_lookup else None
270
286
download_is_metainfo_request = download == metainfo_download
271
287
272
- # Check if the torrent is already in the downloads
273
- encoded_metainfo = deepcopy (metainfo )
274
-
275
- ready_for_unicode = recursive_unicode (encoded_metainfo , ignore_errors = True )
276
- json_dump = json .dumps (ready_for_unicode , ensure_ascii = False )
277
-
278
- return RESTResponse ({"metainfo" : hexlify (json_dump .encode ()).decode (),
288
+ return RESTResponse ({"files" : self .get_files (torrent_def ),
289
+ "name" : torrent_def .get_name_utf8 (),
279
290
"download_exists" : download and not download_is_metainfo_request ,
280
291
"valid_certificate" : valid_cert })
281
292
@@ -302,8 +313,7 @@ async def get_torrent_info_from_file(self, request: web.Request) -> RESTResponse
302
313
metainfo_download = metainfo_lookup .download if metainfo_lookup else None
303
314
requesting_metainfo = download == metainfo_download
304
315
305
- metainfo_unicode = recursive_unicode (deepcopy (tdef .get_metainfo ()), ignore_errors = True )
306
- metainfo_json = json .dumps (metainfo_unicode , ensure_ascii = False )
307
316
return RESTResponse ({"infohash" : hexlify (infohash ).decode (),
308
- "metainfo" : hexlify (metainfo_json .encode ('utf-8' )).decode (),
317
+ "files" : self .get_files (tdef ),
318
+ "name" : tdef .get_name_utf8 (),
309
319
"download_exists" : download and not requesting_metainfo })
0 commit comments