Skip to content

Commit

Permalink
Made TorrentDefNoMetainfo inherit from TorrentDef (#7758)
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink committed Dec 6, 2023
1 parent 795583c commit 33d247a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ def test_torrent_no_metainfo():
assert not tdef.is_private()
assert tdef.get_name_utf8() == "video.avi"
assert tdef.get_nr_pieces() == 0
assert tdef.torrent_info is None
tdef.load_torrent_info()
assert tdef.torrent_info is None

torrent2 = TorrentDefNoMetainfo(b"12345678901234567890", VIDEO_FILE_NAME, "magnet:")
assert len(torrent2.get_trackers()) == 0
Expand Down
69 changes: 20 additions & 49 deletions src/tribler/core/components/libtorrent/torrentdef.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Author(s): Arno Bakker
"""
from __future__ import annotations

import itertools
import logging
from asyncio import get_running_loop
Expand Down Expand Up @@ -520,68 +522,37 @@ def get_index_of_file_in_files(self, file):
raise ValueError("File not found in single-file torrent")


class TorrentDefNoMetainfo:
class TorrentDefNoMetainfo(TorrentDef):
"""
Instances of this class are used when working with a torrent def that contains no metainfo (yet), for instance,
when starting a download with only an infohash. Other methods that are using this class do not distinguish between
a TorrentDef with and without data and may still expect this class to have various methods in TorrentDef
implemented.
"""

def __init__(self, infohash, name, url=None):
assert isinstance(infohash, bytes), f"INFOHASH has invalid type: {type(infohash)}"
assert len(infohash) == INFOHASH_LENGTH, "INFOHASH has invalid length: %d" % len(infohash)
def __init__(self, infohash: bytes, name: bytes, url: bytes | None = None):
torrent_parameters = {
b'name': name
}
if url is not None:
torrent_parameters[b'urllist'] = [url]
super().__init__(torrent_parameters=torrent_parameters)
self.infohash = infohash
self.name = name
self.url = url

def get_name(self):
return self.name

def get_infohash(self):
return self.infohash

def get_length(self, selectedfiles=None): # pylint: disable=unused-argument
return 0

def get_metainfo(self):
def get_url(self) -> bytes | None:
if urllist := self.torrent_parameters.get(b'urllist'):
return urllist[0]
return None

def get_url(self):
return self.url
@property
def torrent_info(self) -> lt.torrent_info | None:
return None

def is_multifile_torrent(self):
return False
def load_torrent_info(self) -> None:
pass

def get_name_utf8(self):
"""
Not all names are utf-8, attempt to construct it as utf-8 anyway.
"""
return escape_as_utf8(self.name.encode('utf-8 ') if isinstance(self.name, str) else self.name)
return self.get_name()

def get_name_as_unicode(self):
return ensure_unicode(self.name, 'utf-8')

def get_files(self, exts=None):
return []

def get_files_with_length(self, exts=None):
return []

def get_trackers(self) -> set:
"""
Returns a flat set of all known trackers.
:return: all known trackers
:rtype: set
"""
if self.url and self.url.startswith('magnet:'):
trackers = parse_magnetlink(self.url)[2]
return set(trackers)
return set()

def is_private(self):
return False

def get_nr_pieces(self):
return 0
return self.get_name()

0 comments on commit 33d247a

Please sign in to comment.