Skip to content

Instantly share code, notes, and snippets.

@a12k
Last active June 22, 2018 12:50
Show Gist options
  • Save a12k/b41467c3f71d20bb2f488814cf8dfda3 to your computer and use it in GitHub Desktop.
Save a12k/b41467c3f71d20bb2f488814cf8dfda3 to your computer and use it in GitHub Desktop.
download github private repo readmes
# usage: python3 ghreadme.py
import json
import requests
GHUSERNAME = '' # your username here
GHPASSWORD = '' # your GH personal access token here
GHORG = 'department-of-veterans-affairs' # change this for other orgs
class GHReadme(object):
def __init__(self):
self.ghusername = GHUSERNAME
self.ghpassword = GHPASSWORD
self.ghorg = GHORG
self.api_base = 'https://api.github.com/'
def _get_private_repos(self):
"""finds all private repos in org """
private_repos = set()
page = 1
url = self.api_base + 'orgs/' + self.ghorg + '/repos?page=' + str(page) + '&per_page=100'
while url:
# import pdb; pdb.set_trace()
print('Getting private repos - Page ' + str(page))
r = requests.get(url, auth=(self.ghusername, self.ghpassword))
json_content = json.loads(r.content)
for i in range(len(json_content)):
if json_content[i]['private']:
private_repos.add(json_content[i]['name'])
try:
url = r.links['next']['url']
except KeyError:
url = None
page += 1
print('\n' + str(len(private_repos)) + ' private repos found\n')
return private_repos
def _get_readme_links(self, repos):
"""resolves private repos to readme links"""
repo_dict = {}
for repo in repos:
print('Getting README.md link for repo ' + repo)
r = requests.get(self.api_base + 'repos/' + self.ghorg + '/' + repo + '/readme',
auth=(self.ghusername, self.ghpassword))
json_repo = json.loads(r.content)
try:
repo_dict[repo] = json_repo['download_url']
except KeyError: # this is hacky
pass
print('\nGot links for ' + str(len(repo_dict)) + ' private repos\n')
return repo_dict
def _download_readmes(self, repos):
"""downloads all readmes to working diectory """
count = 0
for repo_name, readme_url in repos.items():
print('Downloading README.md for ' + repo_name + ' as ' + repo_name + '_README.md')
r = requests.get(readme_url, auth=(self.ghusername, self.ghpassword))
with open(repo_name + '_README.md', 'wb') as f:
f.write(r.content)
count += 1
print('\n' + str(count) + ' READMEs Downloaded')
def run_task(self):
"""runs the loop to get private repos, get readme links, download """
private_repo_list = self._get_private_repos()
private_repo_dict = self._get_readme_links(private_repo_list)
self._download_readmes(private_repo_dict)
print('\nDONE!\n')
if __name__ == '__main__':
g = GHReadme()
g.run_task()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment