import os import requests import base64 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 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 = [] past_sponsors = [] for sponsorship in sponsorships: sponsor_entity = sponsorship["sponsorEntity"] created_at = sponsorship["createdAt"] privacy_level = sponsorship["privacyLevel"] monthly_price = sponsorship["tier"]["monthlyPriceInCents"] 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, } # Check if the sponsorship is current or past if privacy_level == "PUBLIC": current_sponsors.append(sponsor) else: past_sponsors.append(sponsor) print("Current Sponsors:") print(current_sponsors) print("\nPast Sponsors:") print(past_sponsors) return {"current_sponsors": current_sponsors, "past_sponsors": past_sponsors} def generate_sponsors_table(current_sponsors, past_sponsors): current_table = "| Current Sponsors |\n|---|\n" for sponsor in current_sponsors: current_table += f"| [{sponsor['name'] or sponsor['login']}]({sponsor['url']}) - ${sponsor['monthly_price'] / 100:.2f} |\n" past_table = "| Past Sponsors |\n|---|\n" for sponsor in past_sponsors: past_table += f"| [{sponsor['name'] or sponsor['login']}]({sponsor['url']}) - ${sponsor['monthly_price'] / 100:.2f} |\n" return current_table + "\n" + past_table def update_readme(sponsors_table): global headers repo_owner = "jokob-sk" repo_name = "Pi.Alert" # 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 = "" end_marker = "" # 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() current_sponsors = sponsors_data.get("current_sponsors", []) past_sponsors = sponsors_data.get("past_sponsors", []) sponsors_table = generate_sponsors_table(current_sponsors, past_sponsors) update_readme(sponsors_table) if __name__ == "__main__": main()