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

时间:2018-04-13 14:06:09

标签: curl github wget github-api

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

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

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

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

  

使用此网址进行开发

     

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

我与githup支持联系,以便在密集开发后获得解除阻止,并获得使用github API代替r​​awgit的答案!

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

1 个答案:

答案 0 :(得分:5)

回答:使用格式为

的网址
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的基本网址
  • :owner/:repo/将其替换为所有者和存储库的名称
  • :path将其替换为存储库中文件的路径
  • ?ref=可选参数,用于选择分支,提交或标记以从中获取文件。如果未指定,则从存储库默认分支
  • 获取文件

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

    要获取RAW文件,必须设置
  • Accept:application/vnd.github.v3.raw标头。如果没有此标头,您将获得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

相关问题