From fa0c803fe38c62d55f823c40c5c81203fe5635ed Mon Sep 17 00:00:00 2001 From: gounux Date: Fri, 26 Apr 2024 09:29:21 +0200 Subject: [PATCH] Add JSON client for feed and tags --- geotribu_cli/json/__init__.py | 0 geotribu_cli/json/json_client.py | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 geotribu_cli/json/__init__.py create mode 100644 geotribu_cli/json/json_client.py diff --git a/geotribu_cli/json/__init__.py b/geotribu_cli/json/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/geotribu_cli/json/json_client.py b/geotribu_cli/json/json_client.py new file mode 100644 index 0000000..11657d5 --- /dev/null +++ b/geotribu_cli/json/json_client.py @@ -0,0 +1,58 @@ +from typing import Any + +import requests +from requests import Response + +from geotribu_cli.__about__ import __executable_name__, __version__ + +JSON_FEED_URL = "https://geotribu.fr/feed_json_created.json" +JSON_TAGS_URL = "https://geotribu.fr/tags.json" + +HEADERS: dict = { + b"Accept": b"application/json", + b"User-Agent": bytes(f"{__executable_name__}/{__version__}", "utf8"), +} + + +class JsonFeedClient: + def __init__(self, url: str = JSON_FEED_URL, tags_url: str = JSON_TAGS_URL): + """ + Class initialization + + Args: + url: JSON feed URL, defaults to https://geotribu.fr/feed_json_created.json + tags_url: JSON tags URL, defaults to https://geotribu.fr/tags.json + """ + self.url = url + self.tags_url = tags_url + + @property + def items(self) -> list[dict[str, Any]]: + """ + Fetch Geotribu JSON feed items + + Returns: + List of dicts representing raw JSON feed items + """ + r: Response = requests.get(self.url, headers=HEADERS) + r.raise_for_status() + return r.json()["items"] + + @property + def tags(self, should_sort: bool = False) -> list[str]: + """ + Fetch Geotribu used tags + + Args: + should_sort: if the list of returned tags should be alphabetically sorted + + Returns: + List of tags used by Geotribu + """ + r: Response = requests.get(self.tags_url, headers=HEADERS) + r.raise_for_status() + tags = set() + for item in r.json()["mappings"]: + for tag in item["tags"]: + tags.add(tag) + return sorted(tags) if should_sort else list(tags)