-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredditAPI.js
48 lines (43 loc) · 1.69 KB
/
redditAPI.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
import askhistorians from './data/topAllTimeListings/askhistorians.json';
import askscience from './data/topAllTimeListings/askscience.json';
import everythingscience from './data/topAllTimeListings/everythingscience.json';
import explainlikeimfive from './data/topAllTimeListings/explainlikeimfive.json';
import history from './data/topAllTimeListings/history.json';
import iwanttolearn from './data/topAllTimeListings/iwanttolearn.json';
import science from './data/topAllTimeListings/science.json';
import todayilearned from './data/topAllTimeListings/todayilearned.json';
const backupTopPostsJson = {
askhistorians,
askscience,
everythingscience,
explainlikeimfive,
history,
iwanttolearn,
science,
todayilearned,
};
export async function fetchSubTopPosts(sub, duration) {
try {
const url = `https://www.reddit.com/r/${sub}/top/.json?t=${duration}`;
const response = await fetch(url);
if (!response.ok) {
console.log(`Request to ${url} was not successful.`);
// Return static subreddit listings data (top posts of all time)
const data = backupTopPostsJson[sub.toLowerCase()];
return { data, successfulFetch: false };
}
const data = await response.json();
return { data, successfulFetch: true };
} catch(e) {
// If fetch throws an error, e.g. CORS policy block
// Try to return static subreddit listings data (top posts of all time)
const data = backupTopPostsJson[sub.toLowerCase()];
return { data, successfulFetch: false };
}
}
export async function fetchPostComments(commentsPath) {
const url = `https://www.reddit.com${commentsPath}.json`;
const response = await fetch(url);
const data = await response.json();
return data;
}