This repository was archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreddit-api.js
More file actions
58 lines (50 loc) · 1.5 KB
/
reddit-api.js
File metadata and controls
58 lines (50 loc) · 1.5 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
const fetch = require('node-fetch');
const Qs = require('qs');
const load = (url, query) => {
const queryString = query ? `?${Qs.stringify(query)}` : '';
return fetch(`${url}${queryString}`).then(response => response.json());
};
const loadSubreddit = name => load(`https://reddit.com/r/${name}/about.json`);
const loadSubredditListings = (
name,
listingsType = 'hot',
{ limit, after, before, timeInterval }
) =>
load(`https://reddit.com/r/${name}/${listingsType}.json`, {
limit,
after,
before,
t: timeInterval
});
const loadUser = name => load(`https://reddit.com/user/${name}.json`);
const loadUserPosts = (name, { limit, after }) =>
load(`https://reddit.com/user/${name}/submitted.json`, { limit, after });
const loadUserComments = (name, { limit, after }) =>
load(`https://reddit.com/user/${name}/comments.json`, { limit, after });
const loadComments = (
subredditName,
linkId,
{ depth, limit, sort, comment, threaded }
) =>
load(`https://reddit.com/r/${subredditName}/comments/${linkId}.json`, {
depth,
limit,
sort,
comment,
threaded
});
const loadRandomSubreddit = async (isNsfw = false) => {
const url = `https://reddit.com/r/${isNsfw ? 'randnsfw' : 'random'}.json`;
const redditResponse = await fetch(url);
const json = await redditResponse.json();
return json.data.children[0].data.subreddit;
};
module.exports = {
loadSubreddit,
loadSubredditListings,
loadUser,
loadUserPosts,
loadUserComments,
loadRandomSubreddit,
loadComments
};