qutebrowser: Try to fix medium redirects
This commit is contained in:
parent
cf0544b0a5
commit
6e8132d311
1 changed files with 37 additions and 28 deletions
|
@ -1,18 +1,31 @@
|
|||
import random
|
||||
from urllib import parse
|
||||
import re
|
||||
from qutebrowser.api import interceptor
|
||||
from qutebrowser.extensions.interceptors import RedirectException
|
||||
from qutebrowser.extensions.interceptors import QUrl, RedirectException
|
||||
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'
|
||||
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.
|
||||
"""
|
||||
new_path = f"{url.path()}{url.query()}"
|
||||
url.setQuery("")
|
||||
url.setPath(re.sub(r"m/global-identity-2redirectUrl=", "", new_path))
|
||||
# double unquoting necessary!
|
||||
# I suppose we double-wrap it earlier somewhere?
|
||||
# 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 = {
|
||||
"youtube": {
|
||||
|
@ -26,7 +39,6 @@ redirects = {
|
|||
"yt.funami.tech",
|
||||
"iv.melmac.space",
|
||||
"invidious.silur.me",
|
||||
"inv.riverside.rocks",
|
||||
"invidious.lidarshield.cloud",
|
||||
"invidious.flokinet.to",
|
||||
"invidious.snopyta.org",
|
||||
|
@ -58,7 +70,6 @@ redirects = {
|
|||
"source": ["reddit.com"],
|
||||
"target": [
|
||||
"td.vern.cc",
|
||||
"teddit.adminforge.de",
|
||||
"teddit.artemislena.eu",
|
||||
"teddit.bus-hit.me",
|
||||
"teddit.hostux.net",
|
||||
|
@ -66,7 +77,6 @@ redirects = {
|
|||
"teddit.net",
|
||||
"teddit.pussthecat.org",
|
||||
"teddit.sethforprivacy.com",
|
||||
"teddit.totaldarkness.net",
|
||||
"teddit.zaggy.nl",
|
||||
],
|
||||
},
|
||||
|
@ -155,14 +165,11 @@ redirects = {
|
|||
"source": ["medium.com"],
|
||||
"target": [
|
||||
"scribe.rip",
|
||||
"scribe.nixnet.services",
|
||||
"scribe.citizen4.eu",
|
||||
"scribe.bus-hit.me",
|
||||
"scribe.froth.zone",
|
||||
"scribe.privacydev.net",
|
||||
"sc.vern.cc",
|
||||
],
|
||||
"postprocess": fixScribePath
|
||||
"postprocess": fixScribePath,
|
||||
},
|
||||
"google": {
|
||||
"source": ["google.com"],
|
||||
|
@ -199,22 +206,24 @@ def rewrite(request: interceptor.Request):
|
|||
|
||||
url = request.request_url
|
||||
|
||||
for service in redirects.values():
|
||||
matched = False
|
||||
for source in service["source"]:
|
||||
if re.search(source, url.host()):
|
||||
matched = True
|
||||
if service := _should_be_redirected(url.host()):
|
||||
# TODO integrate pinging and always surf to fastest?
|
||||
target = service["target"][random.randint(0, len(service["target"]) - 1)]
|
||||
if target is not None and url.setHost(target) is not False:
|
||||
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)]
|
||||
if target is not None and url.setHost(target) is not False:
|
||||
if "postprocess" in service:
|
||||
service["postprocess"](url)
|
||||
try:
|
||||
request.redirect(url)
|
||||
except RedirectException as e:
|
||||
message.error(str(e))
|
||||
break
|
||||
|
||||
def _should_be_redirected(host: str, redirects: dict = redirects) -> dict | None:
|
||||
for service in redirects.values():
|
||||
for source in service["source"]:
|
||||
if re.search(source, host):
|
||||
return service
|
||||
return None
|
||||
|
||||
|
||||
interceptor.register(rewrite)
|
||||
|
|
Loading…
Reference in a new issue