Sponsorship auto-generate v0.1.2

This commit is contained in:
Jokob-sk
2024-01-28 13:47:49 +11:00
parent 4486c57b48
commit a6a495d242
2 changed files with 64 additions and 22 deletions

View File

@@ -2,7 +2,7 @@ name: Update Sponsors
on: on:
schedule: schedule:
- cron: '0 2 * * *' # Set your preferred schedule - cron: '0 2 * * *' # Set your preferred schedule (UTC)
jobs: jobs:
update-readme: update-readme:

View File

@@ -2,45 +2,87 @@ import os
import requests import requests
def fetch_sponsors(): def fetch_sponsors():
repo_owner = "jokob-sk" graphql_url = "https://api.github.com/graphql"
repo_name = "Pi.Alert"
api_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/sponsors"
headers = { headers = {
"Authorization": f"Bearer {os.environ.get('GH_TOKEN')}", "Authorization": f"Bearer {os.environ.get('GH_TOKEN')}",
"Accept": "application/vnd.github.v3+json", "Accept": "application/vnd.github.v4+json",
} }
# GraphQL query to fetch sponsors
graphql_query = """
{
viewer {
login
sponsorshipsAsMaintainer(first: 100, orderBy: {field: CREATED_AT, direction: ASC}, includePrivate: true) {
totalCount
pageInfo {
endCursor
}
nodes {
sponsorEntity {
... on User {
name
login
url
}
... on Organization {
name
url
login
}
}
createdAt
privacyLevel
tier {
monthlyPriceInCents
}
}
}
}
}
"""
response = requests.post(graphql_url, json={"query": graphql_query}, headers=headers)
data = response.json()
if "errors" in data:
print(f"GraphQL query failed: {data['errors']}")
return {"current_sponsors": [], "past_sponsors": []}
sponsorships = data["data"]["viewer"]["sponsorshipsAsMaintainer"]["nodes"]
current_sponsors = [] current_sponsors = []
past_sponsors = [] past_sponsors = []
page = 1 for sponsorship in sponsorships:
while True: sponsor_entity = sponsorship["sponsorEntity"]
params = {"page": page} created_at = sponsorship["createdAt"]
response = requests.get(api_url, headers=headers, params=params) privacy_level = sponsorship["privacyLevel"]
data = response.json() monthly_price = sponsorship["tier"]["monthlyPriceInCents"]
if not data: sponsor = {
break "name": sponsor_entity.get("name"),
"login": sponsor_entity["login"],
"url": sponsor_entity["url"],
"created_at": created_at,
"privacy_level": privacy_level,
"monthly_price": monthly_price,
}
for sponsor in data: if created_at == sponsorship["createdAt"]:
if sponsor["sponsorship_created_at"] == sponsor["sponsorship_updated_at"]: past_sponsors.append(sponsor)
past_sponsors.append(sponsor) else:
else: current_sponsors.append(sponsor)
current_sponsors.append(sponsor)
page += 1
return {"current_sponsors": current_sponsors, "past_sponsors": past_sponsors} return {"current_sponsors": current_sponsors, "past_sponsors": past_sponsors}
def generate_sponsors_table(current_sponsors, past_sponsors): def generate_sponsors_table(current_sponsors, past_sponsors):
current_table = "| Current Sponsors |\n|---|\n" current_table = "| Current Sponsors |\n|---|\n"
for sponsor in current_sponsors: for sponsor in current_sponsors:
current_table += f"| [{sponsor['login']}](https://github.com/{sponsor['login']}) |\n" current_table += f"| [{sponsor['name'] or sponsor['login']}]({sponsor['url']}) - ${sponsor['monthly_price'] / 100:.2f} |\n"
past_table = "| Past Sponsors |\n|---|\n" past_table = "| Past Sponsors |\n|---|\n"
for sponsor in past_sponsors: for sponsor in past_sponsors:
past_table += f"| {sponsor['login']} |\n" past_table += f"| [{sponsor['name'] or sponsor['login']}]({sponsor['url']}) - ${sponsor['monthly_price'] / 100:.2f} |\n"
return current_table + "\n" + past_table return current_table + "\n" + past_table