From 1a6b1e7fff67df936ed313be024b57420687c2d2 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 23 Apr 2023 04:34:58 +0200 Subject: [PATCH] feat: add more generators --- config.example.json | 11 +++++++++++ src/app/api.py | 32 ++++++++++++++++++++++++++++++++ src/app/generator.py | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/config.example.json b/config.example.json index bee0bad..c6b5f34 100644 --- a/config.example.json +++ b/config.example.json @@ -11,6 +11,17 @@ "socials": { "website": "https://yourwebsite.com" } + }, + { + "type": "team", + "organization": "yourorg" + }, + { + "type": "donation", + "links": { + "liberapay": "https://liberapay.com/yourteam", + "github": "https://github.com/sponsors/yourteam" + } } ], "output": "static" diff --git a/src/app/api.py b/src/app/api.py index f37bacd..2025733 100644 --- a/src/app/api.py +++ b/src/app/api.py @@ -35,6 +35,16 @@ class Api: raise NotImplementedError + @abstractmethod + def get_members(self, organization): + '''Gets the team for an organization. + + Args: + organization (str): The organization to get the team for. + ''' + raise NotImplementedError + + class GitHubApi(Api): def __init__(self): pass @@ -112,3 +122,25 @@ class GitHubApi(Api): f"https://api.github.com/repos/{repository}/releases/latest?prerelease={prerelease}" ).json() return transform_release(latest_release) + + def get_members(self, organization): + def transform_team_member(member: dict) -> dict: + '''Transforms a team member into a dict. + + Args: + member (dict): The member to transform. + + Returns: + dict: The transformed member. + ''' + + return { + 'username': member['login'], + 'avatar': member['avatar_url'], # TODO: Proxy via a CDN. + 'link': member['html_url'] + } + + members = requests.get( + f'https://api.github.com/orgs/{organization}/members').json() + # List might not be needed. + return list(map(transform_team_member, members)) \ No newline at end of file diff --git a/src/app/generator.py b/src/app/generator.py index 44aec0b..8ef3bca 100644 --- a/src/app/generator.py +++ b/src/app/generator.py @@ -83,13 +83,35 @@ class SocialApi(Api): super().__init__("social", api) def generate(self, config, path): - new_social = config + new_social = config["socials"] social_path = join(path, f"social.json") - social = read_json(social_path, new_social) - write_json(social, social_path) + write_json(new_social, social_path) +class TeamApi(Api): + def __init__(self, api): + super().__init__("team", api) + + def generate(self, config, path): + organization = config["organization"] + + team = self._api.get_members(organization) + + team_path = join(path, f"team.json") + + write_json(team, team_path) + +class DonationApi(Api): + def __init__(self, api): + super().__init__("donation", api) + + def generate(self, config, path): + donation = config["links"] + + donation_path = join(path, f"donation.json") + + write_json(donation, donation_path) class ApiProvider: _apis: list[Api] @@ -107,8 +129,12 @@ class ApiProvider: class DefaultApiProvider(ApiProvider): def __init__(self): - self._api = api.GitHubApi() # Use GitHub as default api - - super().__init__( - [ReleaseApi(self._api), ContributorApi(self._api), SocialApi(self._api)] - ) + self._api = api.GitHubApi() # Use GitHub as default api + + super().__init__([ + ReleaseApi(self._api), + ContributorApi(self._api), + SocialApi(self._api), + TeamApi(self._api), + DonationApi(self._api) + ])