-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (40 loc) · 1.16 KB
/
index.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
'use strict';
const got = require('got');
const cheerio = require('cheerio');
const linkSplitter = data => {
return data.split('<a href="image')[1].split('"')[0];
};
const apodNasa = () => {
const url = 'https://apod.nasa.gov/';
return got(url).then(res => {
const $ = cheerio.load(res.body);
const link = linkSplitter(res.body);
return {
title: $('center').eq(1).text().split('\n')[1].trim(),
image: `https://apod.nasa.gov/apod/image${link}`
};
}).catch(error => {
if (error) {
error.message = 'Content is unavailable or is in unsupported format!';
}
return error.message;
});
};
const apod = module.exports = () => apodNasa(); // eslint-disable-line no-multi-assign
apod.date = space => {
const url = `https://apod.nasa.gov/apod/ap${space}.html`;
return got(url).then(res => {
const $ = cheerio.load(res.body);
const link = linkSplitter(res.body);
return {
day: $('title').text().split('-')[0].split(': ')[1].trim(),
title: $('title').text().split('-')[1].trim(),
image: `https://apod.nasa.gov/apod/image${link}`
};
}).catch(error => {
if (error) {
error.message = 'Content is unavailable';
}
return error.message;
});
};