diff --git a/tildes/tests/test_url_transform.py b/tildes/tests/test_url_transform.py index 7b1a976..470f77b 100644 --- a/tildes/tests/test_url_transform.py +++ b/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" diff --git a/tildes/tildes/lib/url_transform.py b/tildes/tildes/lib/url_transform.py index c5b69a7..094bcb5 100644 --- a/tildes/tildes/lib/url_transform.py +++ b/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))