怎样通过GitHub API获取某个项目被Star的详细信息(时间,用户ID)

这个需求也比较明确,目前GitHub的页面上并不提供对某个项目Star随时间变化的数据,但实际上可以通过GitHub API获取,在之前的一篇日志中,我简单介绍了通过curl使用GitHub API的方法,不过正如这里介绍的:

Most applications will use an existing wrapper library in the language of your choice, but it's important to familiarize yourself with the underlying API HTTP methods first.

我们这里选择使用PyGitHub这个library:https://github.com/PyGithub/PyGithub

还是以bitcoin这个项目为例,要print出所有star的用户和具体时间,只需要下面这些代码:

from github import Github

g=Github("自己申请的Token")
repo=g.get_repo('bitcoin/bitcoin')
stargazers=repo.get_stargazers_with_dates()
for people in stargazers:
    print people.starred_at
    print people.user

实际上很简单,首先生成主类GitHub:http://pygithub.readthedocs.io/en/latest/github.html

再使用Repository这个class中的method:http://pygithub.readthedocs.io/en/latest/github_objects/Repository.html

就简单总结这么多。


获取GitHub某个项目的下载量,需要使用GitHub API。具体步骤如下: 1. 首先需要获取GitHub Personal Access Token。在GitHub上登录账号,进入Settings -> Developer settings -> Personal access tokens,创建一个新的Token,并给予该Token读取Repository的权限。 2. 使用Java中的HttpURLConnection或者HttpClient等工具,使用该Token向GitHub API发送请求,获取项目的下载量信息。具体的API接口为:https://api.github.com/repos/{username}/{repo}/releases/assets/{asset_id}。 3. 解析API返回的JSON数据,获取项目的下载量信息。 下面是一个示例代码,可以获取指定GitHub项目的下载量: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class DownloadCount { public static void main(String[] args) throws IOException { String username = "your_username"; String repo = "your_repo"; String assetId = "your_asset_id"; String token = "your_token"; URL url = new URL(String.format("https://api.github.com/repos/%s/%s/releases/assets/%s", username, repo, assetId)); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Authorization", "token " + token); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder response = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); String downloadCount = response.toString().split("\"download_count\":")[1].split(",")[0]; System.out.println("Download count: " + downloadCount); } } ``` 需要注意的是,GitHub API有一定的访问限制,如果频繁访问API可能会被GitHub限制访问。因此,建议使用缓存等机制来降低访问频率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值