-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpromises.js
54 lines (43 loc) · 1.21 KB
/
promises.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
'use strict';
const Promise = require('bluebird')
const fs = require('fs')
const request = require('superagent')
const cheerio = require('cheerio')
const each = require('lodash.foreach')
const map = require('lodash.map')
const R = require('ramda')
const Url = require('url')
const filePath = __dirname + '/data/urls.json'
var readFileAsync = Promise.promisify(fs.readFile)
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
)
const requestAsync = (url) => {
return new Promise((resolve, reject) => {
let hostname = Url.parse(url).hostname
request
.get(url)
.end((err, data) => {
if (err) { reject(err) }
var $ = cheerio.load(data.text)
var links = R.filter(u => {
let urlObj = Url.parse(u)
return urlObj.hostname !== hostname
}, extractUrls($('a')))
resolve(links)
})
})
}
readFileAsync(filePath)
.then(function (data) {
var urls = JSON.parse(data)
return Promise.map(urls, requestAsync)
})
.then(function (results) {
console.log('result', results)
})
// })