|
1 | 1 | import requests |
2 | 2 | import json |
3 | 3 | import os |
| 4 | +from collections import defaultdict |
4 | 5 |
|
5 | 6 | GRAPHQL_URL = "https://api.github.com/graphql" |
6 | 7 |
|
7 | 8 | GRAPHQL_QUERY = """ |
8 | | -query { |
9 | | - organization(login: "MMRLApp") { |
| 9 | +query($org: String!) { |
| 10 | + organization(login: $org) { |
10 | 11 | sponsorshipsAsMaintainer(activeOnly: false, first: 100) { |
11 | 12 | nodes { |
12 | 13 | sponsorEntity { |
|
30 | 31 | } |
31 | 32 | """ |
32 | 33 |
|
33 | | -def fetch_sponsors(github_token): |
| 34 | +def fetch_sponsors(github_token, org): |
34 | 35 | headers = { |
35 | 36 | "Authorization": f"Bearer {github_token}", |
36 | 37 | "Content-Type": "application/json" |
37 | 38 | } |
38 | | - |
39 | | - response = requests.post(GRAPHQL_URL, json={"query": GRAPHQL_QUERY}, headers=headers) |
| 39 | + |
| 40 | + response = requests.post( |
| 41 | + GRAPHQL_URL, |
| 42 | + json={"query": GRAPHQL_QUERY, "variables": {"org": org}}, |
| 43 | + headers=headers |
| 44 | + ) |
40 | 45 | response.raise_for_status() |
41 | | - |
| 46 | + |
42 | 47 | return response.json() |
43 | 48 |
|
44 | | -def save_sponsors_to_file(sponsors_data, filename="meta/sponsors.json"): |
45 | | - print(sponsors_data) |
| 49 | +def merge_sponsors(sponsors_list): |
| 50 | + sponsors_dict = defaultdict(lambda: {"avatarUrl": "", "url": "", "amount": 0}) |
46 | 51 |
|
47 | | - sponsors = [ |
48 | | - { |
49 | | - "login": node["sponsorEntity"]["login"], |
50 | | - "avatarUrl": node["sponsorEntity"]["avatarUrl"], |
51 | | - "url": node["sponsorEntity"]["url"], |
52 | | - "amount": node["tier"]["monthlyPriceInCents"] |
53 | | - } |
54 | | - for node in sponsors_data["data"]["organization"]["sponsorshipsAsMaintainer"]["nodes"] |
55 | | - ] |
| 52 | + for sponsor in sponsors_list: |
| 53 | + login = sponsor["login"] |
| 54 | + sponsors_dict[login]["avatarUrl"] = sponsor["avatarUrl"] |
| 55 | + sponsors_dict[login]["url"] = sponsor["url"] |
| 56 | + sponsors_dict[login]["amount"] += sponsor["amount"] |
56 | 57 |
|
57 | | - os.makedirs(os.path.dirname(filename), exist_ok=True) |
58 | | - with open(filename, "w") as f: |
59 | | - json.dump(sponsors, f, indent=2) |
| 58 | + return list({"login": k, **v} for k, v in sponsors_dict.items()) |
60 | 59 |
|
61 | 60 | def main(): |
62 | 61 | github_token = os.getenv("SPONSORS_TOKEN") |
63 | 62 | if not github_token: |
64 | 63 | raise ValueError("GitHub token is not set in environment variables") |
65 | 64 |
|
66 | | - sponsors_data = fetch_sponsors(github_token) |
67 | | - save_sponsors_to_file(sponsors_data) |
| 65 | + orgs = ["MMRLApp", "DerGoogler"] |
| 66 | + all_sponsors = [] |
| 67 | + |
| 68 | + for org in orgs: |
| 69 | + sponsors_data = fetch_sponsors(github_token, org) |
| 70 | + sponsors = [ |
| 71 | + { |
| 72 | + "login": node["sponsorEntity"]["login"], |
| 73 | + "avatarUrl": node["sponsorEntity"]["avatarUrl"], |
| 74 | + "url": node["sponsorEntity"]["url"], |
| 75 | + "amount": node["tier"]["monthlyPriceInCents"] |
| 76 | + } |
| 77 | + for node in sponsors_data.get("data", {}).get("organization", {}).get("sponsorshipsAsMaintainer", {}).get("nodes", []) |
| 78 | + ] |
| 79 | + all_sponsors.extend(sponsors) |
| 80 | + |
| 81 | + merged_sponsors = merge_sponsors(all_sponsors) |
| 82 | + print(json.dumps(merged_sponsors, indent=2)) |
68 | 83 |
|
69 | 84 | if __name__ == "__main__": |
70 | 85 | main() |
0 commit comments