使用 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() 等等,对应着下图中的红色方框的位置进行的筛选:

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

如果出现了 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/#dealing-with-abuse-rate-limits

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

使用 GitHub API 进行数据分析 (Node.js)的更多相关文章

  1. 从Github远程库安装Node.JS

    3)从Github远程库安装Node.JS在这个方法中我们需要一些步骤来把Node.js的从Github上的远程的仓库克隆到本地仓库目录 在开始克隆(克隆)包到本地并且配制之前,我们要先安装以下依赖包 ...

  2. [MEAN Stack] First API -- 1. with Node.js, Express and MongoDB

    Learn how to import data into your MongoDB and then use Express to serve a simple Node.js API. Impor ...

  3. GitHub 如何基於 Node.js 和 Chromium 開發 Atom?

    看到回答里, 多数都没有回答到点子上, 还有些给了非常主观的意见而没有给出实际结论和分析过程. 题主的问题有四个: 1. Github 如何基于 Node.js 和 Chromium 开发 Atom? ...

  4. [MEAN+ Webstrom] First API -- 2.Debug Node.js RESTful application

    Using WebStrom can easily debug the Node applcation. For example, we have an Node+Express applicatio ...

  5. Code Your First API With Node.js and Express: Set Up the Server

    How to Set Up an Express API Server in Node.js In the previous tutorial, we learned what the REST ar ...

  6. 使用 Node.js 搭建一个 API 网关

    原文地址:Building an API Gateway using Node.js 外部客户端访问微服务架构中的服务时,服务端会对认证和传输有一些常见的要求.API 网关提供共享层来处理服务协议之间 ...

  7. node.js学习资料

    Node.js 入门 <汇智网 Node.js 课程> <快速搭建 Node.js 开发环境以及加速 npm> http://fengmk2.com/blog/2014/03/ ...

  8. Node.js(2)-protobuf zeromq gzip

    1.Node.Js环境准备 在win8 + vs.net 2012 环境下调试了很长时间没搞定安装编译问题,重装系统测试了2套环境,解决了编译问题: 1)Win8.1 + vs.net 2013 2) ...

  9. node.js中文资料导航

    以下资料来自gitHUb上面:https://github.com/youyudehexie/node123 Node.js HomePage Node官网七牛镜像 Infoq深入浅出Node.js系 ...

随机推荐

  1. Vue 自定义header

    第一种,全局设置: Vue.http.headers.common['token'] = 'YXBpOnBhc3N3b3Jk'; 第二种,拦截器设置 Vue.http.interceptors.pus ...

  2. Java以邮件附件的方式发送excel文件

    String to = "xxx@qq.com"; // 收件人的QQ邮箱 String from = "xxx@qq.com"; // 发件人的QQ邮箱 St ...

  3. oracle 新增主键

    alter table tablename add constraint pk_tablename primary key (column1,column2,...); 可以新增单主键或联合主键: 新 ...

  4. 1.Java 程序工作原理

    JVM:JAVA虚拟机,java程序运行在jvm上,jvm是java程序的运行环境. java程序的平台无关性:java编写的程序(.java)经过编译器变异成字节码文件(.class).这个字节码文 ...

  5. 静态库(.a)与动态库(.so)的简明介绍

    静态库(.a)与动态库(.so)的简明介绍 gcc有很多关于静态库,动态库的选项如-l,-L,-fPIC,-shared -Wl,-soname,看着很复杂容易混淆,其实静态库和动态库都是应需而生,只 ...

  6. SDUT OJ 效率至上(线段树)

    效率至上 Time Limit: 5000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 题意很简单,给出一个数目为n ...

  7. Domoticz 中接入斐讯 M1 空气质量检测仪

    前言 M1 是翻车讯出的一款空气质量检测仪,如今斐讯的服务器经常连不上了,M1 不动动手接到 Domoticz 怕是变成摆设了.教程参考了这里和官方的 Using Python plugins. 步骤 ...

  8. SSM整合dubbo 进行分页查询

    1.先书写Mapper和sql语句 public interface ActEntityMapper { int deleteByPrimaryKey(String actId); int inser ...

  9. Win7电脑开机无法正常启动只能进入安全模式解决方式

    我们先尝试在开机的时候按F8进入安全模式,进入到安全模式后一次打开“控制面板”-“程序与功能”,然后将卡巴斯基卸载[ http://jingyan.baidu.com/article/ff42efa9 ...

  10. BZOJ - 1497 最小割应用

    题意:基站耗费成本,用户获得利益(前提是投入成本),求最大获利 最小割的简单应用,所有可能的收益-(消耗的成本/失去的收益),无穷大边表示冲突,最小割求括号内的范围即可 #include<ios ...