forked from simontegg/promises
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnamed-callbacks.js
38 lines (31 loc) · 1005 Bytes
/
named-callbacks.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
import Promise from 'bluebird'
import fs from 'fs'
import request from 'request'
import cheerio from 'cheerio'
import { foreach, map, pipe } from 'ramda'
const fetchUrlsFromFile = (filePath, callback) => {
fs.readFile(__dirname + filePath, 'utf8', function (err, data) {
if (err) {
callback(err)
} else {
foreach((url) => { request(url, callback) }, JSON.parse(data))
}
})
}
const formatLinkFile = (anchors) => R.compose(
(links) => JSON.stringify(links, null, '\n '),
map((anchor) => { return anchor.attribs.href }, anchors)
)
fetchUrlsFromFile('/data/urls.json', function (err, data) {
if (err) {
console.log('Oh no! An error: ', err)
} else if (data) {
var fileName = data.request.host + '.json'
var $ = cheerio.load(data.body)
var linkFile = formatLinkFile($('a'))
fs.writeFile(__dirname + '/data/' + fileName, linkFile, 'utf8', function (err) {
if (err) { handleError(err) }
console.log(fileName + ' written!')
})
}
})