Move API URL generation into a common function
This commit is contained in:
parent
4536a7f460
commit
61e173e0fe
|
@ -86,5 +86,12 @@ module.exports = function(fetch) {
|
|||
method: 'GET'
|
||||
}
|
||||
}
|
||||
this.redditApiRequest = function(url, query) {
|
||||
query = query ? ('?' + query) : '';
|
||||
if (config.use_reddit_oauth)
|
||||
return fetch(encodeURI(`https://oauth.reddit.com/{url}{query}`), redditApiGETHeaders());
|
||||
else
|
||||
return fetch(encodeURI(`https://reddit.com/{url}.json{query}`), redditApiGETHeaders());
|
||||
}
|
||||
initRedditApi()
|
||||
}
|
||||
|
|
|
@ -25,13 +25,7 @@ async function processSubredditAbout(subreddit, redis, fetch, RedditAPI) {
|
|||
return returnRelevantKeys(JSON.parse(cached));
|
||||
}
|
||||
|
||||
let url = `https://reddit.com/r/${subreddit}/about.json`;
|
||||
|
||||
if (config.use_reddit_oauth) {
|
||||
url = `https://oauth.reddit.com/r/${subreddit}/about`;
|
||||
}
|
||||
|
||||
const subredditAboutRequest = await fetch(url, redditApiGETHeaders());
|
||||
const subredditAboutRequest = await redditApiRequest(`r/${subreddit}/about`);
|
||||
|
||||
if (subredditAboutRequest.ok) {
|
||||
let response = await subredditAboutRequest.json();
|
||||
|
|
|
@ -179,19 +179,13 @@ homeRoute.get([`/:sort?`, '/frontpage'], async (req, res, next) => {
|
|||
}
|
||||
})();
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth) {
|
||||
if (get_subbed_subreddits)
|
||||
url = `https://oauth.reddit.com/r/${subbed_subreddits}/${sortby}?api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`;
|
||||
else
|
||||
url = `https://oauth.reddit.com/${sortby}?api_type=json&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`;
|
||||
} else {
|
||||
if (get_subbed_subreddits)
|
||||
url = `https://reddit.com/r/${subbed_subreddits}/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`;
|
||||
else
|
||||
url = `https://reddit.com/${sortby}.json?g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`;
|
||||
}
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
let req;
|
||||
if (get_subbed_subreddits)
|
||||
req = redditApiRequest(`r/${subbed_subreddits}/${sortby}`, `api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`);
|
||||
else
|
||||
req = redditApiRequest(sortby, `api_type=json&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`);
|
||||
|
||||
req
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
|
|
@ -235,19 +235,13 @@ saveRoutes.get(
|
|||
}
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth) {
|
||||
if (post_url)
|
||||
url = `https://oauth.reddit.com/comments/${post_id}?api_type=json`;
|
||||
else
|
||||
url = `https://oauth.reddit.com/comments/${post_id}/comment/${comment_id}?api_type=json`;
|
||||
} else {
|
||||
if (post_url)
|
||||
url = `https://reddit.com/comments/${post_id}.json?api_type=json`;
|
||||
else
|
||||
url = `https://reddit.com/comments/${post_id}/comment/${comment_id}.json?api_type=json`;
|
||||
}
|
||||
let req;
|
||||
if (post_url)
|
||||
req = redditApiRequest(`comments/${post_id}`, `api_type=json`);
|
||||
else
|
||||
req = redditApiRequest(`comments/${post_id}/comment/${comment_id}`, `api_type=json`);
|
||||
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
req
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
|
|
@ -122,12 +122,7 @@ subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
|
|||
}
|
||||
})();
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth)
|
||||
url = `https://oauth.reddit.com/r/${subreddit}/search?api_type=json&q=${q}&restrict_sr=${restrict_sr}&include_over_18=${nsfw}&sort=${sortby}&t=${past}${count}${d}&raw_json=${raw_json}`;
|
||||
else
|
||||
url = `https://reddit.com/r/${subreddit}/search.json?api_type=json&q=${q}&restrict_sr=${restrict_sr}&include_over_18=${nsfw}&sort=${sortby}&t=${past}${count}${d}&raw_json=${raw_json}`;
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
redditApiRequest(`r/${subreddit}/search`, `api_type=json&q=${q}&restrict_sr=${restrict_sr}&include_over_18=${nsfw}&sort=${sortby}&t=${past}${count}${d}&raw_json=${raw_json}`)
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
@ -139,8 +134,7 @@ subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
|
|||
*/
|
||||
json.suggested_subreddits = {};
|
||||
if (restrict_sr === 'off' && before == '' && after == '') {
|
||||
let url = `https://reddit.com/subreddits/search.json?q=${q}&include_over_18=${nsfw}&limit=3`;
|
||||
const response = await fetch(encodeURI(url));
|
||||
const response = await redditApiRequest('subreddits/search', `q=${q}&include_over_18=${nsfw}&limit=3`);
|
||||
const data = await response.json();
|
||||
json.suggested_subreddits = data;
|
||||
}
|
||||
|
@ -256,12 +250,7 @@ subredditRoutes.get('/r/:subreddit/about', (req, res, next) => {
|
|||
);
|
||||
})();
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth)
|
||||
url = `https://oauth.reddit.com/r/${subreddit}/about.json?api_type=json&raw_json=${raw_json}`;
|
||||
else
|
||||
url = `https://reddit.com/r/${subreddit}/about.json?api_type=json&raw_json=${raw_json}`;
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
redditApiRequest(`r/${subreddit}/about`, `api_type=json&raw_json=${raw_json}`)
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
@ -372,12 +361,7 @@ subredditRoutes.get(
|
|||
instance_config: config,
|
||||
});
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth)
|
||||
url = `https://oauth.reddit.com/r/${subreddit}/wiki/${page}${sub_page}?api_type=json`;
|
||||
else
|
||||
url = `https://reddit.com/r/${subreddit}/wiki/${page}${sub_page}.json?api_type=json`;
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
redditApiRequest(`r/${subreddit}/wiki/${page}${sub_page}`, 'api_type=json')
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
@ -455,12 +439,7 @@ subredditRoutes.get('/r/:subreddit/w/:page?/:sub_page?', (req, res, next) => {
|
|||
});
|
||||
|
||||
subredditRoutes.get('/r/random', (req, res, next) => {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth)
|
||||
url = `https://oauth.reddit.com/r/random?api_type=json&count=25&g=GLOBAL`;
|
||||
else url = `https://reddit.com/r/random.json?api_type=json&count=25&g=GLOBAL`;
|
||||
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
redditApiRequest('r/random', 'api_type=json&count=25&g=GLOBAL')
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
@ -635,12 +614,7 @@ subredditRoutes.get('/r/:subreddit/:sort?', (req, res, next) => {
|
|||
}
|
||||
})();
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth)
|
||||
url = `https://oauth.reddit.com/r/${subreddit}/${sortby}?api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`;
|
||||
else
|
||||
url = `https://reddit.com/r/${subreddit}/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`;
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
redditApiRequest(`${subreddit}/${sortby}`, `api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`)
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
@ -860,13 +834,7 @@ subredditRoutes.get(
|
|||
}
|
||||
})();
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth)
|
||||
url = `https://oauth.reddit.com${comments_url}?api_type=json&sort=${sortby}&context=${context}&raw_json=${raw_json}`;
|
||||
else
|
||||
url = `https://reddit.com${comments_url}.json?api_type=json&sort=${sortby}&context=${context}&raw_json=${raw_json}`;
|
||||
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
redditApiRequest(comments_url.substring(1), `api_type=json&sort=${sortby}&context=${context}&raw_json=${raw_json}`)
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
@ -1103,20 +1071,13 @@ subredditRoutes.get('/subreddits/:sort?', (req, res, next) => {
|
|||
}
|
||||
})();
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth) {
|
||||
if (!searching)
|
||||
url = `https://oauth.reddit.com/subreddits/${sortby}?api_type=json&count=25&g=GLOBAL&t=${d}&raw_json=${raw_json}`;
|
||||
else
|
||||
url = `https://oauth.reddit.com/subreddits/search?api_type=json&q=${q}&include_over_18=${nsfw}${d}&raw_json=${raw_json}`;
|
||||
} else {
|
||||
if (!searching)
|
||||
url = `https://reddit.com/subreddits/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${d}&raw_json=${raw_json}`;
|
||||
else
|
||||
url = `https://reddit.com/subreddits/search.json?api_type=json&q=${q}&include_over_18=${nsfw}${d}&raw_json=${raw_json}`;
|
||||
}
|
||||
let req;
|
||||
if (!searching)
|
||||
req = redditApiRequest(`subreddits/${sortby}`, `api_type=json&count=25&g=GLOBAL&t=${d}&raw_json=${raw_json}`);
|
||||
else
|
||||
req = redditApiRequest('subreddits/search', `api_type=json&q=${q}&include_over_18=${nsfw}${d}&raw_json=${raw_json}`);
|
||||
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
req
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
|
|
@ -132,25 +132,12 @@ userRoutes.get('/u/:user/:kind?', (req, res, next) => {
|
|||
}
|
||||
})();
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth)
|
||||
url = `https://oauth.reddit.com/user/${user}/about?raw_json=${raw_json}`;
|
||||
else
|
||||
url = `https://reddit.com/user/${user}/about.json?raw_json=${raw_json}`;
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
redditApiRequest(`user/${user}/about`, `raw_json=${raw_json}`)
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
user_data.about = json;
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth) {
|
||||
let endpoint = '/overview';
|
||||
if (post_type !== '') endpoint = post_type;
|
||||
url = `https://oauth.reddit.com/user/${user}${post_type}?limit=26${d}&sort=${sortby}&t=${past}&raw_json=${raw_json}`;
|
||||
} else {
|
||||
url = `https://reddit.com/user/${user}${post_type}.json?limit=26${d}&sort=${sortby}&t=${past}&raw_json=${raw_json}`;
|
||||
}
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
redditApiRequest(`user/${user}${post_type}`, `limit=26${d}&sort=${sortby}&t=${past}&raw_json=${raw_json}`)
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
@ -381,12 +368,7 @@ userRoutes.get('/u/:user/m/:custom_feed/:sort?', (req, res, next) => {
|
|||
}
|
||||
})();
|
||||
} else {
|
||||
let url = '';
|
||||
if (config.use_reddit_oauth)
|
||||
url = `https://oauth.reddit.com/${subreddit}/${sortby}?api_type=json&count=25&g=GLOBAL&t=${past}${d}`;
|
||||
else
|
||||
url = `https://reddit.com/${subreddit}/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${past}${d}`;
|
||||
fetch(encodeURI(url), redditApiGETHeaders())
|
||||
redditApiRequest(`${subreddit}/${sortby}`, `api_type=json&count=25&g=GLOBAL&t=${past}${d}`)
|
||||
.then((result) => {
|
||||
if (result.status === 200) {
|
||||
result.json().then((json) => {
|
||||
|
|
Loading…
Reference in New Issue