-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimgur.js
46 lines (39 loc) · 1.1 KB
/
imgur.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
const axios = require('axios')
const instance = axios.create({
baseURL: 'https://api.imgur.com',
timeout: 10000,
headers: {'Authorization': 'Client-ID 6b4ed3be862fc6f'},
validateStatus: (status) => {
return status >= 200 && status != 500; // default
},
});
const getGallery = ({q='', page=0}) => {
return instance.get(`/3/gallery/search/${page}/?q=${q}`).then(({data}) => {
return data.data
})
}
const getImage = ({id=''}) => {
return instance.get(`/3/image/${id}`).then(({status, data}) => {
if(status == 200) {
return data.data
}
})
}
const getAllImages = ({q=''}) => {
return getGallery({q}).then((r) => {
return axios.all(r.map((t) => {
if (typeof t.cover != 'undefined') {
return getImage({id:t.cover}).then((data) => {
if(data && data.link && typeof data.link != 'undefined' && data.type != 'image/gif') {
return data.link
}
})
}
})).then((r) => {
return r.filter((item) => typeof item != 'undefined')
}).catch((err) => {
console.log(err)
})
});
}
module.exports = { getAllImages }