Use scheme in alt replacement if defined

For users running local instances of service alternatives such as
invidious, the alt replacement procedure broke if the scheme of the
original service (almost always https) didn't match the scheme of their
defined local service (likely http).

This adds a small check to see if the alt has a defined scheme, and if
so, removes the original scheme for that result.

Fixes #806
This commit is contained in:
Ben Busby 2022-07-13 10:25:51 -06:00
parent 2a8519be30
commit 5db72a9552
No known key found for this signature in database
GPG Key ID: B9B7231E01D924A1

View File

@ -128,7 +128,8 @@ def get_site_alt(link: str) -> str:
"""
# Need to replace full hostname with alternative to encapsulate
# subdomains as well
hostname = urlparse.urlparse(link).hostname
parsed_link = urlparse.urlparse(link)
hostname = parsed_link.hostname
for site_key in SITE_ALTS.keys():
if not hostname or site_key not in hostname or not SITE_ALTS[site_key]:
@ -137,13 +138,22 @@ def get_site_alt(link: str) -> str:
# Wikipedia -> Wikiless replacements require the subdomain (if it's
# a 2-char language code) to be passed as a URL param to Wikiless
# in order to preserve the language setting.
url_params = ''
params = ''
if 'wikipedia' in hostname:
subdomain = hostname.split('.')[0]
if len(subdomain) == 2:
url_params = f'?lang={subdomain}'
params = f'?lang={subdomain}'
parsed_alt = urlparse.urlparse(SITE_ALTS[site_key])
link = link.replace(hostname, SITE_ALTS[site_key]) + params
# If a scheme is specified in the alternative, this results in a replaced
# link that looks like "https://http://altservice.tld". In this case, we
# can remove the original scheme from the result and use the one specified
# for the alt.
if parsed_alt.scheme:
link = '//'.join(link.split('//')[1:])
link = link.replace(hostname, SITE_ALTS[site_key]) + url_params
for prefix in SKIP_PREFIX:
link = link.replace(prefix, '//')
break