-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
36 lines (28 loc) · 916 Bytes
/
app.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
//http://stackoverflow.com/questions/10639914/is-there-a-way-to-get-bings-photo-of-the-day
var fs = require('fs')
var path = require('path')
var request = require('request')
function fetch(){
var uri = 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'
request(uri, function(error, response, body){
try {
var data = JSON.parse(body)
var images = data.images
var pictures = path.join(__dirname, '../../Pictures/bing-wallpapers/')
images.forEach(function(image){
var ext = path.extname(image.url).split('&')[0]
var img = path.join(pictures, image.fullstartdate + ext)
if (!fs.existsSync(img)) {
request(`https://www.bing.com${image.url}`).pipe(
fs.createWriteStream(img)
)
} else {
console.log('already exists')
}
})
} catch (e) {
console.error(e)
}
})
}
fetch()