-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
update_sponsors.py
executable file
·161 lines (129 loc) · 5.14 KB
/
update_sponsors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import requests
import base64
from datetime import datetime
def fetch_sponsors():
global headers
graphql_url = "https://api.github.com/graphql"
headers = {
"Authorization": f"Bearer {os.environ.get('GH_TOKEN')}",
"Accept": "application/vnd.github.v4+json",
}
# GraphQL query to fetch public sponsors
graphql_query = """
{
user(login: "jokob-sk") {
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()
print(f"Debug GraphQL query result: {data}")
if "errors" in data:
print(f"GraphQL query failed: {data['errors']}")
return {"sponsors": []}
sponsorships = data["data"]["user"]["sponsorshipsAsMaintainer"]["nodes"]
sponsors = []
for sponsorship in sponsorships:
privacy_level = sponsorship["privacyLevel"]
# Only include sponsors with privacyLevel set to "PUBLIC"
if privacy_level == "PUBLIC":
sponsor_entity = sponsorship["sponsorEntity"]
created_at = datetime.strptime(sponsorship["createdAt"], "%Y-%m-%dT%H:%M:%SZ")
# Check if tier is not None before accessing its properties
tier = sponsorship.get("tier", {})
monthly_price = tier.get("monthlyPriceInCents") if tier else None
sponsor = {
"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,
}
sponsors.append(sponsor)
print("Public Sponsors:")
print(sponsors)
return {"sponsors": sponsors}
def generate_sponsors_table(sponsors):
sponsors_table = "| All Sponsors |\n|---|\n"
for sponsor in sponsors:
sponsors_table += f"| [{sponsor['name'] or sponsor['login']}]({sponsor['url']}) |\n"
return sponsors_table
def update_readme(sponsors_table):
global headers
repo_owner = "jokob-sk"
repo_name = "NetAlertX"
# Update the README.md file in the GitHub repository
api_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/contents/README.md"
# Fetch the current content of the README.md file
response = requests.get(api_url, headers=headers)
readme_data = response.json()
# Extract content from the dictionary
readme_content = base64.b64decode(readme_data['content']).decode()
# Find the start and end markers
start_marker = "<!-- SPONSORS-LIST DO NOT MODIFY BELOW -->"
end_marker = "<!-- SPONSORS-LIST DO NOT MODIFY ABOVE -->"
# Replace the content between markers with the generated sponsors table
start_index = readme_content.find(start_marker)
end_index = readme_content.find(end_marker, start_index + len(start_marker))
if start_index != -1 and end_index != -1:
updated_readme = (
readme_content[:start_index + len(start_marker)]
+ "\n"
+ sponsors_table
+ "\n"
+ readme_content[end_index:]
)
else:
print("Markers not found in README.md. Make sure they are correctly placed.")
return
updated_content_base64 = base64.b64encode(updated_readme.encode()).decode()
# Create a commit to update the README.md file
commit_message = "[🤖Automation] Update README with sponsors information"
commit_data = {
"message": commit_message,
"content": updated_content_base64,
"sha": readme_data["sha"],
"branch": "main", # Update the branch name as needed
}
commit_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/contents/README.md"
commit_response = requests.put(commit_url, headers=headers, json=commit_data)
if commit_response.status_code == 200:
print("README.md updated successfully in the GitHub repository.")
else:
print(f"Failed to update README.md. Status code: {commit_response.status_code}")
print(commit_response.json())
print("README.md updated successfully with the sponsors table.")
def main():
sponsors_data = fetch_sponsors()
sponsors = sponsors_data.get("sponsors", [])
sponsors_table = generate_sponsors_table(sponsors)
update_readme(sponsors_table)
if __name__ == "__main__":
main()