-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetRandomWallPaper.js
More file actions
68 lines (68 loc) · 2.41 KB
/
setRandomWallPaper.js
File metadata and controls
68 lines (68 loc) · 2.41 KB
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
import request from 'request'
import fs from 'fs'
import setWallpaper from './setWallpaper'
import { localDataLocation } from './localDataLocation'
const listLocation = 'https://raw.githubusercontent.com/limhenry/earthview/master/earthview.json'
const dataloc = localDataLocation()
export default (cb) => {
request({
url: listLocation,
json: true
},
(error, response, body) => {
if (!error && response.statusCode === 200) {
// List loaded
const list = body
// Choose random image (+data)
const listObj = list[Math.round(Math.random() * list.length)]
const url = listObj.image
const downloadloc = `${dataloc}/${url.split('/')[url.split('/').length - 1]}`
// const downloadloc = `./images/bg.jpg`
fs.createWriteStream(downloadloc)
// console.log('wallpaper download')
request(url, { encoding: 'binary' }, function (error, response, body) {
if (!error) {
fs.writeFile(downloadloc, body, 'binary', async function (err) {
if (!err) {
// console.log('download complete')
// Image downloaded
// Set as wallpaper
setWallpaper(downloadloc, (err) => {
if (!err) {
// console.log('wallpaper set')
// Remove file
fs.unlink(downloadloc, (err) => {
if (err) {
console.error(err)
return
}
// console.log('wallpaper file removed')
// file removed, wallpaper set successfully!
// remove old data
try {
fs.unlinkSync(`${dataloc}/imageData.json`)
} catch (err) {
}
// file removed
// console.log('old meta removed')
// save data about wallpaper
fs.writeFile(`${dataloc}/imageData.json`, JSON.stringify(listObj), function (err) {
if (err) {
console.log(err)
}
// FILE SAVED!
// console.log('new meta saved')
if (cb) {
cb() // Callback
}
})
})
}
})
}
})
}
})
}
})
}