-
Notifications
You must be signed in to change notification settings - Fork 0
/
getImagesFromUnplash.js
197 lines (166 loc) · 5.05 KB
/
getImagesFromUnplash.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const puppeteer = require("puppeteer");
const prompt = require("prompt-async");
const fs = require("fs");
const axios = require("axios");
// -----------------
// USER INPUT CONFIG
// -----------------
var promptSchema = {
properties: {
search: {
pattern: /^[a-zA-Z\s\-]+$/,
description: "What is the search term(s)?",
required: true
},
number: {
pattern: /^(0|[1-9][0-9]*)$/,
description: "How many results should we download?",
required: true
},
folder: {
pattern: /^[a-zA-Z\s\-]+$/,
description: "Which folder should we save these images to?",
required: true
}
}
};
const imageLibraryPath = "./unsplashimages";
// create the folder for images if it doesn't yet exist
if (!fs.existsSync(`${imageLibraryPath}`)) {
fs.mkdirSync(`${imageLibraryPath}`);
}
// -------------
// MAIN FUNCTION
// -------------
(async () => {
console.log(
`
Welcome to the Unsplash image scraper, let's get some nice images! ...
`
);
await sleep(500);
prompt.start();
const promptResult = await prompt.get(promptSchema);
console.log(
`
Now let's go download the top ${promptResult.number} results from "${promptResult.search}" unsplash search...
`
);
await sleep(1000);
console.log(
`
Obtaining results from unsplash.com/search/${slugify(promptResult.search)}
`
);
const browser = await puppeteer.launch({ headless: true }); // default is true
const page = await browser.newPage();
await page.goto(
`https://www.unsplash.com/search/${slugify(promptResult.search)}`,
{ waitUntil: "networkidle2" }
);
let imageUrls = new Array(Number(promptResult.number));
await asyncForEach(imageUrls, async (_, i) => {
// Select the element to begin
let nthImageElement = (await page.$$("a[itemprop='contentUrl'] img"))[i];
// Scroll to it
if (nthImageElement) {
try {
await page.evaluate(el => {
try {
el.scrollIntoView();
} catch (e) {
console.log(e);
}
}, nthImageElement);
} catch (e) {
console.log(e);
}
// reselect the element now that we've scrolled to it
nthImageElement = (await page.$$("a[itemprop='contentUrl'] img"))[i];
// grab the imageurls from the srcset attribute of element
const value = await (await nthImageElement.getProperty(
"srcset"
)).jsonValue();
// Chop the single url and store in our global array at position i
imageUrls[i] = value.split("?")[0];
}
});
// Clean the url list in case any elements are empty
imageUrls = imageUrls.filter(url => url);
console.log(
`
Downloading images to ${imageLibraryPath}/${promptResult.folder}/
`
);
await Promise.all(downloadImagesFromUrlList(imageUrls, promptResult.folder));
console.log(
`
All images downloaded
`
);
await browser.close();
})();
// ----------------
// HELPER FUNCTIONS
// ----------------
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
async function downloadImage(url, imagePath, index) {
try {
const response = await axios({
url,
responseType: "stream"
});
return new Promise((resolve, reject) => {
response.data
.pipe(fs.createWriteStream(imagePath))
.on("finish", () => {
console.log(` image ${index} download complete `);
resolve();
})
.on("error", e => reject(e));
});
} catch (e) {
console.log(e);
}
}
function downloadImagesFromUrlList(urlArray, folderName) {
// create the folder for images if it doesn't yet exist
if (!fs.existsSync(`${imageLibraryPath}/${folderName}`)) {
fs.mkdirSync(`${imageLibraryPath}/${folderName}`);
}
return urlArray.map((url, index) => {
// name should be the last part of url
const urlSplit = url.split("/");
const name = urlSplit[urlSplit.length - 1];
// download this image and put in the folder
return downloadImage(
url,
`${imageLibraryPath}/${folderName}/${name}.jpg`,
index
);
});
}
function slugify(string) {
const a =
"àáäâãåăæąçćčđďèéěėëêęğǵḧìíïîįłḿǹńňñòóöôœøṕŕřßşśšșťțùúüûǘůűūųẃẍÿýźžż·/_,:;";
const b =
"aaaaaaaaacccddeeeeeeegghiiiiilmnnnnooooooprrsssssttuuuuuuuuuwxyyzzz------";
const p = new RegExp(a.split("").join("|"), "g");
return string
.toString()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(p, c => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, "-and-") // Replace & with 'and'
.replace(/[^\w\-]+/g, "") // Remove all non-word characters
.replace(/\-\-+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, ""); // Trim - from end of text
}