Skip to content

Instantly share code, notes, and snippets.

@subidit
Created April 12, 2024 17:40
Show Gist options
  • Select an option

  • Save subidit/48b9b9231dd0cae01b7c2119d6b1e881 to your computer and use it in GitHub Desktop.

Select an option

Save subidit/48b9b9231dd0cae01b7c2119d6b1e881 to your computer and use it in GitHub Desktop.
Find who have unstarred a GitHub repository
import requests
def get_stargazers(repo_owner, repo_name):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/stargazers"
headers = {"Accept": "application/vnd.github.v3.star+json"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return [user['user']['login'] for user in response.json()]
else:
print(f"Failed to fetch stargazers: {response.status_code}")
return []
def compare_stargazers(old_stargazers, new_stargazers):
unstarred_users = [user for user in old_stargazers if user not in new_stargazers]
return unstarred_users
def main():
# Input repository details
repo_owner = input("Enter the owner of the repository: ")
repo_name = input("Enter the name of the repository: ")
# Fetch current stargazers
current_stargazers = get_stargazers(repo_owner, repo_name)
# Load previous stargazers from file
try:
with open(f"{repo_owner}_{repo_name}_stargazers.txt", "r") as file:
previous_stargazers = file.read().splitlines()
except FileNotFoundError:
previous_stargazers = []
# Compare stargazers
unstarred_users = compare_stargazers(previous_stargazers, current_stargazers)
# Output unstarred users
if unstarred_users:
print("Users who have unstarred the repository:")
for user in unstarred_users:
print(user)
else:
print("No users have unstarred the repository.")
# Save current stargazers to file for future comparison
with open(f"{repo_owner}_{repo_name}_stargazers.txt", "w") as file:
for user in current_stargazers:
file.write(user + "\n")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment