Skip to content

Instantly share code, notes, and snippets.

@OrkoHunter
Created June 7, 2018 23:18
Show Gist options
  • Save OrkoHunter/d20d1a196a30c5898f7e104b77b0538c to your computer and use it in GitHub Desktop.
Save OrkoHunter/d20d1a196a30c5898f7e104b77b0538c to your computer and use it in GitHub Desktop.
GitHub API script to check the existence of NOTICE files
import requests
username = ""
token = ""
def fetch_all_pages(query, params=None, headers=None):
"""
If the query returns paginated results,
this function recursively fetchs all the results, concatenate and return.
"""
r = requests.get(query, params=params, headers=headers, auth=(USERNAME, TOKEN))
if not r.ok:
raise(Exception("Error in fetch_all_pages", "query : ", query, "r.json() ", r.json()))
link = r.headers.get('link', None)
if link is None:
return r.json()
if 'rel="next"' not in link:
return r.json()
else:
next_url = None
for url in link.split(','):
if 'rel="next"' in url:
next_url = url.split(';')[0][1:-1]
if next_url[0] != 'h':
next_url = next_url[1:]
return r.json() + fetch_all_pages(next_url, params=params, headers=headers)
all_repos = fetch_all_pages("https://api.github.com/orgs/twitter/repos")
for repo in all_repos:
r = requests.get("https://api.github.com/repos/" + repo['full_name'] + "/contents/NOTICE", auth=(USERNAME, TOKEN))
if r.status_code == 404:
print("✕ [{repo_name}|https://github.com/{repo_name}]".format(repo_name=repo['full_name']))
else:
print("✔ [{repo_name}|https://github.com/{repo_name}]".format(repo_name=repo['full_name']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment