Browse Source

Convert mobile Twitter links to standard ones

This is the first url transformation that only applies to a specific
site (and domain).
merge-requests/55/head
Deimos 6 years ago
parent
commit
bbf3ffb9ac
  1. 16
      tildes/tests/test_url_transform.py
  2. 14
      tildes/tildes/lib/url_transform.py

16
tildes/tests/test_url_transform.py

@ -18,3 +18,19 @@ def test_non_utm_params_unaffected():
cleaned_url = apply_url_transformations(url) cleaned_url = apply_url_transformations(url)
assert cleaned_url == 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

14
tildes/tildes/lib/url_transform.py

@ -115,3 +115,17 @@ class UtmQueryParamRemover(UrlTransformer):
} }
return parsed_url._replace(query=urlencode(cleaned_params, doseq=True)) 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")
Loading…
Cancel
Save