Last active
November 28, 2023 16:07
-
-
Save hacktivist123/53512c03d282d846410347a02eeb3686 to your computer and use it in GitHub Desktop.
This script calculate the total stars in all repos in a public Github Organisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def get_org_repos(org_name, token): | |
"""Fetch all repositories for the specified organization.""" | |
repos = [] | |
page = 1 | |
while True: | |
url = f'https://api.github.com/orgs/{org_name}/repos?page={page}' | |
headers = {'Authorization': f'token {token}'} | |
response = requests.get(url, headers=headers) | |
if response.status_code != 200: | |
raise Exception(f'Error fetching repos: {response.content}') | |
data = response.json() | |
if not data: # No more data to paginate | |
break | |
repos.extend(data) | |
page += 1 | |
return repos | |
def count_stars(repos): | |
"""Count the total number of stars across all repositories.""" | |
return sum(repo['stargazers_count'] for repo in repos) | |
def main(): | |
org_name = input("Enter the GitHub organization name: ") | |
token = input("Enter your GitHub API token: ") | |
try: | |
repos = get_org_repos(org_name, token) | |
total_stars = count_stars(repos) | |
print(f"Total stars in the {org_name} org: {total_stars}") | |
except Exception as e: | |
print(f"Error: {e}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment