-
Notifications
You must be signed in to change notification settings - Fork 2
/
crawler.js
executable file
·147 lines (145 loc) · 5.34 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const fetch = require('node-fetch');
const { Client } = require('pg');
const cheerio = require('cheerio');
const config = require('./config.json');
const HttpsProxyAgent = require('https-proxy-agent');
var proxyAgent = undefined;
if (config.proxy) {
proxyAgent = new HttpsProxyAgent(config.proxy);
}
var sql_config;
if (config.sql) {
sql_config = config.sql;
} else {
var url = process.env.DATABASE_URL;
url = url.replace("pgsql://", "");
url = url.replace(":", "/");
url = url.replace("@", "/");
url = url.split("/");
console.log(url);
sql_config = {
"user": url[0],
"host": url[2],
"database": url[3],
"password": url[1],
"port": 5432
}
}
const client = new Client(sql_config);
var all_data = { "slack": {}, "hackathon": {}, "summit": {}, "fanpage": {}, "github": {} };
const arrayOfPromises = [
new Promise((resolve, reject) => {
let to = setTimeout(() => {
console.log("slack api timeout");
resolve(1);
}, 15000);
fetch(config.slack_api)
.then(res => res.text())
.then(body => {
//console.log(body);
all_data['slack'] = JSON.parse(body);
console.log("slack api get!");
clearTimeout(to);
resolve(1);
})
}),
new Promise((resolve, reject) => {
let to = setTimeout(() => {
console.log("g0v database timeout");
resolve(2);
}, 30000);
fetch(config.g0v_database)
.then(res => res.text())
.then(body => {
var $ = cheerio.load(body);
if ($('tr').length > 0) {
let counter = 0;
for (let row_id = 0; row_id < $('tr').length; row_id++) {
if ($($('td', $('tr')[row_id])[2]).text().length > 0) {
counter++;
}
}
counter--; // remove head row
all_data['hackathon']['proposals'] = counter;
var last_row = $($('tr')[counter]);
all_data['hackathon']['count'] = Number($($('td', last_row)[1]).text()) + 1;
all_data['hackathon']['current_title'] = $($('td', last_row)[2]).text();
//console.log(all_data['hackathon']);
console.log("g0v database get!");
}
else {
console.log("g0v database get error");
}
clearTimeout(to);
resolve(2);
});
}),
new Promise((resolve, reject) => {
let to = setTimeout(() => {
console.log("fb fanpage timeout");
resolve(3);
}, 15000);
fetch(config.g0v_fanpage, { headers: { 'Accept-Language': 'zh-TW' }, agent: proxyAgent })
.then(res => res.text())
.then(body => {
//console.log(body);
var $ = cheerio.load(body);
var like = follow = null;
var like_match = $("body").text().match('[0-9,]* 個讚');
var follow_match = $("body").text().match('[0-9,]* 人在追蹤');
if (like_match != null)
like = Number(like_match[0].replace(",", "").replace(" 個讚", ""));
if (follow_match != null)
follow = Number(follow_match[0].replace(",", "").replace(" 人在追蹤", ""));
if (like == null || follow == null) {
throw new Error("g0v fanpage get error!!");
}
else {
all_data['fanpage'] = {
"like": like,
"follow": follow
}
console.log("g0v fanpage get!");
}
clearTimeout(to);
resolve(3);
})
}),
new Promise((resolve, reject) => {
let to = setTimeout(() => {
console.log("github api timeout");
resolve(4);
}, 15000);
fetch('https://api.github.com/orgs/g0v', {
"header": {
"Accept": "application/vnd.github.v3+json"
}
})
.then(res => res.text())
.then(body => {
var _org = JSON.parse(body);
all_data['github']['repos'] = _org.public_repos;
console.log("github api get!");
clearTimeout(to);
resolve(4);
});
})
]
client.connect();
client.query(`SELECT * FROM dashboard.counter ORDER BY create_at DESC LIMIT 1;`, (error, result) => {
if (error) {
console.log(error.stack);
} else {
all_data = result.rows[0]['data'];
console.log("old data: ", all_data);
Promise.all(arrayOfPromises).then(() => {
all_data['summit'] = { "count": 5 };
console.log("new data: ", all_data);
client.query(`INSERT INTO dashboard.counter VALUES(\'${JSON.stringify(all_data)}\');`, (err, res) => {
console.log(err, res);
client.end();
throw "end";
});
});
}
});