Skip to content

Instantly share code, notes, and snippets.

@okalachev
Last active September 17, 2022 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okalachev/1de02a84f69e4bd953e7f0ec19d381af to your computer and use it in GitHub Desktop.
Save okalachev/1de02a84f69e4bd953e7f0ec19d381af to your computer and use it in GitHub Desktop.
Keep track of who stars and unstars your repos 😈
#!/usr/bin/env python3
# Fetch all GitHub repo stargazers
# Usage: REPO=<username>/<repo> ./stars.py
# Results are saved in JSON and TXT format into ./<username>-<repo>/ directory (by timestamp)
import os, os.path
from datetime import datetime
import requests
import json
import glob
repo = os.environ['REPO']
dir = repo.replace('/', '-')
stargazers = []
page = 1
while True:
print('Request page', page)
r = requests.get('https://api.github.com/repos/{}/stargazers'.format(repo),
params={'per_page': 100, 'page': page})
r.raise_for_status()
res = r.json()
if not res:
break
stargazers += res
page += 1
print('Stargazers:', len(stargazers))
try:
os.mkdir(dir)
except FileExistsError:
pass
filename = datetime.now().isoformat()
open(os.path.join(dir, filename + '.json'), 'w').write(json.dumps(stargazers))
new_txt = os.path.join(dir, filename + '.txt')
txt = open(new_txt, 'w')
for stargazer in stargazers:
txt.write(stargazer['login'] + '\n')
txt.close()
# show diff
files = sorted(glob.glob('*.txt', root_dir=dir))
if len(files) == 1:
import sys
sys.exit(0)
last_txt = os.path.join(dir, files[-2])
# removed
for user in open(last_txt):
if user not in open(new_txt):
print('\033[91m-', 'https://github.com/' + user.strip())
# added
for user in open(new_txt):
if user not in open(last_txt):
print('\033[92m+', 'https://github.com/' + user.strip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment