-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (43 loc) Β· 1.46 KB
/
index.js
File metadata and controls
47 lines (43 loc) Β· 1.46 KB
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
const scrapeIt = require('scrape-it');
const fetch = require('node-fetch');
const urls = require('./recipes.json');
const API_ENDPOINT = 'https://recipes-api.fknussel.com/recipes';
const API_TOKEN = '';
const scraperOptions = {
title: {
selector: '.header h2',
convert: str => str.replace('β ', '')
},
image: {
selector: '.content img.image',
attr: 'src'
}
};
const scraperPromises = urls
.map(url => scrapeIt(url, scraperOptions))
.map(scraperPromise => scraperPromise.then(({ data, response }) => ({
title: data.title,
image_url: data.image,
source_url: response.responseUrl
})))
.map(scraperPromise => scraperPromise.catch(err => {
console.log('π¨ Error while scraping site:', err);
}));
const apiPromises = scraperPromises
.map(scraperPromise => scraperPromise.then(payload => {
return fetch(API_ENDPOINT, {
method: 'post',
headers: {
'Authorization': `Bearer ${API_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
}))
.map(fetchPromise => fetchPromise.then(response => response.json()))
.map(fetchPromise => fetchPromise.then(json => {
console.log(`β
[${json.meta.status}] ${json.data.title}`);
}))
.map(fetchPromise => fetchPromise.catch(err => {
console.log('π¨ Error while posting to API:', err);
}));