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