Skip to content

Instantly share code, notes, and snippets.

@tekton
Created December 8, 2018 04:35
Show Gist options
  • Save tekton/0927580096e7047fedb7dea78d3b061f to your computer and use it in GitHub Desktop.
Save tekton/0927580096e7047fedb7dea78d3b061f to your computer and use it in GitHub Desktop.
download all repos for a user
import urllib.request
import subprocess
import argparse
import json
def call_github_repos(username, oauth=None, base_url="https://api.github.com"):
headers = {"Accept": "application/vnd.github.v3+json"}
if oauth:
headers["Authorization"] = "token {}".format(oauth)
full_url = "{}/users/{}/repos".format(base_url, username)
request = urllib.request.Request(full_url, headers=headers)
data = None
with urllib.request.urlopen(request) as response:
data = json.loads(response.read())
return data
def clone_repo(repo_url):
process = subprocess.Popen(["git", "clone", repo_url], stdout=subprocess.PIPE)
output = process.communicate()[0]
def main_helper(username, oauth=None, act="clone", meth="ssh"):
data = call_github_repos(username, oauth)
for repo in data:
# print(repo["git_url"]) # uncomment for some extra logging
clone_repo(repo["git_url"])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--username", help="github username", required=True)
args = parser.parse_args()
main_helper(args.username)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment