-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.js
51 lines (42 loc) · 1.12 KB
/
process.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
const fs = require('fs');
const axios = require('axios').default;
const cheerio = require('cheerio');
const slugify = require('slugify');
const url = 'https://www.joelsternfeld.net/artworks/2018/3/25/sweet-earth-experimental-utopias-in-america'
const fp = 'joelsternfeld-post.html'
// axios.get(url).then(res => {
// fs.writeFileSync(fp, res.data)
// });
const html = fs.readFileSync(fp)
const $ = cheerio.load(html)
var out = {
title: 'Sweet Earth by Joel Sternberg',
sources: [{
path: url
}],
resources: []
}
$('.slide').each((i,el) => {
// console.log($(el).html())
var title = $('.meta-title', el).text()
var desc = $('.meta-description', el).text()
var img = $('img', el).attr('data-src')
var item = {
title: title,
desc: desc,
img: img,
img_cache: 'img/' + slugify(title, {lower: true}) + '.jpg'
}
out.resources.push(item)
})
fs.writeFileSync('datapackage.json', JSON.stringify(out, null, 2))
out.resources.forEach(item => {
axios({
method: 'get',
url: item.img,
responseType:'stream'
})
.then(res => {
res.data.pipe(fs.createWriteStream(item.img_cache));
})
})