forked from simontegg/promises
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nested.js
51 lines (41 loc) · 1.28 KB
/
nested.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
'use strict';
const Promise = require('bluebird')
const fs = require('fs')
const pg = require('pg')
const request = require('superagent')
const cheerio = require('cheerio')
const Url = require('url')
const R = require('ramda')
const handleError = (err) => {
console.log('Oh no! An error:', err)
}
const extractUrls = R.compose(
R.uniq,
R.filter(href => R.match(/http/g, href).length > 0),
R.map(anchor => anchor.attribs.href),
R.filter(anchor => anchor.attribs && anchor.attribs.href),
R.values
)
fs.readFile(__dirname + '/data/urls.json', 'utf8', (err, urls) => {
if (err) { handleError(err) }
R.forEach((url) => {
let hostname = Url.parse(url).hostname
request
.get(url)
.end((err, data) => {
if (err) { handleError(err) }
let fileName = hostname + '.json'
let $ = cheerio.load(data.text)
let links = R.filter(u => {
let urlObj = Url.parse(u)
return urlObj.hostname !== hostname
}, extractUrls($('a')))
let linkFile = JSON.stringify(links, null, '\n')
console.log(links)
fs.writeFile(__dirname + '/data/' + fileName, linkFile, 'utf8', function (err) {
if (err) { handleError(err) }
console.log(fileName + ' written!')
})
})
}, JSON.parse(urls))
})