forked from xdesro/personalsit.es
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildSites.js
72 lines (67 loc) · 2.05 KB
/
buildSites.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const fs = require('fs');
const http = require('http');
const cloudinary = require('cloudinary').v2;
const frontMatter = require('front-matter');
const captureWebsite = require('capture-website');
const filenamifyUrl = require('filenamify-url');
const { resolve } = require('path');
require('dotenv').config();
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
const getSites = () => {
const files = fs.readdirSync('sites').map((file) => {
return fs.readFileSync(`sites/${file}`, 'utf-8');
});
const urls = files
.map((file) => {
const parsedFile = frontMatter(file);
const { url } = parsedFile.attributes;
return { title: filenamifyUrl(url, { replacement: '' }), url };
})
.filter((site) => {
const { title, url } = site;
if (title == undefined || url == undefined) return false;
return true;
});
return urls;
};
const getScreenshot = async (site) => {
// TODO — Check if screenshot exists in Cloudinary space before running Puppeteer
// TODO — Statically render URLs in template files
const doesntExist = await new Promise((resolve, reject) => {
http.get(cloudinary.url(site.title), (response) => {
if (response.statusCode !== 404) {
console.log(`${site.title} exists on cloudinary.`);
} else {
console.log(`${site.title} DOES NOT exist on cloudinary.`);
}
resolve(response.statusCode === 404);
});
});
if (doesntExist) {
try {
const screenshot = await captureWebsite.buffer(site.url, { timeout: 5 });
cloudinary.uploader
.upload_stream({ resource_type: 'image', public_id: site.title })
.end(screenshot);
} catch (error) {
console.error(site.title, error);
}
}
};
(async () => {
const sites = await getSites();
(async () => {
for (const site of sites) {
await getScreenshot(site);
}
console.log(`
=====
Screenshots collection complete.
=====`);
})();
return;
})();