代码之家  ›  专栏  ›  技术社区  ›  Gnadelwartz

从脚本访问/下载github文件的正式方式?

  •  4
  • Gnadelwartz  · 技术社区  · 6 年前

    如何从github存储库访问/下载特定文件的一个常见提示是使用rawgit,例如:

    curl https://rawgit.com/webmin/webmin/master/os_list.txt
    

    这将为您提供webmin github存储库中文件的当前版本。

    然而,对于生产脚本来说,这样做有很大的缺点,因为如果经常使用rawgit URL,您将被阻止。这在rawgit上也有说明。com:

    使用此URL进行开发

    您推送到GitHub的新更改将在几分钟内反映出来。 过度流量将被限制并列入黑名单。

    我与githup支持部门联系,希望在深入开发之后能够解锁,并得到了使用github API而不是rawgit的答案!

    问题:如何使用github API从github存储库中检索特定文件?

    1 回复  |  直到 6 年前
        1
  •  6
  •   Gnadelwartz    6 年前

    答复 :使用以下格式的URL

    https://api.github.com/repos/:owner/:repo/contents/:path?ref=tag/commit/branch
    

    这个 Accept:application/vnd.github.v3.raw 标题集。


    获取 os-lists.txt 上述示例中的文件使用:

    curl -s -H "Accept:application/vnd.github.v3.raw" https://api.github.com/repos/webmin/webmin/contents/os_lists.txt
    

    说明:

    • https://api.github.com/repos/ github API的基本URL
    • :owner/:repo/ 将其替换为所有者和存储库的名称
    • :path 将其替换为存储库中文件的路径
    • ?ref= 可选参数,用于选择从中获取文件的分支、提交或标记。如果未指定,则从存储库默认分支获取文件

    有关更多信息,请参阅: https://developer.github.com/v3/repos/contents/

    • 接受:应用程序/vnd。github。v3。未经加工的 必须在获取原始文件时设置标头。如果没有此标头,您将获得JSON格式的文件信息:
    {
          "name": "os_list.txt",
          "path": "os_list.txt",
          "sha": "2fa32a1860063f47c9d9ddcfe73368329cef0ba1",
          "size": 31563,
          "url": "https://api.github.com/repos/webmin/webmin/contents/os_list.txt?ref=master",
          "html_url": "https://github.com/webmin/webmin/blob/master/os_list.txt",
          "git_url": "https://api.github.com/repos/webmin/webmin/git/blobs/2fa32a1860063f47c9d9ddcfe73368329cef0ba1",
          "download_url": "https://raw.githubusercontent.com/webmin/webmin/master/os_list.txt",
          "type": "file",
          "content": "IyBQY......",
    }
    

    有关更多信息,请参阅: https://developer.github.com/v3/


    参考文献:

    https://developer.github.com/v3/repos/contents/

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

    https://github.com/qooob/authentic-theme/pull/1083