【问题标题】:it is possible to download GitHub repository to my local computer可以将 GitHub 存储库下载到我的本地计算机
【发布时间】:2014-03-17 17:04:57
【问题描述】:

你好朋友,我刚开始使用 GitHub,我只想知道可以通过使用 GitHub Api 或 Api 库(即 Github api 的 python 库“pygithub3”)将 github 存储库下载到我的本地计算机

【问题讨论】:

    标签: api github repository github-api


    【解决方案1】:

    使用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 所做的大量添加。

    【讨论】:

    • 好的,那么你能推荐我另一个用于 GitHub Api 的 python 库吗?并感谢您的回答:)
    • @user3321823 我在我的示例中建议使用 github3.py。
    • 是的,但是在您的回答中,您写道 pygithub3 没有得到积极开发,这就是为什么我要询问哪个 python 库比 pygithub3 更好?请建议您喜欢哪一个。
    • pygithub3 不是 github3.py。它们是截然不同的库。我更喜欢github3.py,因为我写的
    • 好的,谢谢 Cordasco 先生的重播 我尝试了您建议的示例,但出现错误 --> 访问 api.github.com/users/myusername/repos/info/refs 时请求的 URL 返回错误:403。
    【解决方案2】:

    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
    

    【讨论】:

    • 我不明白显示 GitHub API 版本 2 的用法有什么帮助。该版本已被弃用且完全不相关。
    • @sigmavirus24 我已经编辑了使用 V3 api 的答案。总体思路保持不变。
    • 你在第一个例子中的API url还是很错误的。
    猜你喜欢
    • 2022-01-24
    • 2021-05-22
    • 2011-09-05
    • 2017-05-09
    • 2017-08-17
    • 2014-10-11
    • 2019-04-26
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多