feat: add more generators

This commit is contained in:
oSumAtrIX 2023-04-23 04:34:58 +02:00
parent 078a8da77f
commit 1a6b1e7fff
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
3 changed files with 77 additions and 8 deletions

View File

@ -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"

View File

@ -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))

View File

@ -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)
])