-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
163 lines (153 loc) · 4.11 KB
/
app.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
const fs = require('fs');
const util = require('util');
const xray = require('x-ray')();
const URL = 'https://developer.mozilla.org/en-US/docs/Web/CSS/Reference';
const NOT_FOUND = '';
const OUTPUT = './output.json';
const TIMEOUT = 3000;
getItems(URL)
.then(getDefinitions)
.then(formatResults)
.then((results) => {
console.log('Crawling is succeeded!');
fs.writeFile(OUTPUT, JSON.stringify(results, null, '\t'), 'utf8', (err) => {
if (err) {
throw err;
}
console.log('Building an output file: ', OUTPUT);
});
}).catch((err) => {
console.error('err >> ', err);
});
/**
* getItems
* 引数として受け取ったurlにアクセスし、
* aタグの情報をオブジェクトにまとめる
*
* @param url
* @return {Promise}
*/
function getItems(url) {
return new Promise((resolve, reject) => {
xray(url, '.index a', [{
type: 'code@text',
link: '@href'
}])((err, obj) => {
if (err) {
return reject(err);
}
else if (isEmptyObject(obj) || !obj) {
return reject(new Error('The target is not found.'));
}
resolve(obj);
});
});
}
/**
* getDefinitions
* itemsに格納されたlink情報を元に5件ずつクローリングを行う
* TODO: クローリングを指定する文字列を引数に追加する
*
* @param items {type: 'string', link: 'string'} を格納した配列
* @return {Promise}
*/
function getDefinitions(items) {
if (!util.isArray(items)) {
throw new Error('items must be Array');
}
console.log('All Items: ', items.length);
let results = [];
let completed = 0;
const request = () => {
const process = [];
const limit = 5 + completed;
let offset = completed;
while (offset < limit && offset < items.length) {
const promise = getDefinition(items[offset]);
if (promise) {
process.push(promise);
}
offset++;
}
completed = offset;
console.log('The request has been completed: ', completed);
return Promise.all(process).then((res) => {
results = results.concat(res);
if (completed < items.length) {
return request();
}
return Promise.resolve(results);
});
};
return request();
}
/**
* getDefinition
* itemに格納されたlinkを元にクローリングを行う
* TODO: クローリングを指定する文字列を引数に追加する
*
* @param item {type: 'string', link: 'string'} を格納した配列
* @return {Promise} || {} linkが存在しない場合は何も行わない
*/
function getDefinition(item) {
if (!item.link) {
throw new Error('link property does not exist for item');
}
return new Promise((resolve, reject) => {
let timerId = setTimeout(function() {
console.log("Request timeout:", item.type);
result.type = item.type;
result.definitions = [NOT_FOUND];
resolve(result);
}, 3000);
let result = {};
xray(item.link, '#Values + dl', ['code@text'])((err, definitions) => {
clearTimeout(timerId);
if (err) {
return reject(err);
}
if (!definitions) {
definitions = [NOT_FOUND];
}
result.type = item.type;
result.definitions = definitions;
resolve(result);
});
});
}
/**
* formatResults
* クローリングされたdefinitionsから<, >, ', /といった文字列を取り除く
*
* @param results
* @return {Promise} || {} 引数がArrayではない場合、なにもしない
*/
function formatResults(results) {
if (!util.isArray(results)) {
throw new Error('results must be Array');
}
return new Promise((resolve, reject) => {
results = results.filter((result) => {
if (result.definitions.length < 1) {
return null;
}
let formatted = result.definitions.join(', ')
.trim()
.replace(/<|\/|>|'/g, '');
result.definitions = formatted;
return result;
});
resolve(results);
});
}
/**
* isEmptyObject
* 空のオブジェクトを判定する
*
* @param obj
* @return {Bool}
*/
function isEmptyObject(obj) {
return !Object.keys(obj).length;
}