github学习之githubAPI详解

https://developer.github.com/v3/libraries/


https://github.com/mamh-mixed/python-github-api

安装这个库,用起来比较方便
$ pip install PyGithub

获取jenkinsci下面的所有仓库

# coding=utf-8
import os
import sys
from github import Github


def get_org_repos(g, organization_name):
    organization_repos = g.get_organization(organization_name).get_repos()
    for repo in organization_repos:
           print(repo.full_name)   # 有了仓库名称,就可以 去 git clone 下载这些仓库代码了。


def main(argv): # main 方法
    g = Github("这里使用token比较好,使用账号密码会报错")
    organization_name = "jenkinsci"
    get_org_repos(g, organization_name)



if __name__ == "__main__":
    main(sys.argv[1:])


g.get_organization(organization_name) 会返回一个 class Organization(github.GithubObject.CompletableGithubObject): 实例。文档接口https://docs.github.com/en/rest/reference/orgs

这个实例里面有个 get_repos 方法,用来获取仓库信息的。它返回 一个 github.PaginatedList.PaginatedList 实例,我们去for循环遍历就行了。

class PaginatedList(PaginatedListBase):
    """
    This class abstracts the `pagination of the API <http://developer.github.com/v3/#pagination>`_.

    You can simply enumerate through instances of this class::

        for repo in user.get_repos():
            print(repo.name)

    If you want to know the total number of items in the list::

        print(user.get_repos().totalCount)
        print(len(user.get_repos()))

    You can also index them or take slices::

        second_repo = user.get_repos()[1]
        first_repos = user.get_repos()[:10]

    If you want to iterate in reversed order, just do::

        for repo in user.get_repos().reversed:
            print(repo.name)

    And if you really need it, you can explicitly access a specific page::

        some_repos = user.get_repos().get_page(0)
        some_other_repos = user.get_repos().get_page(3)
    """

这个里面存放的是 class Repository(github.GithubObject.CompletableGithubObject) 实例,文档接口https://docs.github.com/en/rest#pagination
通过Repository实例我们可以获取仓库的 full_name, 等等信息。fork,是否是克隆的其他的仓库的。如果是克隆的还会有个parent信息。
parent也是一个Repository实例。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值