使用 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. [AGC005C]Tree Restoring 构造

    Description ​ 给出一个数组a,要求构造一颗树,使节点x距离最远的点的距离为\(a_x\). Input ​ 第一行一个正整数NN(2≤N≤1002≤N≤100) ​ 接下来一行,有NN个 ...

  2. crm录入成绩modelformset组件

    不基于formset组件的普通写法>> views: class RecordScoreView(View): # 录入成绩 def get(self, request,class_stu ...

  3. 【算法】关于图论中的最小生成树(Minimum Spanning Tree)详解

    本节纲要 什么是图(network) 什么是最小生成树 (minimum spanning tree) 最小生成树的算法 什么是图(network)? 这里的图当然不是我们日常说的图片或者地图.通常情 ...

  4. [HAOI2011]Problem b BZOJ2301 数学

    题目描述 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 输入输出格式 输入格式: 第一行一个整数 ...

  5. idea 激活

    激活时选择License server,填入 http://idea.wlphp.com:1017 点击Active即可 2DZ8RPRSBU-eyJsaWNlbnNlSWQiOiIyRFo4UlBS ...

  6. vim多行注释与删除

    一.多行注释 1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;2. 在行首使用上下键选择需要注释的多行;3. 按下键盘(大写)“I”键,进入插入模式:4. 然后输入注 ...

  7. python-day15---面向对象

    面向对象零:面向对象的说明: 面向过程和面向对象: 面向对象:一切以对象为中心(解决的问题本身) 面向过程:一切以过程为中心(解决问题的步骤) 面向对象我们用 “ class 类名:” 来表示面向对象 ...

  8. python基础语法之基础语法规则以及设置

    1. 编码格式 在python3以上版本中,py文件默认采用UTF-8格式编码,所有的字符串都是unicode字符串.当然,我们也可以自己为源码文件指定不同的编码,以utf-8为例,相关代码如下所示: ...

  9. ubuntu系统下如何切换输入法

    如何切换输入法:ctrl+空格键 输入中文时如何翻页:键盘上的 - +两个键 super表示:美式键盘中的Win键

  10. java.math.BigDecimal cannot be cast to java.lang.String

    从数据库总查询出的count(*) 函数统计的值,类型转换方法: Map<String,Integer> map = new HashMap<String,Integer>() ...