-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwallpaper.js
223 lines (208 loc) · 6.27 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
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
#!/usr/bin/env node
const commander = require('commander-plus')
const wallpaper = require('wallpaper')
const chalk = require('chalk')
const request = require('request')
const path = require('path')
const fs = require('fs')
const http = require('http')
const url = require('url')
const home = require('os').homedir()
const mkpath = require('mkpath')
const del = require('del')
const figlet = require('figlet')
const clear = require('clear')
let HOST = 'https://api.unsplash.com/'
let lastarg = '2 '
let clientkey = ''
let automatic = false
let change = false
let imageInfo = {
user: '',
url: ''
}
const errorHandle = /errors\"\:\["(.*)"]}/g
console.log(
chalk.blue(
figlet.textSync('JackPaper', {
horizontalLayout: 'full',
font: '3D-ASCII'
}))
)
commander
.version('1.2.3')
.command('change')
.description('Change Background')
.action((q) => {
if (typeof(q) !== 'object') {
applyImage(q)
} else {
applyImage()
}
})
commander
.command('auto')
.option('-t, --time <time>', 'adjust the time')
.description('Change Background per adjusted time')
.action((auto, time) => {
automatic = true
if (typeof(auto) !== 'object' && time.time) {
setInterval(() => {
applyImage(auto)
}, (time.time * 1000))
} else if (auto.time){
setInterval(() => {
applyImage()
}, (auto.time * 1000))
} else {
console.log('--time required. Example: ', chalk.bold.green('jackpaper auto --time 10'))
process.exit()
}
})
commander
.command('key')
.description('Set your UnSplash Developer ID')
.action((key) => {
setKey()
})
commander.on('--help', () => {
clear()
console.log('Usage: jackpaper <command> [options]')
console.log('')
console.log('Commands:')
console.log('')
console.log('change Change Background')
console.log('auto <options> Change Background per adjusted time')
console.log('key Set your UnSplash Developer ID')
console.log('')
console.log('Options:')
console.log('-t, --time set time in seconds for auto command')
console.log('-h, --help output usage information')
console.log('-V, --version output the version number')
console.log(' ')
console.log(' Examples:')
console.log(' ')
console.log(' $ jackpaper --version')
console.log(' $ jackpaper --help')
console.log(' $ jackpaper key')
console.log(' $ jackpaper change')
console.log(' $ jackpaper change galaxy')
console.log(' $ jackpaper auto --time 10')
console.log(' $ jackpaper auto galaxy --time 10')
console.log('')
});
commander.parse(process.argv)
function setKey() {
commander.prompt('Unsplash Application ID: ', (key) => {
clientkey = key
mkpath.sync(path.join(home, '/.jackpaper'))
const keypath = path.join(home, '/.jackpaper/key.txt')
fs.writeFile(keypath, key, (err) => {
if (err) throw err
clear()
console.log(chalk.bold.blue('\n Key Saved!', keypath))
process.exit()
})
})
}
function applyImage (query) {
fs.readFile(path.join(home, '/.jackpaper/key.txt'), "utf8", function read(err, data) {
if (err) {
console.log(chalk.bold.red('You need to set your key first!'))
setKey()
} else {
clientkey = data
getPhotos('random', query, (error, photos, link) => {
// if (error) console.log(error)
if (!error) {
download(photos.urls.full, './assets/image.jpg').then(() => {
if (!automatic) {
process.exit()
}
})
} else {
// console.log(error)
let err = error.toString()
let m = errorHandle.exec(err)
// console.log('m[1] = ', m[1])
if (m[1] == "OAuth error: The access token is invalid") {
console.log(chalk.bold.red('Your Key is Not Valid! Please Run this Command: ', chalk.bold.green('$ jackpaper key')))
} else {
console.log(m[1])
process.exit()
}
process.exit()
clear()
}
})
}
})
}
function download (_href, _filepath) {
return new Promise((resolve, reject) => {
del.sync([path.join(home,'/.jackpaper/*.jpg')], {force: true})
const hrefStartsWithHttp = _href.indexOf('http') !== 0
const href = hrefStartsWithHttp ? ('http://' + _href) : _href
const parsedURL = url.parse(href)
const filepath = path.join(home,'/.jackpaper/' + Date.now() + '.jpg') || parsedURL.pathname.split('/').join('_')
mkpath.sync(path.join(home, '/.jackpaper'))
console.log('downloading', href, '...')
http.get({
host: parsedURL.host,
path: parsedURL.pathname
}, (res) => {
let chunks = []
res.on('data', (chunk) => {
chunks.push(chunk)
})
res.on('end', () => {
const buffer = Buffer.concat(chunks)
fs.writeFile(filepath, buffer, (err) => {
if (err) throw err
console.log('saved', filepath)
wallpaper.set(filepath).then(() => {
clear()
console.log(
chalk.yellow(
figlet.textSync('Unsplash', { horizontalLayout: 'full' })
)
)
console.log('\nPhoto By: ', chalk.bold.red(imageInfo.user), '\nUrl: ', chalk.bold.red(imageInfo.url))
resolve()
})
})
})
})
})
}
function getPhotos(page, query, callback) {
let params = {}
if (page != null)
params.page = page
if (query != null)
params.query = query
request({
url: (HOST + path.join('photos/random')),
method: 'GET',
qs: params,
headers: {
'Content-type': 'application/json',
'Authorization': 'Client-ID ' + clientkey
}
},
(err, res, body) => {
if (body == 'Rate Limit Exceeded') {
console.log(chalk.bold.red('Your rate limit has reached the max, allow up to an hour for it to refresh. Or put a new key in.'))
process.exit()
} else {
let json = JSON.parse(body)
if (json.user) {
imageInfo.user = json.user.name
imageInfo.url = json.links.html
}
if (err) return callback(err)
if (res.statusCode !== 200) return callback(new Error(body), null)
return callback(null, JSON.parse(body), res.headers.link)
}
})
}