From bbf3ffb9ac7bb69e4191dcee3bd6fbe9db6c5583 Mon Sep 17 00:00:00 2001 From: Deimos Date: Fri, 4 Jan 2019 21:49:17 -0700 Subject: [PATCH] Convert mobile Twitter links to standard ones This is the first url transformation that only applies to a specific site (and domain). --- tildes/tests/test_url_transform.py | 16 ++++++++++++++++ tildes/tildes/lib/url_transform.py | 14 ++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/tildes/tests/test_url_transform.py b/tildes/tests/test_url_transform.py index 833a053..7b1a976 100644 --- a/tildes/tests/test_url_transform.py +++ b/tildes/tests/test_url_transform.py @@ -18,3 +18,19 @@ def test_non_utm_params_unaffected(): cleaned_url = apply_url_transformations(url) assert cleaned_url == url + + +def test_twitter_mobile_conversion(): + """Ensure that links to the Twitter mobile version are converted.""" + url = "https://mobile.twitter.com/acarboni/status/976545648391553024" + cleaned_url = apply_url_transformations(url) + + assert cleaned_url == "https://twitter.com/acarboni/status/976545648391553024" + + +def test_other_mobile_subdomain_not_removed(): + """Ensure that the Twitter mobile conversion isn't hitting other domains.""" + url = "http://mobile.example.com/something" + cleaned_url = apply_url_transformations(url) + + assert cleaned_url == url diff --git a/tildes/tildes/lib/url_transform.py b/tildes/tildes/lib/url_transform.py index 825aaf3..c5b69a7 100644 --- a/tildes/tildes/lib/url_transform.py +++ b/tildes/tildes/lib/url_transform.py @@ -115,3 +115,17 @@ class UtmQueryParamRemover(UrlTransformer): } return parsed_url._replace(query=urlencode(cleaned_params, doseq=True)) + + +class TwitterMobileConverter(UrlTransformer): + """Convert links to Twitter mobile version to the bare domain.""" + + @classmethod + def is_applicable(cls, parsed_url: ParseResult) -> bool: + """Return whether this transformation should be applied to the url.""" + return parsed_url.hostname == "mobile.twitter.com" + + @classmethod + def apply_transformation(cls, parsed_url: ParseResult) -> ParseResult: + """Apply the actual transformation process to the url.""" + return parsed_url._replace(netloc="twitter.com")