From 44a56cd5f2cec45702c239ad7716fa9d5ae883b0 Mon Sep 17 00:00:00 2001 From: Eliot Berriot <contact@eliotberriot.com> Date: Sun, 9 Jun 2019 15:21:20 +0200 Subject: [PATCH] Flattr support --- retribute_api/providers.py | 21 ++++++++++++++++++++- tests/search/test_means.py | 6 +++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/retribute_api/providers.py b/retribute_api/providers.py index e604501..0df4df9 100644 --- a/retribute_api/providers.py +++ b/retribute_api/providers.py @@ -41,7 +41,17 @@ class BasicUsernameInPathProvider(Provider): if not re.match(self.domain_regex, parsed_url.hostname): return - result = re.match(self.username_regex, parsed_url.path) + regexes = ( + self.username_regex + if isinstance(self.username_regex, list) + else [self.username_regex] + ) + result = None + for r in regexes: + + result = re.match(r, parsed_url.path) + if result: + break if not result: return username = result.groups()[0] @@ -121,6 +131,15 @@ class Etsy(BasicUsernameInPathProvider): username_regex = r"^\/(?:[a-zA-Z-_]+/)?shop/([\w\.]+)/?$" +@registry.register +class FLattr(BasicUsernameInPathProvider): + id = "flattr" + label = "Flattr" + url = "https://flattr.com" + domain_regex = r"^(\w+\.)*flattr\.com" + username_regex = [r"^\/profile/([\w\.]+)/?$", r"^\/@([\w\.]+)/?$"] + + @registry.register class BandCamp(BasicUsernameInDomainProvider): id = "bandcamp" diff --git a/tests/search/test_means.py b/tests/search/test_means.py index 75abd94..e9537c6 100644 --- a/tests/search/test_means.py +++ b/tests/search/test_means.py @@ -31,12 +31,16 @@ from retribute_api.search import means {"provider": "opencollective", "id": "username"}, ), ("https://username.bandcamp.com", {"provider": "bandcamp", "id": "username"}), - ("https://username.bandcamp.com", {"provider": "bandcamp", "id": "username"}), ( "https://username.bandcamp.com/noop", {"provider": "bandcamp", "id": "username"}, ), ("https://patreon.com/username/nope", None), + ("https://flattr.com/@username", {"provider": "flattr", "id": "username"}), + ( + "https://flattr.com/profile/username", + {"provider": "flattr", "id": "username"}, + ), ], ) def test_extract_from_url(input, expected): -- GitLab