qutebrowser: Try to fix medium redirects

This commit is contained in:
Marty Oehme 2023-08-29 22:45:09 +02:00
parent cf0544b0a5
commit 6e8132d311
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -1,18 +1,31 @@
import random import random
from urllib import parse
import re import re
from qutebrowser.api import interceptor from qutebrowser.api import interceptor
from qutebrowser.extensions.interceptors import RedirectException from qutebrowser.extensions.interceptors import QUrl, RedirectException
from qutebrowser.utils import message from qutebrowser.utils import message
def fixScribePath(url):
""" Fix external medium blog to scribe translation. def fixScribePath(url: QUrl):
"""Fix external medium blog to scribe translation.
Some paths from medium will go through a 'global identity' Some paths from medium will go through a 'global identity'
path which messes up the actual url path we want to go path which messes up the actual url path we want to go
to and puts it in queries. This puts it back on the path. to and puts it in queries. This puts it back on the path.
""" """
new_path = f"{url.path()}{url.query()}" # double unquoting necessary!
url.setQuery("") # I suppose we double-wrap it earlier somewhere?
url.setPath(re.sub(r"m/global-identity-2redirectUrl=", "", new_path)) # unquoted = parse.unquote(
# url.path(options=QUrl.ComponentFormattingOption.FullyEncoded)
# )
path = parse.unquote(f"{url.path()}{url.query()}", encoding='ascii')
url.setQuery(None)
new_path = re.sub(r"m/global-identity-2redirectUrl=", "", path)
url.setPath(
parse.quote(new_path),
mode=QUrl.ParsingMode.StrictMode,
)
return url
redirects = { redirects = {
"youtube": { "youtube": {
@ -26,7 +39,6 @@ redirects = {
"yt.funami.tech", "yt.funami.tech",
"iv.melmac.space", "iv.melmac.space",
"invidious.silur.me", "invidious.silur.me",
"inv.riverside.rocks",
"invidious.lidarshield.cloud", "invidious.lidarshield.cloud",
"invidious.flokinet.to", "invidious.flokinet.to",
"invidious.snopyta.org", "invidious.snopyta.org",
@ -58,7 +70,6 @@ redirects = {
"source": ["reddit.com"], "source": ["reddit.com"],
"target": [ "target": [
"td.vern.cc", "td.vern.cc",
"teddit.adminforge.de",
"teddit.artemislena.eu", "teddit.artemislena.eu",
"teddit.bus-hit.me", "teddit.bus-hit.me",
"teddit.hostux.net", "teddit.hostux.net",
@ -66,7 +77,6 @@ redirects = {
"teddit.net", "teddit.net",
"teddit.pussthecat.org", "teddit.pussthecat.org",
"teddit.sethforprivacy.com", "teddit.sethforprivacy.com",
"teddit.totaldarkness.net",
"teddit.zaggy.nl", "teddit.zaggy.nl",
], ],
}, },
@ -155,14 +165,11 @@ redirects = {
"source": ["medium.com"], "source": ["medium.com"],
"target": [ "target": [
"scribe.rip", "scribe.rip",
"scribe.nixnet.services",
"scribe.citizen4.eu", "scribe.citizen4.eu",
"scribe.bus-hit.me", "scribe.bus-hit.me",
"scribe.froth.zone",
"scribe.privacydev.net",
"sc.vern.cc", "sc.vern.cc",
], ],
"postprocess": fixScribePath "postprocess": fixScribePath,
}, },
"google": { "google": {
"source": ["google.com"], "source": ["google.com"],
@ -199,22 +206,24 @@ def rewrite(request: interceptor.Request):
url = request.request_url url = request.request_url
for service in redirects.values(): if service := _should_be_redirected(url.host()):
matched = False # TODO integrate pinging and always surf to fastest?
for source in service["source"]: target = service["target"][random.randint(0, len(service["target"]) - 1)]
if re.search(source, url.host()): if target is not None and url.setHost(target) is not False:
matched = True if "postprocess" in service:
url = service["postprocess"](url)
try:
request.redirect(url)
except RedirectException as e:
message.error(str(e))
if matched:
target = service["target"][random.randint(0, len(service["target"]) - 1)] def _should_be_redirected(host: str, redirects: dict = redirects) -> dict | None:
if target is not None and url.setHost(target) is not False: for service in redirects.values():
if "postprocess" in service: for source in service["source"]:
service["postprocess"](url) if re.search(source, host):
try: return service
request.redirect(url) return None
except RedirectException as e:
message.error(str(e))
break
interceptor.register(rewrite) interceptor.register(rewrite)