我想随机抽取 GitHub 公共(public)存储库样本,将它们一一下载以用于统计目的。我尝试使用以下 Powershell 代码:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$id = Get-Random -Minimum 0 -Maximum 500
$jsonContent = Invoke-WebRequest "http://api.github.com/repositories?since=$id" | ConvertFrom-Json
我确实获得了公共(public)存储库的列表,但我无法限制它们的数量。
我尝试使用 ?page=1&per_page=1
但没有成功。我只想将 clone_url
解析为 git clone
。
有什么想法吗?也欢迎其他从 github 下载随机存储库的解决方案。
最佳答案
你很接近。这应该有效:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$id = Get-Random -Minimum 0 -Maximum 99
$jsonContent = Invoke-WebRequest "http://api.github.com/repositories?since=1" | ConvertFrom-Json
$gitUrl = Invoke-WebRequest -Uri ($jsonContent[$id].url) | ConvertFrom-Json | Select-Object -ExpandProperty git_url
git clone $gitUrl
如果您想通过 ssh 克隆,请务必将 git_url
更改为 ssh_url
。
说明
调用http://api.github.com/repositories?since=x始终代表相同的最后 100 个存储库,因此无需随机化那里的数字。
第一次调用的列表为您提供了 100 个存储库的 api URL。因此,随机检查其中一个并 grep url,以使用另一个 WebRequest 克隆到存储库的 api URL。
关于git - 使用 Powershell 下载随机 GitHub 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53705079/