forked from maxthyen/table-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (28 loc) · 1.04 KB
/
index.js
File metadata and controls
30 lines (28 loc) · 1.04 KB
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
'use strict';
var request = require('request');
var xray = require('x-ray')();
var tabletojson = require('tabletojson');
module.exports.get = function get(url) {
return new Promise(function(resolve, reject) {
request.get(url, function(err, response, body) {
if (err) {
return reject(err);
}
if (response.statusCode >= 400) {
return reject(new Error('The website requested returned an error!'));
}
xray(body, ['table@html'])(function (conversionError, tableHtmlList) {
if (conversionError) {
return reject(conversionError);
}
resolve(tableHtmlList.map(function(table) {
// xray returns the html inside each table tag, and tabletojson
// expects a valid html table, so we need to re-wrap the table.
// Returning the first element in the converted array because
// we should only ever be parsing one table at a time within this map.
return tabletojson.convert('<table>' + table + '</table>')[0];
}));
});
})
});
};