This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (91 loc) · 2.41 KB
/
index.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
const fetch = require('node-fetch');
const sortTypes = ['new', 'top'];
/**
*
* @param {String} sub The subreddit
* @param {String} [sort] How to sort, either top or new
* @example
* await api("meme");
* @Milo123459
*/
module.exports = async function (sub, sort) {
if (!sub) return Promise.reject('Error, you did not specify a subreddit!');
if (sort && !sortTypes.includes(sort))
return Promise.reject('Error, you did not specify a valid sort type!');
const response = await fetch(
`https://imageapi.fionn.live/reddit/${sub.toLowerCase()}${
sort ? `?sort=${sort}` : ''
}`.trim(),
{
headers: {
'user-agent': `ImageAPI.JS V${require('./package.json').version}`,
},
}
).then((res) => res.json());
if (response.error || response.err)
return Promise.reject(
Object.entries(response)
.map(
(value) =>
`${value[0]}: ${
value[1].map ? value[1].map((v) => `${v}`).join(', ') : value[1]
}`
)
.join(' ')
);
return response.img;
};
/**
*
* @param {String} sub The subreddit
* @param {String} [sort] How to sort, either top or new
* @example
* await api.advanced("meme");
* @Milo123459
*/
module.exports.advanced = async function (sub, sort) {
if (!sub) return Promise.reject('Error, you did not specify a subreddit!');
if (sort && !sortTypes.includes(sort)) return Promise.reject('Error, you did not specify a valid sort type!');
const response = await fetch(
`https://imageapi.fionn.live/reddit/${sub.toLowerCase()}${
sort ? `?sort=${sort}` : ''
}`.trim(),
{
headers: {
'user-agent': `ImageAPI.JS V${require('./package.json').version}`,
},
}
).then((res) => res.json());
if (response.error || response.err)
return Promise.reject(
Object.entries(response)
.map(
(value) =>
`${value[0]}: ${
value[1].map ? value[1].map((v) => `${v}`).join(', ') : value[1]
}`
)
.join(' ')
);
return {
img: response.img,
title: response.title,
upvotes: response.upvotes,
author: response.author,
upvoteRatio: response.upvoteRatio,
comments: response.comments,
downvotes: response.downvotes,
text: response.text,
post: response.post
};
};
/**
* Get stats via the /stats endpoint
*/
module.exports.stats = async () => {
return await fetch('https://imageapi.fionn.live/stats', {
headers: {
'user-agent': `ImageAPI.JS V${require('./package.json').version}`,
},
}).then((res) => res.json());
};