-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (83 loc) · 2.54 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import fs from 'node:fs';
import axios from 'axios';
import fetch from 'node-fetch';
import { parse } from 'node-html-parser';
const dir = './memes'; // name of the directory/folder
if (!fs.existsSync(dir)) {
// check if folder already exists
fs.mkdirSync(dir); // creating folder
}
// Download HTML from meme website as a string (body is a string)
const downloadHTML = await fetch(
'https://memegen-link-examples-upleveled.netlify.app/',
);
const body = await downloadHTML.text();
// Convert string "body" into an object
const root = parse(body);
// Get only <img /> elements from the whole of html
const onlyImageTags = root.querySelectorAll('img');
// Get the first 10 image urls from all <img /> elements
const tenImages = [];
for (let i = 0; i < 11; i++) {
tenImages.push(onlyImageTags[i]['rawAttrs']);
}
// Trim the strings inside the array
function trimUrl(arrOfStrings) {
return arrOfStrings.map((str) => {
str = str.trim();
str = str.slice(5, -1);
return str;
});
}
const bareUrl = trimUrl(tenImages);
// Use Axios to download images
async function download(url, filepath) {
const response = await axios({
url,
method: 'GET',
responseType: 'stream',
});
return new Promise((resolve, reject) => {
response.data
.pipe(fs.createWriteStream(filepath))
.on('error', reject)
.once('close', () => {
resolve(filepath);
});
});
}
// Create local paths for 10 images in memes folder
const paths = [];
function createPathForImage() {
for (let i = 0; i < 9; i++) {
paths.push(`./memes/0${i + 1}.jpg`);
}
paths.push('./memes/10.jpg');
return paths;
}
createPathForImage();
// download images and save them in memes folder
for (let i = 0; i < 10; i++) {
const imgLink = bareUrl[i];
const filepath = paths[i];
await download(imgLink, filepath);
}
// Stretch goal to download the Gandalf meme per user request
let memeRequest = process.argv[3]; // string "gandalf"
if (memeRequest) {
memeRequest = memeRequest.toLowerCase();
}
let gandalfLink;
for (let i = 0; i < 143; i++) {
if (onlyImageTags[i]['rawAttrs'].includes('gandalf')) {
gandalfLink = onlyImageTags[i]['rawAttrs'];
}
}
gandalfLink = gandalfLink.trim();
gandalfLink = gandalfLink.slice(5, -1);
const pathToGandalf = './memes/gandalf.jpg';
if (memeRequest === 'gandalf') {
await download(gandalfLink, pathToGandalf);
} else if (!memeRequest) {
console.log('Please type <hello gandalf> to download an extra meme');
}