diff --git a/src/tribler/gui/tests/test_downloadsdetailstabwidget.py b/src/tribler/gui/tests/test_downloadsdetailstabwidget.py deleted file mode 100644 index 6e6541738a..0000000000 --- a/src/tribler/gui/tests/test_downloadsdetailstabwidget.py +++ /dev/null @@ -1,32 +0,0 @@ -from tribler.gui.widgets.downloadsdetailstabwidget import DownloadsDetailsTabWidget - - -def test_create_magnet_no_input(): - # Test that the create_magnet method returns an empty string when no input is provided - magnet = DownloadsDetailsTabWidget.create_magnet(None, None, []) - assert magnet == '' - - -def test_create_magnet_no_trackers(): - # Test that the create_magnet method returns a magnet link without trackers when no trackers are provided - magnet = DownloadsDetailsTabWidget.create_magnet('infohash', 'name', []) - assert magnet == 'magnet:?xt=urn:btih:infohash&dn=name' - - -def test_create_magnet_no_urls(): - # Test that the create_magnet method returns a magnet link without trackers when no urls are provided - magnet = DownloadsDetailsTabWidget.create_magnet('infohash', 'name', [{}, {'url': 'tracker'}]) - assert magnet == 'magnet:?xt=urn:btih:infohash&dn=name&tr=tracker' - - -def test_create_magnet_invalid_urls(): - # Test that the create_magnet method returns a magnet link only for trackers when valid urls - trackers = [{'url': '[PeX]'}, {'url': '[DHT]'}, {'url': 'tracker'}] - magnet = DownloadsDetailsTabWidget.create_magnet('infohash', 'name', trackers) - assert magnet == 'magnet:?xt=urn:btih:infohash&dn=name&tr=tracker' - - -def test_create_magnet_multiple_trackers(): - # Test that the create_magnet method returns a magnet link with multiple trackers - magnet = DownloadsDetailsTabWidget.create_magnet('infohash', 'name', [{'url': 'tracker1'}, {'url': 'tracker2'}]) - assert magnet == 'magnet:?xt=urn:btih:infohash&dn=name&tr=tracker1&tr=tracker2' diff --git a/src/tribler/gui/tests/test_utilities.py b/src/tribler/gui/tests/test_utilities.py index bfe9cc689d..3f90e6eb63 100644 --- a/src/tribler/gui/tests/test_utilities.py +++ b/src/tribler/gui/tests/test_utilities.py @@ -93,27 +93,55 @@ def test_quote_plus_unicode_compound(): def test_compose_magnetlink(): infohash = "DC4B96CF85A85CEEDB8ADC4B96CF85A85CEEDB8A" name = "Some torrent name" - trackers = ['http://tracker1.example.com:8080/announce', 'http://tracker1.example.com:8080/announce'] + trackers = [ + {'url': 'http://tracker1.example.com:8080/announce'}, + {'url': 'http://tracker1.example.com:8080/announce'}, + ] - expected_link0 = "" expected_link1 = "magnet:?xt=urn:btih:DC4B96CF85A85CEEDB8ADC4B96CF85A85CEEDB8A" - expected_link2 = "magnet:?xt=urn:btih:DC4B96CF85A85CEEDB8ADC4B96CF85A85CEEDB8A&dn=Some+torrent+name" expected_link3 = ( "magnet:?xt=urn:btih:DC4B96CF85A85CEEDB8ADC4B96CF85A85CEEDB8A&dn=Some+torrent+name" "&tr=http://tracker1.example.com:8080/announce&tr=http://tracker1.example.com:8080/announce" ) - composed_link0 = compose_magnetlink(None) composed_link1 = compose_magnetlink(infohash) - composed_link2 = compose_magnetlink(infohash, name=name) composed_link3 = compose_magnetlink(infohash, name=name, trackers=trackers) - assert composed_link0 == expected_link0 assert composed_link1 == expected_link1 - assert composed_link2 == expected_link2 assert composed_link3 == expected_link3 +def test_create_magnet_no_input(): + # Test that the create_magnet method returns an empty string when no input is provided + magnet = compose_magnetlink(None, None, []) + assert magnet == '' + + +def test_create_magnet_no_trackers(): + # Test that the create_magnet method returns a magnet link without trackers when no trackers are provided + magnet = compose_magnetlink('infohash', 'name', []) + assert magnet == 'magnet:?xt=urn:btih:infohash&dn=name' + + +def test_create_magnet_no_urls(): + # Test that the create_magnet method returns a magnet link without trackers when no urls are provided + magnet = compose_magnetlink('infohash', 'name', [{}, {'url': 'tracker'}]) + assert magnet == 'magnet:?xt=urn:btih:infohash&dn=name&tr=tracker' + + +def test_create_magnet_invalid_urls(): + # Test that the create_magnet method returns a magnet link only for trackers when valid urls + trackers = [{'url': '[PeX]'}, {'url': '[DHT]'}, {'url': 'tracker'}] + magnet = compose_magnetlink('infohash', 'name', trackers) + assert magnet == 'magnet:?xt=urn:btih:infohash&dn=name&tr=tracker' + + +def test_create_magnet_multiple_trackers(): + # Test that the create_magnet method returns a magnet link with multiple trackers + magnet = compose_magnetlink('infohash', 'name', [{'url': 'tracker1'}, {'url': 'tracker2'}]) + assert magnet == 'magnet:?xt=urn:btih:infohash&dn=name&tr=tracker1&tr=tracker2' + + def test_is_dict_has(): assert not dict_item_is_any_of(None, None, None) assert not dict_item_is_any_of({}, None, None) diff --git a/src/tribler/gui/utilities.py b/src/tribler/gui/utilities.py index 0fdc1513f4..8a53941f77 100644 --- a/src/tribler/gui/utilities.py +++ b/src/tribler/gui/utilities.py @@ -352,7 +352,7 @@ def get_health(seeders, leechers, last_tracker_check): return HEALTH_DEAD -def compose_magnetlink(infohash=None, name=None, trackers=None): +def compose_magnetlink(infohash:Optional[str]=None, name:Optional[str]=None, trackers:Optional[List[Dict]]=None): """ Composes magnet link from infohash, display name and trackers. The format is: magnet:?xt=urn:btih:&dn=[&tr=] @@ -367,9 +367,16 @@ def compose_magnetlink(infohash=None, name=None, trackers=None): magnet = f"magnet:?xt=urn:btih:{infohash}" if name: magnet = f"{magnet}&dn={quote_plus_unicode(name)}" - if trackers and isinstance(trackers, list): - for tracker in trackers: - magnet = f"{magnet}&tr={tracker}" + + if not trackers: + return magnet + + invalid_urls = {'[DHT]', '[PeX]'} + urls = (t.get('url') for t in trackers) + valid_urls = [u for u in urls if u and u not in invalid_urls] + + for url in valid_urls: + magnet = f"{magnet}&tr={url}" return magnet diff --git a/src/tribler/gui/widgets/downloadsdetailstabwidget.py b/src/tribler/gui/widgets/downloadsdetailstabwidget.py index d47f0e276e..a80c1a3ad9 100644 --- a/src/tribler/gui/widgets/downloadsdetailstabwidget.py +++ b/src/tribler/gui/widgets/downloadsdetailstabwidget.py @@ -2,7 +2,7 @@ import operator from enum import IntEnum from pathlib import PurePosixPath -from typing import Dict, List, Optional +from typing import Dict, Optional from PyQt5.QtCore import QTimer, Qt from PyQt5.QtWidgets import QTabWidget, QTreeWidgetItem @@ -198,32 +198,12 @@ def update_pages(self, new_download=False): item = QTreeWidgetItem(self.window().download_peers_list) DownloadsDetailsTabWidget.update_peer_row(item, peer) - @staticmethod - def create_magnet(infohash: Optional[str], name: Optional[str], trackers: List[Dict]) -> str: - """ - Create a magnet link from the given infohash, name and list of trackers. - Args: - infohash: The infohash of the torrent. - name: The name of the torrent. - trackers: The list of trackers. - - Returns: - The magnet link. - """ - invalid_urls = {'[DHT]', '[PeX]'} - urls = (t.get('url') for t in trackers) - valid_urls = [u for u in urls if u and u not in invalid_urls] - - magnet = compose_magnetlink( - infohash=infohash, - name=name, - trackers=valid_urls - ) - return magnet - def on_copy_magnet_clicked(self, _): """ Copy the magnet link of the current download to the clipboard.""" - magnet = self.create_magnet( + if not self.current_download: + return + + magnet = compose_magnetlink( infohash=self.current_download.get('infohash'), name=self.current_download.get('name'), trackers=self.current_download.get('trackers', [])