-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse-link-page.js
98 lines (78 loc) · 1.92 KB
/
parse-link-page.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
'use strict';
var forEachAsync = require('foreachasync').forEachAsync
;
function parse(data) {
var line = data.replace(/[\s\S]*<font size="10">([\s\S]*?)<\/font>[\s\S]*/, '$1').trim()
, link
, name
;
line = line
.replace(/<\/a>/g, ' ')
.replace(/<br>/g, ' ')
.replace(/\n/g, ' ')
.replace(/\s+/g, ' ')
;
//console.log('[line]', line);
link = line.replace(/.*href="(.*?)".*/, '$1');
line = line.replace(/<a.*?>/, ' ').replace(/\s+/g, ' ');
if (!/http/.test(link)) {
link = '';
}
name = line.replace(/.*Phone Number is\s+([^<]+).*/, '$1').trim();
//console.log('[link]', link);
//console.log('[name]', name);
return { link: link, name: name };
}
function parseAll(buffers) {
var carriers = []
;
buffers.forEach(function (buffer) {
var obj = parse(buffer)
;
carriers.push(obj);
});
return carriers;
}
function build(dirpath, cb) {
var fs = require('fs')
;
fs.readdir(dirpath, function (err, nodes) {
var carriers = []
;
forEachAsync(nodes, function (next, node) {
if (!/\.html$/.test(node)) {
next();
return;
}
var filepath = dirpath + '/' + node
;
console.log(filepath);
fs.readFile(filepath, 'utf8', function (err, data) {
var obj
;
obj = parse(data);
obj.carrier = node.replace(/.html/, '');
carriers.push(obj);
console.log('');
next();
});
}).then(function () {
cb(null, carriers);
});
});
}
module.exports.parse = parse;
module.exports.parseAll = parseAll;
module.exports.build = build;
function run() {
var fs = require('fs')
;
build(process.argv[2] || 'data/carriers', function (err, carriers) {
fs.writeFile('carriers.json', JSON.stringify(carriers, null, ' '), 'utf8', function () {
console.log('All Done', 'carriers.json');
});
});
}
if (require.main === module) {
run();
}