Skip to content

Commit

Permalink
modularized
Browse files Browse the repository at this point in the history
  • Loading branch information
x3388638 committed Jul 17, 2018
1 parent 62e54ae commit 4786124
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 72 deletions.
41 changes: 0 additions & 41 deletions Crawler.js

This file was deleted.

60 changes: 36 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
const express = require('express');
const app = express();
const http = require('http').Server(app);
const bodyParser = require('body-parser');
const cors = require('cors');
const Crawler = require('./Crawler');
const request = require('request');
const cheerio = require('cheerio');

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
function fetch(account, raw) {
return new Promise((resolve, reject) => {
request.get(`https://github.com/${ account }`, (err, res, body) => {
if (err) {
reject(err);
return;
}

app.get('/graph/:account', cors(), (req, res) => {
const { account } = req.params;
Crawler.fetch(account).then((graph) => {
res.send(graph);
});
});
resolve(body);
});
}).then((document) => {
const $ = cheerio.load(document);
const $calendar = $('.calendar-graph');
if (raw) {
const result = {};
$calendar.find('svg > g > g').each((i, week) => {
$(week).find('rect').each((i, day) => {
result[$(day).data('date')] = $(day).data('count');
});
});

return Promise.resolve(result);
}

const $container = $(`<div></div>`).append($calendar);
const graph = `<div>
<style>
.calendar-graph text.month { font-size: 10px; fill: #767676; }
.calendar-graph text.wday { font-size: 9px; fill: #767676; }
</style>
${ $container.html() }
`;

app.get('/data/:account', cors(), (req, res) => {
const { account } = req.params;
Crawler.fetch(account, true).then((data) => {
res.json(data)
return Promise.resolve(graph);
});
});
}

http.listen(7774, function () {
console.log('listening on 127.0.0.1:7774');
});
module.exports = { fetch };
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"name": "github-calendar-graph-crawler",
"version": "1.0.0",
"description": "",
"name": "github-calendar-graph",
"version": "0.1.0",
"description": "Get GitHub contribution calendar graph",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "ISC",
"author": "Y.Y. Chang",
"license": "MIT",
"repository": {
"url": "git@github.com:x3388638/github-calendar-graph.git",
"type": "git"
},
"dependencies": {
"body-parser": "^1.18.3",
"cheerio": "^1.0.0-rc.2",
Expand Down

0 comments on commit 4786124

Please sign in to comment.