【发布时间】:2014-03-17 17:04:57
【问题描述】:
你好朋友,我刚开始使用 GitHub,我只想知道可以通过使用 GitHub Api 或 Api 库(即 Github api 的 python 库“pygithub3”)将 github 存储库下载到我的本地计算机
【问题讨论】:
标签: api github repository github-api
你好朋友,我刚开始使用 GitHub,我只想知道可以通过使用 GitHub Api 或 Api 库(即 Github api 的 python 库“pygithub3”)将 github 存储库下载到我的本地计算机
【问题讨论】:
标签: api github repository github-api
使用github3.py,您可以通过以下方式克隆所有存储库(包括分叉和私有存储库):
import github3
import subprocess
g = github3.login('username', 'password')
for repo in g.iter_repos(type='all'):
subprocess.call(['git', 'clone', repo.clone_url])
如果您要克隆任意存储库,您可以这样做:
import github3
import subprocess
r = github3.repository('owner', 'repository_name')
subprocess.call(['git', 'clone', repo.clone_url])
pygithub3 一年多没有积极开发。我建议不要使用它,因为它没有维护,并且缺少 GitHub 从那时起对其 API 所做的大量添加。
【讨论】:
如this Gist 所示,最简单的解决方案就是调用 git clone。
#!/usr/bin/env python
# Script to clone all the github repos that a user is watching
import requests
import json
import subprocess
# Grab all the URLs of the watched repo
user = 'jharjono'
r = requests.get("http://github.com/api/users/%s/subscriptions" % (user))
repos = json.loads(r.content)
urls = [repo['url'] for repo in repos['repositories']]
# Clone them all
for url in urls:
cmd = 'git clone ' + url
pipe = subprocess.Popen(cmd, shell=True)
pipe.wait()
print "Finished cloning %d watched repos!" % (len(urls))
This gist,它使用 pygithub3,将在它找到的 repos 上调用 git clone:
#!/usr/bin/env python
import pygithub3
gh = None
def gather_clone_urls(organization, no_forks=True):
all_repos = gh.repos.list(user=organization).all()
for repo in all_repos:
# Don't print the urls for repos that are forks.
if no_forks and repo.fork:
continue
yield repo.clone_url
if __name__ == '__main__':
gh = pygithub3.Github()
clone_urls = gather_clone_urls("gittip")
for url in clone_urls:
print url
【讨论】: