Browse Source

Remove Reddit's share-tracking query params

merge-requests/55/head
Deimos 6 years ago
parent
commit
ea41d1838a
  1. 8
      tildes/tests/test_url_transform.py
  2. 19
      tildes/tildes/lib/url_transform.py

8
tildes/tests/test_url_transform.py

@ -34,3 +34,11 @@ def test_other_mobile_subdomain_not_removed():
cleaned_url = apply_url_transformations(url)
assert cleaned_url == url
def test_reddit_tracking_removed():
"""Ensure that Reddit's "share tracking" query params are removed."""
url = "https://www.reddit.com/r/tildes/comments/8k14is/_/?sort=new&st=abcdefgh&sh=1234asd"
cleaned_url = apply_url_transformations(url)
assert cleaned_url == "https://www.reddit.com/r/tildes/comments/8k14is/_/?sort=new"

19
tildes/tildes/lib/url_transform.py

@ -129,3 +129,22 @@ class TwitterMobileConverter(UrlTransformer):
def apply_transformation(cls, parsed_url: ParseResult) -> ParseResult:
"""Apply the actual transformation process to the url."""
return parsed_url._replace(netloc="twitter.com")
class RedditTrackingRemover(UrlTransformer):
"""Remove Reddit's "share tracking" query parameters (st and sh)."""
@classmethod
def is_applicable(cls, parsed_url: ParseResult) -> bool:
"""Return whether this transformation should be applied to the url."""
return parsed_url.hostname == "www.reddit.com"
@classmethod
def apply_transformation(cls, parsed_url: ParseResult) -> ParseResult:
"""Apply the actual transformation process to the url."""
query_params = parse_qs(parsed_url.query)
query_params.pop("st", None)
query_params.pop("sh", None)
return parsed_url._replace(query=urlencode(query_params, doseq=True))
Loading…
Cancel
Save