qutebrowser: Use fastside for redirects by default

Default to fastside.link instead of farside.link.
This commit is contained in:
Marty Oehme 2024-06-27 17:35:35 +02:00
parent 52c0260d6d
commit d99c908ac3
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -1,6 +1,6 @@
import random import random
import re import re
from typing import Any, Callable from typing import Callable
from urllib import parse from urllib import parse
from qutebrowser.api import interceptor from qutebrowser.api import interceptor
@ -29,7 +29,11 @@ def fixScribePath(url: QUrl):
return url return url
redirects = { type Service = dict[str, list[str]]
type Redirects = dict[str, Service]
redirects: Redirects = {
"youtube": { "youtube": {
"source": ["youtube.com"], "source": ["youtube.com"],
"farside": ["invidious"], "farside": ["invidious"],
@ -163,11 +167,9 @@ def rewrite(request: interceptor.Request) -> None:
url = request.request_url url = request.request_url
if service := _should_be_redirected(url.host()): if service := _should_be_redirected(url.host()):
if "farside" in service: url = _farside_redirect(
url = _farside_redirect(url, _pick_random(service["farside"])) url, _pick_random(service["farside" if "farside" in service else "target"])
else: )
srv = _pick_random(service["target"])
url = _target_redirect(url, srv)
try: try:
request.redirect(url) request.redirect(url)
except RedirectException as e: except RedirectException as e:
@ -177,28 +179,24 @@ def rewrite(request: interceptor.Request) -> None:
url = service["postprocess"](url) url = service["postprocess"](url)
def _farside_redirect(url: QUrl, service: str) -> QUrl: def _farside_redirect(url: QUrl, service: str, use_fastside: bool = True) -> QUrl:
try: try:
url.setHost("farside.link") url.setHost("fastside.link" if use_fastside else "farside.link")
url.setPath(f"/{service}{url.path()}") url.setPath(f"/{service}{url.path()}")
except RedirectException as e: except RedirectException as e:
message.error(str(e)) message.error(str(e))
return url return url
def _target_redirect(url: QUrl, target: str) -> QUrl: def _pick_random[T](choices: list[T]) -> T:
if target is not None and url.setHost(target) is not False:
return url
return url
def _pick_random(choices: list) -> Any:
return choices[random.randint(0, len(choices) - 1)] return choices[random.randint(0, len(choices) - 1)]
def _should_be_redirected( def _should_be_redirected(
host: str, redirects: dict = redirects # TODO: Update to use typedefs/classes instead of this jumble
) -> dict[str, list] | None: host: str,
redirects: Redirects = redirects,
) -> Service | None:
for service in redirects.values(): for service in redirects.values():
for source in service["source"]: for source in service["source"]:
if re.search(source, host): if re.search(source, host):