-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.js
80 lines (69 loc) · 2.17 KB
/
client.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
var compose = require('request-compose')
var fs = require('fs')
var path = require('path')
var format = {
search: require('./format/search'),
wallpaper: require('./format/wallpaper'),
favorites: require('./format/favorites'),
url: require('./format/url'),
}
module.exports = {
search: ({
q, categories, purity, resolutions, atleast,
ratios, colors, sorting, topRange, order, page,
...options
}) => compose(
_ => compose.client(Object.assign({}, options, {
url: format.url.page('search'),
qs: {
q, categories, purity, resolutions, atleast,
ratios, colors, sorting, topRange, order, page
}
})),
({body}) => format.search(body)
)(),
wallpaper: ({id, ...options}) =>
compose(
_ => compose.client(Object.assign({}, options, {
url: format.url.page(`wallpaper/${id}`),
})),
({body}) => format.wallpaper(body)
)(),
image: ({id, size='thumb', ext='jpg', location=process.cwd(), ...options}) =>
compose.stream(Object.assign({}, options, {
url: format.url[size](id, ext)
}))
.then(({res}) => new Promise((resolve, reject) => {
var file = `wallhaven${size === 'thumb' ? '-th' : ''}-${id}.${ext}`
res.pipe(
fs.createWriteStream(path.join(location, file))
.on('close', resolve)
.on('error', reject))
}))
.catch((err) => ext === 'jpg' && err.message === '404 Not Found'
? module.exports.image(Object.assign({},
options, {id, size, ext: 'png', location}))
: Promise.reject(err)
),
login: ({user, pass, ...options}) =>
compose(
_ => compose.client(Object.assign({}, options, {
method: 'POST',
url: 'https://alpha.wallhaven.cc/auth/login',
form: {
username: user,
password: pass,
}
})),
({res}) => res.headers['set-cookie'].join('; ')
)(),
favorites: ({id='', page, cookie, ...options}) =>
compose(
_ => compose.client(Object.assign({}, options, {
url: `https://alpha.wallhaven.cc/favorites/${id}`,
qs: {page},
headers: {cookie},
})),
({body}) => format.favorites(body)
)(),
}