forked from DMarby/unsplash-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
375 lines (301 loc) · 10.2 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
var async = require('async')
var commander = require('commander')
var cheerio = require('cheerio')
var request = require('request')
var fs = require('fs')
var exec = require('child_process').exec
var pretty = require('prettysize')
var archiver = require('archiver')
var pjson = require('./package.json')
var path = require('path')
var noConfig = false
var changedMetadata = false
try {
var config = require('./config')
if (!config.concurrent_downloads || !config.folder_path || !config.git_push || !config.all) {
noConfig = true
}
} catch (error) {
var config = {}
noConfig = true
}
var headers = {
'User-Agent': 'Unsplash-Downloader'
}
if (noConfig) {
commander
.version(pjson.version)
.option('-c, --concurrent_downloads <amount>', 'Amount of concurrent downloads allowed', 5)
.option('-f, --folder_path <path>', 'Folder path of where to download the photos', 'photos')
.option('-g, --git_push', 'Automatically commit & push to git repo in photos folder path')
.option('-a, --all', 'Download all images on the front page rather than just the featured ones')
.option('-C, --check_for_deleted', 'Check if an image has been deleted/re-added, and update the metadata.')
.parse(process.argv)
}
var concurrent_downloads = !config.concurrent_downloads ? commander.concurrent_downloads : config.concurrent_downloads
var folder_path = !config.folder_path ? commander.folder_path : config.folder_path
var git_push = !config.git_push ? commander.git_push : config.git_push
var all = !config.all ? commander.all : config.all
var check_for_deleted = !config.check_for_deleted ? commander.check_for_deleted : config.check_for_deleted
if (!concurrent_downloads || !folder_path) {
commander.help()
}
var highestId = 0
try {
var metadata = require(folder_path + '/metadata.json')
metadata.forEach(function (image) {
if (image.id >= highestId) {
highestId = image.id
}
})
} catch (error) {
var metadata = []
}
var getPageCountAndDownload = function () {
getPageCount(function (pageCount) {
async.times(pageCount, function (page, next) {
getImageInfo(page += 1, function (error, imageInfo) {
if (error) {
return next(error)
}
next(null, imageInfo)
})
}, function (error, imageInfo) {
if (error) {
return console.error('Error getting page count', error)
}
var imagesToDownload = []
imageInfo.forEach(function (imageInfoList) {
imagesToDownload = imagesToDownload.concat(imageInfoList)
})
prepareToDownloadImages(imagesToDownload)
})
})
}
var root_url = all ? 'https://unsplash.com/new' : 'https://unsplash.com'
var getPageCount = function (callback) {
var highestPage = 0
request(root_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body)
$('.pagination a').each(function (index, element) {
var linkText = $(this).text()
if (!isNaN(parseInt(linkText)) && (parseInt(linkText) > highestPage)) {
highestPage = parseInt(linkText)
}
})
callback(highestPage)
}
})
}
var imageAlreadyExists = function (imageMetadata) {
var images = metadata.filter(function (image) {
return image.image_url === imageMetadata.image_url
})
if (images.length) {
return images[0]
}
return false
}
var getImageInfo = function (page, callback) {
var imageInfo = []
request(root_url + '?page=' + page, function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body)
$('.photo-container').each(function (index, element) {
var url = $(this).find('.photo a').attr('href')
var image_url = $(this).find('.photo-description__download a').attr('href')
var imageMetadata = {
post_url: 'https://unsplash.com' + url,
image_url: 'https://unsplash.com' + image_url,
unsplash_id: url.replace('/photos/', ''),
page: page
}
var the_author = $(this).find('.photo-description__author h2 a').first()
imageMetadata.author = the_author && the_author.text() ? removeSpaces(the_author.text()) : 'Unknown'
if (imageMetadata.author) {
imageMetadata.author_url = 'https://unsplash.com' + the_author.attr('href')
}
if (!imageMetadata.image_url) {
console.log('Could not find image url for ' + post_url)
} else {
var exists = imageAlreadyExists(imageMetadata)
if (exists) {
var currentMetadata = metadata[metadata.indexOf(exists)]
if (currentMetadata.post_url !== imageMetadata.post_url || currentMetadata.author !== imageMetadata.author || currentMetadata.author_url !== imageMetadata.author_url) {
changedMetadata = true
metadata[metadata.indexOf(exists)].post_url = imageMetadata.post_url
metadata[metadata.indexOf(exists)].author = imageMetadata.author
metadata[metadata.indexOf(exists)].author_url = imageMetadata.author_url
}
} else {
imageInfo.push(imageMetadata)
}
}
})
callback(null, imageInfo)
} else {
callback(error ? error : response.statusCode)
}
})
}
var removeSpaces = function (str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '')
}
var prepareToDownloadImages = function (imagesToDownload) {
if (!imagesToDownload.length) {
console.log('Nothing to download!')
if (check_for_deleted) {
checkForDeletedImages()
} else {
if (changedMetadata) {
postDownloadTasks()
}
}
return
}
imagesToDownload.reverse()
var imagesToDownloadWithId = []
for (var i in imagesToDownload) {
var image = imagesToDownload[i]
image.id = ++highestId
imagesToDownloadWithId.push(image)
}
downloadImages(imagesToDownloadWithId)
}
var downloadImages = function (imagesToDownload) {
var currentPost = 0
async.eachLimit(imagesToDownload, concurrent_downloads, function (imageToDownload, next) {
console.log('Downloading image ' + (++currentPost) + ' of ' + imagesToDownload.length + ' (' + imageToDownload.post_url + ') on page ' + imageToDownload.page)
downloadImage(imageToDownload, function (the_metadata) {
if (!the_metadata) {
console.log('Problem downloading ' + imageToDownload.post_url)
} else {
delete the_metadata.page
metadata.push(the_metadata)
}
next()
})
}, function (error) {
console.log('Done!')
metadata.sort(function (a,b) {
return a.id - b.id
})
if (check_for_deleted) {
checkForDeletedImages(true)
} else {
postDownloadTasks()
}
})
}
var checkForDeletedImages = function (didDownloadImages) {
console.log('Checking for deleted images!')
var deletedImages = []
async.eachLimit(metadata, concurrent_downloads, function (image, next) {
if (image.deleted) {
return next()
}
request.head({ url: image.image_url, headers: headers }, function (err, res, body) {
if (res && res.statusCode === 404) {
console.log('%s has been deleted!', image.post_url)
deletedImages.push(image)
var filename = path.resolve(folder_path, image.filename)
fs.unlink(filename, function (error) {
if (error) {
console.log('Error deleting %s!', filename)
}
next()
})
} else {
next()
}
})
}, function (error) {
deletedImages.forEach(function (image) {
metadata[metadata.indexOf(image)] = { id: image.id, deleted: true }
})
if (deletedImages.length || changedMetadata || didDownloadImages) {
postDownloadTasks()
}
})
}
var postDownloadTasks = function () {
fs.writeFile(path.resolve(folder_path, 'metadata.json'), JSON.stringify(metadata, null, 4), 'utf8', function (error) {
if (error) {
console.log('Error writing metadata!')
return console.log(error)
}
if (config.create_zip) {
console.log('Creating zip')
fs.unlink(config.create_zip, function (error) {
var archive = archiver('zip')
var output = fs.createWriteStream(config.create_zip)
output.on('close', function () {
console.log('Done creating zip, total size: %s', pretty(archive.pointer()))
doGitPush()
if (!git_push) {
runPostCommand()
}
})
archive.on('error', function (error) {
console.log('Error creating zip: ')
throw error
})
archive.pipe(output)
archive.bulk([
{ expand: true, cwd: folder_path, src: ['*.jpeg', 'metadata.json'] }
])
archive.finalize()
})
} else {
doGitPush()
if (!git_push) {
runPostCommand()
}
}
})
}
var downloadImage = function (the_metadata, callback) {
var filename = String('0000' + the_metadata.id).slice(-4) + '_' + the_metadata.unsplash_id + '.jpeg'
the_metadata.filename = filename
var file = fs.createWriteStream(path.resolve(folder_path, filename))
var deleted = false
file.on('finish', function () {
file.close(function () {
if (deleted) {
fs.unlink(path.resolve(folder_path, filename), function (error) {
callback()
})
} else {
callback(the_metadata)
}
})
})
request.get({ url: the_metadata.image_url, headers: headers })
.on('response', function (response) {
if (response.statusCode === 404) {
deleted = true
}
})
.pipe(file)
}
var doGitPush = function () {
if (git_push) {
console.log('Pushing to git!')
exec('cd ' + folder_path + ' && git add -A . && git commit -am \'Update images - ' + new Date().toLocaleDateString() + '\' && git push origin master', function (error, stdout, stderr) {
console.log(stdout)
runPostCommand()
})
}
}
var runPostCommand = function () {
if (config.post_command) {
console.log('Executing post_command')
exec(config.post_command, function (error, stdout, stderr) {
console.log(stdout)
})
}
}
fs.mkdir(folder_path, function (error) {
getPageCountAndDownload()
})