Skip to content

Instantly share code, notes, and snippets.

@nthall
Created October 4, 2020 05:11
Show Gist options
  • Save nthall/e64047ce479975f0512bc22449cc6e11 to your computer and use it in GitHub Desktop.
Save nthall/e64047ce479975f0512bc22449cc6e11 to your computer and use it in GitHub Desktop.
a script that checks your starred github repos for projects that have opted in to Hacktoberfest 2020 by adding the topic "Hacktoberfest" to the repo
#!/usr/bin/python
import sys
from colorama import Fore, init, Style
from requests import Session
from requests.utils import parse_header_links
class Crawler:
def __init__(self, username=None, auth_token=None):
self.good = []
self.ua = 'nthall'
init(autoreset=True)
if username:
self.username = username
self.starred_url = 'https://api.github.com/users/{}/starred'.format(self.username)
else:
return self.show_help()
if auth_token:
self.auth_token = auth_token
def show_help(self):
print("Simple script to check your Stars on Github for repos that have opted in to Hacktoberfest 2020")
print("USAGE: python hackto.py {username} {access_token}")
print(" username: your Github username (or someone else's, I guess? whatever.")
print(" access_token: a Personal Access Token. This optional param is necessary to avoid the rate-limiting"
+ " that Github applies to anonymous requests.")
def process_page(self, current_url):
s = Session()
s.headers = {'User-Agent': self.ua, 'accept': 'application/vnd.github.mercy-preview+json'}
if self.auth_token:
s.auth = (self.username, self.auth_token)
stars = s.get(current_url, data={'per_page': 100})
try:
data = stars.json()
except Exception:
print(stars.status_code)
print(stars.text)
sys.exit(1)
for repo in data:
topics = s.get('https://api.github.com/repos/{}/topics'.format(repo['full_name']))
try:
if 'hacktoberfest' in topics.json()['names']:
self.good.append(repo)
except KeyError:
pass
next_url = False
try:
links = {d['rel']: d['url'] for d in parse_header_links(stars.headers['Link'])}
if 'next' in links:
next_url = links['next']
except KeyError:
next_url = False
import json
print(json.dumps(stars.headers, indent=4, sort_keys=True))
print(stars.text)
return next_url
def run(self):
print("Getting {}{}{}'s starred repos...".format(Fore.CYAN, self.username, Style.RESET_ALL))
next_url = self.process_page(self.starred_url)
while next_url:
next_url = self.process_page(next_url)
if self.good:
print('-'*30)
for repo in self.good:
print(Fore.CYAN + repo['full_name'])
print(Fore.BLUE + repo['html_url'])
print('{}{}{} - {}{}{} open issues'.format(
Fore.MAGENTA,
repo['language'],
Style.RESET_ALL,
Fore.GREEN,
repo['open_issues'],
Style.RESET_ALL
)
)
print('-'*30)
else:
print('ah geez, nothing found! sorry.')
if __name__ == '__main__':
if len(sys.argv) == 3:
c = Crawler(sys.argv[1], sys.argv[2])
elif len(sys.argv) == 2:
c = Crawler(sys.argv[1])
else:
c = Crawler()
c.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment