Skip to content

Commit

Permalink
solve rate limit problem
Browse files Browse the repository at this point in the history
  • Loading branch information
timqian committed Mar 7, 2016
1 parent fb81a78 commit 39a253c
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 74 deletions.
35 changes: 14 additions & 21 deletions dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.js.map

Large diffs are not rendered by default.

File renamed without changes.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<svg></svg>
</div>
<h4>
News: <a href="https://github.com/timqian/star-history-plugin">Chrome extension version of star-history</a> is now available. (with higher rate limit)
<a href="https://github.com/timqian/star-history-plugin">Chrome extension version of star-history</a>
</h4>
</script>
<script type="text/javascript" src="./dist/bundle.js" charset="utf-8"></script>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "timqian",
"license": "MIT",
"dependencies": {
"axios": "^0.5.4",
"axios": "^0.9.1",
"babel-polyfill": "^6.5.0",
"d3": "^3.5.6",
"nvd3": "^1.8.1"
Expand Down
86 changes: 66 additions & 20 deletions src/getStarHistory.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,89 @@
import axios from 'axios';
import generateUrls from './generateUrls';

const getConfig = {
// https://github.com/blog/1509-personal-api-tokens
const access_token = ['9da45c0ed04c77c47278bb260d7c6b6c2c9b9fa8', 'd96ed1e68bec80e725db8f23327a96839def67a6', '569e1881d1b810c39e35c650f056ea6fac05a400'];

const axiosGit = axios.create({
headers: {
Accept: 'application/vnd.github.v3.star+json',
},
};
params: {
access_token: access_token[Math.floor(Math.random() * access_token.length)],
},
});

const sampleNum = 18; // number of sample requests to do

/**
* get star history
* @param {sting} repo
* @return {array} history - [{date: ,starNum:}, ...]
* generate Urls and pageNums
* @param {sting} repo - eg: 'timqian/jsCodeStructure'
* @return {object} {sampleUrls, pageIndexes} - urls to be fatched(length <=10) and page indexes
*/
export default async function(repo) {
const {
samplePageUrls, pageIndexes
} = await generateUrls(repo).catch(e => {
console.log(e); // throw don't work
async function generateUrls(repo) {
let sampleUrls = []; // store sampleUrls to be rquested
let pageIndexes = []; // used to calculate total stars for this page

const initUrl = `https://api.github.com/repos/${repo}/stargazers`; // used to get star infors
const initRes = await axiosGit.get(initUrl).catch(res => {
throw 'No such repo or netowrk err!';
});
if (pageIndexes.length < 1) {
alert('There is no such repo on github or the repo has less then 30 stars')

/* link Sample (no link when star < 30):
<https://api.github.com/repositories/40237624/stargazers?access_token=2e71ec1017dda2220ccba0f6922ecefd9ea44ac7&page=2>;
rel="next", <https://api.github.com/repositories/40237624/stargazers?access_token=2e71ec1017dda2220ccba0f6922ecefd9ea44ac7&page=4>; rel="last"
*/
const link = initRes.headers.link;

if (!link) {
throw 'Too few stars (less than 30)!';
}
const getArray = samplePageUrls.map(url => axios.get(url, getConfig));
// console.log(getArray);

const resArray = await Promise.all(getArray).catch(e => {
console.log(e); // throw don't work
alert(`Sorry, Git API rate limit exceeded for your ip address, please wait for an hour`);
const pageNum = /next.*?page=(\d*).*?last/.exec(link)[1]; // total page number

// generate { sampleUrls, pageIndexes } accordingly
if (pageNum <= sampleNum) {
for (let i = 2; i <= pageNum; i++) {
pageIndexes.push(i);
sampleUrls.push(initUrl + '?page=' + i);
}
} else {
for (let i = 1; i <= sampleNum; i++) {
let pageIndex = Math.round(i / sampleNum * pageNum) - 1; //for bootstrap bug
pageIndexes.push(pageIndex);
sampleUrls.push(initUrl + '?page=' + pageIndex);
}
}

console.log("pageIndexes", pageIndexes);
return { sampleUrls, pageIndexes };
}

/**
* get star history
* @param {sting} repo - eg: 'timqian/jsCodeStructure'
* @return {array} history - eg: [{date: 2015-3-1,starNum: 12}, ...]
*/
async function getStarHistory(repo) {

const { sampleUrls, pageIndexes } = await generateUrls(repo).catch(e => {
throw e;
});

// promisese to request sampleUrls
const getArray = sampleUrls.map(url => axiosGit.get(url));

const resArray = await Promise.all(getArray).catch(res => {
throw 'Github api limit exceeded, Try in the new hour!'
});
// console.log(resArray);

const starHistory = pageIndexes.map((p, i) => {
return {
date: resArray[i].data[0].starred_at.slice(0, 10),
starNum: 30 * (p - 1),
};
});
console.log(starHistory);

return starHistory;
}

export default getStarHistory;
35 changes: 5 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
import "babel-polyfill"; // for async/await http://babeljs.io/docs/usage/polyfill/
import d3 from 'd3';
import nv from 'nvd3';
import axios from 'axios';
import getStarHistory from './getStarHistory';

//
// let code = window.location.search.slice(6);
// console.log(code);
// if (code.length < 1) {
// window.location.href='https://github.com/login/oauth/authorize?client_id=4e4f2621589085b864d7';
// } else {
// const postData = {
// code,
// client_id: '4e4f2621589085b864d7',
// client_secret: 'd990379890dd26d973f227304d4c88b10528c76b',
// };
//
// console.log(postData);
// (async function(){
// const res = await axios.post('https://github.com/login/oauth/access_token', postData)
// .catch(err => {console.log(err);});
// console.log('hi');
// console.log(res);
// })();
// }

let data = [];

d3.select("button").on("click", async function() {
Expand All @@ -33,13 +11,10 @@ d3.select("button").on("click", async function() {

let repo = document.getElementById('repo').value
repo = repo == '' ? 'torvalds/linux' : repo;
console.log(repo);
/*console.log(repo);*/

const starHistory = await getStarHistory(repo).catch(function(err) {
console.log(err);
alert(`Sorry, Git API rate limit exceeded for your ip address, please wait for an hour`);
});
console.log(starHistory);
const starHistory = await getStarHistory(repo).catch(err => { alert(err); });
/*console.log(starHistory);*/

// 新数据集
data.push({
Expand All @@ -51,7 +26,7 @@ d3.select("button").on("click", async function() {
}
}),
});
console.log(JSON.stringify(data));
/*console.log(JSON.stringify(data));*/

nv.addGraph(function() {
var chart = nv.models.lineChart()
Expand Down Expand Up @@ -79,5 +54,5 @@ d3.select("button").on("click", async function() {

document.getElementById('theBtn').removeAttribute("disabled");
document.getElementById('theGif').style.display = 'none';
console.log('hi');
/*console.log('hi');*/
});
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
entry: './src/index.js',
/*devtool: 'inline-source-map', // for source map*/
output: {
path: './dist',
filename: 'bundle.js'
Expand Down

0 comments on commit 39a253c

Please sign in to comment.