首页 技术 正文
技术 2022年11月21日
0 收藏 374 点赞 3,112 浏览 2201 个字

使用 GitHub API 进行数据分析 (Node.js)

Node.js 的访问 GitHub 的 API 库,通过 npm 或者 yarn 安装:

 yarn add github-api

官方示例

获取 rate limit,有修改:

 var GitHub = require('github-api'); var gh = new GitHub( {token: "Your Token Here" } ); /**
* Search API has its own rate limit other than other apis
*/
function rateLimit() {
// check our rate-limit status
// since we're unauthenticated the limit is 60 requests per hour
gh.getRateLimit().getRateLimit()
.then( function(resp) {
console.log('Limit remaining: ' + resp.data.rate.remaining);
// date constructor takes epoch milliseconds and we get epoch seconds
console.log('Reset date: ' + new Date(resp.data.rate.reset * 1000));
}).catch( function(error) {
console.log('Error fetching rate limit', error.message);
});
} rateLimit();

在构建 GitHub 对象时,输入用户名及密码,或者你在 GitHub 上申请的 Token。

申请 Token 步骤比较简单,进入 https://github.com/settings/developers 中,选择 Personal Access Tokens,点击 Generate New Token,该 Token 在关闭页面后便无法再次查看,需要将它记录下来。

注意这里打印的 rate limit 次数是 5000/h,但它与 Search api 是有区别的。参考: https://github.com/github-tools/github/issues/487

搜索

官方示例中只有一个获取 rate limit 的示例,如果想要使用 GitHub 提供的 Search api 对整个 GitHub 进行搜索,其实也比较简单:

 keyword = 'test';
gh.search( {q: keyword, type: "code" } ).forCode() // *
.then( ( result ) => {
console.log( 'Processing data...' );
const data = result.data;
if( saveFile ) {
console.log( 'Saving into file...' );
writeJsonIntoFile( "./repos.data", data );
} for( let i = 0; i < data.length; i++ ) {
console.log( `Searching Page ${i+1}` );
process( data[i] );
} }).catch( ( error ) => {
console.log( `Status ${error.response.statusText }` )
console.log( `Wait ${error.response.headers.retry-after} seconds before try again.`) writeJsonIntoFile( "./test.log", error );
});

上面 * 号位置有几个不同的接口:forCode()、forRepositories() 等等,对应着下图中的红色方框的位置进行的筛选:

使用 GitHub API 进行数据分析 (Node.js)

之前提到过,这个 Search API 是有限制的,如果你过度使用的话,forCode() 中会捕获到错误 error,将 error.data 打印出来:

使用 GitHub API 进行数据分析 (Node.js)

如果出现了 abuse detection 的话,就需要等待一定时间后才能进行查询操作,等待的时间在响应的头部信息中:error.response.headers.retry-after。

顺便一提,使用这个搜索 api 很耗时,应该是网络的原因。

如果在对 JSON 对象做 JSON.stringify 处理时出现 converting circular structure to json 的错误,需要解决掉循环引用,可参考下方中提到的做法:

https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json

我将 JSON 对象的日志输出封装成了 writeJsonToFile 方法中。

小结

这里简单的提到了 GitHub api 的用法和使用过程中的一些注意事项,但仅仅通过 api 获取到了相应的数据还没有对其进行处理。

数据的处理需要先对 Search.forCode() 方法的返回值 data 进行分析,以后再说。

参考

1. https://developer.github.com/v3/guides/best-practices-for-integrators/

2. https://github.com/github-tools/github/issues/487

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,429
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,245
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,689
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726