Last active
September 30, 2015 08:28
-
-
Save ifduyue/1752725 to your computer and use it in GitHub Desktop.
clone_all_github_repos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
#-*- coding: utf-8 -*- | |
import sys | |
import os | |
from urlfetch import get | |
user = sys.argv[1] | |
r = get('https://api.github.com/users/%s/repos' % user) | |
repos = r.json | |
links = [] | |
for i in r.getheader('link', '').split(','): | |
try: | |
url, params = i.split(';', 1) | |
except ValueError: | |
url, params = i, '' | |
link = {} | |
link['url'] = url.strip('''<> '"''') | |
for param in params.split(';'): | |
try: | |
k, v = param.split('=') | |
except ValueError: | |
break | |
link[k.strip(''' '"''')] = v.strip(''' '"''') | |
links.append(link) | |
for link in links: | |
if link.get('rel') != 'next': | |
continue | |
r = get(link['url']) | |
repos.extend(r.json) | |
for repo in r.json: | |
cmd = 'git clone ' + repo['ssh_url'] | |
print cmd, '......' | |
os.system(cmd) | |
if repo['fork']: | |
rr = get('https://api.github.com/repos/%s/%s' % (user, repo['name'])) | |
cmd = 'cd %s; git remote add upstream %s' % (repo['name'], rr.json['parent']['git_url']) | |
print '>>>', cmd, '......' | |
os.system(cmd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment