-
Notifications
You must be signed in to change notification settings - Fork 1
/
wallpaper.js
87 lines (73 loc) · 2.35 KB
/
wallpaper.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
const fs = require('fs')
const axios = require('axios')
const buildURL = require('axios/lib/helpers/buildURL')
const apiUrl = 'https://api.500px.com/v1/photos'
class Wallpaper {
constructor(options = {}) {
this.outFileName = options.outFileName
this.feature = options.feature || ''
this.category = options.category || ''
this.minWidth = options.minWidth || 0
this.minHeight = options.minHeight || 0
this.mustBeLandscape = options.mustBeLandscape || false
const maxEdgeLength = Math.max(this.minWidth, this.minHeight)
this.imageSize = maxEdgeLength <= 2048 ? 2048 : 4096
}
async findAndDownload() {
try {
const photos = await this.retrievePhotos()
const photo = this.findWallpaper(photos)
await this.download(photo)
} catch (error) {
console.error(error)
process.exitCode = 1
}
}
async retrievePhotos() {
const interceptor = axios.interceptors.request.use(config => {
console.log('Retrieving photos:', this.getUrl(config))
return config
})
const response = await axios.get(apiUrl, {
params: {
feature: this.feature,
only: this.category,
image_size: [this.imageSize],
},
})
axios.interceptors.request.eject(interceptor)
return response.data.photos
}
findWallpaper(photos) {
return photos.find(photo => this.isWallpaper(photo))
}
async download(photo) {
const displayName = this.getPhotoDisplayName(photo)
const image = photo.images.find(image => image.size === this.imageSize)
const fileNameWithExt = `${this.outFileName || displayName}.${image.format}`
console.log('Downloading:', displayName)
const response = await axios.get(image.url, { responseType: 'stream' })
const file = fs.createWriteStream(fileNameWithExt)
response.data.pipe(file)
console.log('Image saved:', fileNameWithExt)
}
isWallpaper(photo) {
if (
(!this.mustBeLandscape || photo.width > photo.height) &&
photo.width >= this.minWidth &&
photo.height >= this.minHeight
) {
return true
} else {
console.log('Skipping:', this.getPhotoDisplayName(photo))
return false
}
}
getPhotoDisplayName(photo) {
return photo.url.split('/').pop()
}
getUrl(config) {
return buildURL(config.url, config.params, config.paramsSerializer)
}
}
module.exports = Wallpaper