-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
index.js
106 lines (91 loc) · 2.25 KB
/
index.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
'use strict'
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const got = require('got')
const fs = require('fs')
const path = require('path')
const SCRAPERS = require('./scrapers')
const URLS = require('./urls')
/**
* Run.
*/
console.log('Fetching the results for each scraper...')
getResults()
.then(results => {
console.log('Removing the old results...')
const dir = path.resolve(__dirname, 'results')
rimraf.sync(dir)
results.forEach(result => {
const file = path.resolve(dir, `${result.name}.json`)
const string = JSON.stringify(result.results, null, 2)
mkdirp.sync(dir)
fs.writeFileSync(file, string)
})
console.log('Success! The results have been compiled for each scraper.')
})
.catch(err => {
console.log('An error occurred:')
console.log()
console.log(err)
console.log()
console.log(err.stack)
})
/**
* Get the metadata results.
*
* @return {Promise} results
*/
function getResults () {
return getHtmls(URLS).then(htmls => {
return getScrapersResults(SCRAPERS, URLS, htmls).then(results => results)
})
}
/**
* Get the metadata results from all `SCRAPERS` and `urls` and `htmls`.
*
* @param {Array} SCRAPERS
* @param {Array} urls
* @param {Array} htmls
* @return {Promise} results
*/
function getScrapersResults (SCRAPERS, urls, htmls) {
return Promise.all(
SCRAPERS.map(SCRAPER => {
return getScraperResults(SCRAPER, urls, htmls).then(results => {
return {
name: SCRAPER.name,
results
}
})
})
)
}
/**
* Get metadata results from a single `SCRAPER` and `urls` and `htmls`.
*
* @param {Object} SCRAPER
* @param {Array} urls
* @param {Array} htmls
* @return {Promise} results
*/
function getScraperResults (SCRAPER, urls, htmls) {
return Promise.all(
urls.map((url, i) => {
const html = htmls[i]
const name = SCRAPER.name
const Module = require(name)
return SCRAPER.scrape(Module, url, html).then(metadata => {
return SCRAPER.normalize(metadata)
})
})
)
}
/**
* Get html from a list of `urls`.
*
* @param {Array} urls
* @return {Promise} htmls
*/
function getHtmls (urls) {
return Promise.all(urls.map(url => got(url).then(res => res.body)))
}