Skip to content

Commit

Permalink
Refactor magnet link creation and testing
Browse files Browse the repository at this point in the history
Magnet link creation logic has been refactored for better clarity and maintainability. The 'create_magnet' method from the DownloadsDetailsTabWidget class was removed, and its functionality was integrated into the 'compose_magnetlink' function in utilities.py. This change simplifies the codebase by centralizing magnet link generation in one place.

The tests for creating magnet links were also moved from test_downloadsdetailstabwidget.py to test_utilities.py to reflect this change. Additional checks were added to ensure that invalid URLs are properly handled during magnet link creation.

In addition, type hints were added to the 'compose_magnetlink' function for improved readability and understanding of expected input types.
  • Loading branch information
drew2a committed May 6, 2024
1 parent 07c9950 commit 8c039a4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 68 deletions.
32 changes: 0 additions & 32 deletions src/tribler/gui/tests/test_downloadsdetailstabwidget.py

This file was deleted.

42 changes: 35 additions & 7 deletions src/tribler/gui/tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions src/tribler/gui/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:<infohash>&dn=<name>[&tr=<tracker>]
Expand All @@ -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


Expand Down
30 changes: 5 additions & 25 deletions src/tribler/gui/widgets/downloadsdetailstabwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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', [])
Expand Down

0 comments on commit 8c039a4

Please sign in to comment.