Skip to content
Snippets Groups Projects
Verified Commit c8c64f36 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Added support for custom URLs containing a ?retribute query parameter

parent d9dd69f0
No related branches found
No related tags found
No related merge requests found
import re
import urllib.parse
class Registry(object):
......@@ -21,8 +22,8 @@ registry = Registry()
class Provider(object):
id = None
additional_ids = []
domain_regex = None
username_regex = None
description = None
url = None
def match_by_name(self, value):
if not value:
......@@ -119,3 +120,16 @@ class BandCamp(BasicUsernameInDomainProvider):
url = "https://bandcamp.com"
username_regex = r"^(\w+)\.bandcamp\.com"
@registry.register
class Custom(Provider):
id = "custom"
label = "Custom"
description = "Any url that contains a 'retribute' query parameter, e.g https://my.shop?retribute"
def match_from_url(self, parsed_url):
params = urllib.parse.parse_qs(parsed_url.query, keep_blank_values=True)
for key in params:
if key.lower() == "retribute":
return {"provider": "custom", "id": parsed_url.geturl()}
......@@ -183,7 +183,7 @@ class ProvidersConsumer(AsyncHttpConsumer):
@wrapper_500
async def handle(self, body):
data = [
{"id": p.id, "label": p.label, "url": p.url}
{"id": p.id, "label": p.label, "url": p.url, "description": p.description}
for _, p in sorted(providers.registry, key=lambda v: v[0])
]
await json_response(self, 200, data)
......@@ -80,7 +80,7 @@ async def test_search_multiple(loop, application, mocker, coroutine_mock, dummyc
async def test_providers(loop, application, mocker, coroutine_mock):
expected = [
{"id": p.id, "label": p.label, "url": p.url}
{"id": p.id, "label": p.label, "url": p.url, "description": p.description}
for _, p in sorted(providers.registry, key=lambda v: v[0])
]
communicator = HttpCommunicator(application, "GET", "/v1/providers")
......
......@@ -14,6 +14,10 @@ from retribute_api.search import means
("https://www.paypal.me/username", {"provider": "paypal", "id": "username"}),
("https://fr.tipeee.com/username", {"provider": "tipeee", "id": "username"}),
("https://tipeee.com/username", {"provider": "tipeee", "id": "username"}),
(
"https://custom.com/username?retribute",
{"provider": "custom", "id": "https://custom.com/username?retribute"},
),
(
"https://opencollective.com/username",
{"provider": "opencollective", "id": "username"},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment