Skip to content

Instantly share code, notes, and snippets.

@ifduyue
Last active September 30, 2015 08:28
Show Gist options
  • Save ifduyue/1752725 to your computer and use it in GitHub Desktop.
Save ifduyue/1752725 to your computer and use it in GitHub Desktop.
clone_all_github_repos
#!/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