-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
64 lines (56 loc) · 2.09 KB
/
crawler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
// var config = require('./config.json');
var github_url = 'https://github.com';
var trand_list = [];
var filename = 'data.json';
var init = function(cb){
cb();
}
var github_trand_kr = function(){
request({url : 'https://github.com/trending?since=monthly' , timeout: 100000}, function(error, response, body){
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
var list = $('.repo-list-item');
for(var i=0; i<list.length; i++){
var data = {
"no" : ("00" + (i+1)).slice(-2)
, "title" : $(list[i]).children('.repo-list-name').text().replace(/\n/gi,"").replace(/(\s*)/g,"")
, "desc" : $(list[i]).children('.repo-list-description').text().replace(/\n/gi,"").replace(/(\s{2,})/g,"")
, "url" : github_url + $(list[i]).children('.repo-list-name').children('a').attr('href')
, "etc" : $(list[i]).children('.repo-list-meta').text().replace(/\n/gi,"").replace(/(\s{2,})/g,"").replace("•Built by","").replace(" this month","")
, "owner" : {}
}
trand_list.push(data);
var pname = data.title.split('/')[1];
var content = '# ' + pname + '\n\n- 페이지 링크: '+ data.url;
var fname = '022-'+ data.no + '-' + pname +'.md';
write_md(fname,content);
}
console.log(trand_list);
write_json(trand_list);
} else {
console.log("error project : " + url);
console.log("error : " +error);
if(!error){
console.log("status code : " +response.statusCode);
}
cb();
}
});
}
var write_json = function(_json, cb){
fs.writeFile(filename, JSON.stringify(_json), function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
}
var write_md =function(fname, content, cb){
fs.writeFile(fname, content, function (err) {
if (err) throw err;
console.log(fname + 'It\'s saved!');
});
}
init(github_trand_kr);