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

Official way to access / download github files from scripts?

一个经常给出的如何从 github 存储库访问/下载特定文件的技巧是使用 rawgit,例如:

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

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

然而,对于生产脚本来说这样做有一个很大的缺点,因为如果经常使用 rawgit URL 你会被阻止。 rawgit.com:

上也有说明

Use this URL for development

New changes you push to GitHub will be reflected within minutes. Excessive traffic will be throttled and blacklisted.

我在密集开发后与githup支持联系以获得畅通无阻并得到答案使用github API而不是rawgit!

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

答案:使用格式URL

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

Accept:application/vnd.github.v3.rawheader集合。


要从上面的示例中获取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 的基础 URL API
  • :owner/:repo/ 将其替换为所有者和存储库的名称
  • :path 将其替换为存储库中文件的路径
  • ?ref= select 分支、提交或标记的可选参数以从中获取文件。如果未指定,您将从存储库默认分支
  • 获取文件

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

  • Accept:application/vnd.github.v3.raw header 必须在获取 RAW 文件时设置。没有这个 header 你会得到 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