Skip to content

Commit

Permalink
Merge pull request #5288 from ichorid/add_direct_metainfo
Browse files Browse the repository at this point in the history
Load metainfo directly when unchecking 'anonymous' checkbox
  • Loading branch information
devos50 authored Apr 12, 2020
2 parents 5ce259d + 55431c7 commit c4f45a5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ async def get_metainfo(self, infohash, timeout=30, hops=None):
to a few peers, and downloading the metadata for the torrent.
:param infohash: The (binary) infohash to lookup metainfo for.
:param timeout: A timeout in seconds.
:param hops: the number of tunnel hops to use for this lookup. If None, use config default.
:return: The metainfo
"""
infohash_hex = hexlify(infohash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def verify_valid_dict(json_data):
path = "http://localhost:%d/ubuntu.torrent" % file_server_port
verify_valid_dict(await self.do_request('torrentinfo?uri=%s' % path, expected_code=200))

def get_metainfo(infohash, timeout=20):
hops_list = []

def get_metainfo(infohash, timeout=20, hops=None):
if hops is not None:
hops_list.append(hops)
with open(TESTS_DATA_DIR / "bak_single.torrent", mode='rb') as torrent_file:
torrent_data = torrent_file.read()
tdef = TorrentDef.load_from_memory(torrent_data)
Expand All @@ -92,6 +96,11 @@ def get_metainfo_timeout(*args, **kwargs):
self.session.dlmgr.get_metainfo = get_metainfo
verify_valid_dict(await self.do_request('torrentinfo?uri=%s' % path, expected_code=200))

await self.do_request('torrentinfo?uri=%s&hops=0' % path, expected_code=200)
self.assertListEqual([0], hops_list)

await self.do_request('torrentinfo?uri=%s&hops=foo' % path, expected_code=400)

path = 'http://fdsafksdlafdslkdksdlfjs9fsafasdf7lkdzz32.n38/324.torrent'
await self.do_request('torrentinfo?uri=%s' % path, expected_code=500)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ async def get_torrent_info(self, request):
"""

args = request.query

hops = None
if 'hops' in args:
try:
hops = int(args['hops'])
except ValueError:
return RESTResponse({"error": f"wrong value of 'hops' parameter: {repr(args['hops'])}"},
status=HTTP_BAD_REQUEST)

if 'uri' not in args or not args['uri']:
return RESTResponse({"error": "uri parameter missing"}, status=HTTP_BAD_REQUEST)

Expand All @@ -67,14 +76,14 @@ async def get_torrent_info(self, request):
if response.startswith(b'magnet'):
_, infohash, _ = parse_magnetlink(response)
if infohash:
metainfo = await self.session.dlmgr.get_metainfo(infohash, timeout=60)
metainfo = await self.session.dlmgr.get_metainfo(infohash, timeout=60, hops=hops)
else:
metainfo = bdecode_compat(response)
elif uri.startswith('magnet'):
infohash = parse_magnetlink(uri)[1]
if infohash is None:
return RESTResponse({"error": "missing infohash"}, status=HTTP_BAD_REQUEST)
metainfo = await self.session.dlmgr.get_metainfo(infohash, timeout=60)
metainfo = await self.session.dlmgr.get_metainfo(infohash, timeout=60, hops=hops)
else:
return RESTResponse({"error": "invalid uri"}, status=HTTP_BAD_REQUEST)

Expand Down
30 changes: 16 additions & 14 deletions src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self, parent, download_uri):
self.dialog_widget.select_all_files_button.clicked.connect(self.on_all_files_selected_clicked)
self.dialog_widget.deselect_all_files_button.clicked.connect(self.on_all_files_deselected_clicked)
self.dialog_widget.loading_files_label.clicked.connect(self.on_reload_torrent_info)
self.dialog_widget.anon_download_checkbox.clicked.connect(self.on_reload_torrent_info)

self.dialog_widget.destination_input.setStyleSheet(
"""
Expand Down Expand Up @@ -153,20 +154,22 @@ def get_selected_files(self):
return included_files

def perform_files_request(self):
self.rest_request = TriblerNetworkRequest(
"torrentinfo?uri=%s" % quote_plus_unicode(self.download_uri),
self.on_received_metainfo,
capture_errors=False,
)
direct = not self.dialog_widget.anon_download_checkbox.isChecked()
request = f"torrentinfo?uri={quote_plus_unicode(self.download_uri)}"
if direct is True:
request = request + f"&hops=0"
self.rest_request = TriblerNetworkRequest(request, self.on_received_metainfo, capture_errors=False)

if self.metainfo_retries <= METAINFO_MAX_RETRIES:
loading_message = (
"Loading torrent files..."
if not self.metainfo_retries
else "Timeout in fetching files. Retrying (%s/%s)" % (self.metainfo_retries, METAINFO_MAX_RETRIES)
fetch_mode = 'directly' if direct else 'anonymously'
loading_message = f"Loading torrent files {fetch_mode}..."
timeout_message = (
f"Timeout in fetching files {fetch_mode}. Retrying ({self.metainfo_retries}/{METAINFO_MAX_RETRIES})"
)
self.dialog_widget.loading_files_label.setText(loading_message)

self.dialog_widget.loading_files_label.setText(
loading_message if not self.metainfo_retries else timeout_message
)
self.metainfo_fetch_timer = QTimer()
self.metainfo_fetch_timer.timeout.connect(self.perform_files_request)
self.metainfo_fetch_timer.setSingleShot(True)
Expand Down Expand Up @@ -230,10 +233,9 @@ def on_reload_torrent_info(self):
This method is called when user clicks the QLabel text showing loading or error message. Here, we reset
the number of retries to fetch the metainfo. Note color of QLabel is also reset to white.
"""
if self.metainfo_retries > METAINFO_MAX_RETRIES:
self.dialog_widget.loading_files_label.setStyleSheet("color:#ffffff;")
self.metainfo_retries = 0
self.perform_files_request()
self.dialog_widget.loading_files_label.setStyleSheet("color:#ffffff;")
self.metainfo_retries = 0
self.perform_files_request()

def on_browse_dir_clicked(self):
chosen_dir = QFileDialog.getExistingDirectory(
Expand Down

0 comments on commit c4f45a5

Please sign in to comment.