use async for processMoreComments

This commit is contained in:
teddit 2021-09-09 19:19:52 +02:00
parent a6b9599463
commit 873e56a23b
2 changed files with 41 additions and 46 deletions

View File

@ -1,56 +1,51 @@
const config = require('../config'); const config = require('../config');
const { redisAsync } = require('./redis'); const { redisAsync } = require('./redis');
async function moreComments(fetch, redis, post_url, comment_ids, id) { async function processMoreComments(fetch, redis, post_url, comment_ids, id) {
if (post_url) { if (post_url) {
let key = `${post_url}:morechildren:comment_ids:${comment_ids}` let key = `${post_url}:morechildren:comment_ids:${comment_ids}`
redis.get(key, (error, json) => {
if (error) { try {
console.error(`Error getting the ${key} key from redis (moreComments()).`, error) const cached = await redisAsync.get(key);
return null;
if (cached !== null) {
return JSON.parse(cached);
} }
if (json) {
json = JSON.parse(json)
return json;
} else {
let url = `https://oauth.reddit.com/api/morechildren?api_type=json&children=${comment_ids}&limit_children=false&link_id=t3_${id}` let url = `https://oauth.reddit.com/api/morechildren?api_type=json&children=${comment_ids}&limit_children=false&link_id=t3_${id}`
fetch(encodeURI(url), redditApiGETHeaders()) const moreCommentsRequest = await fetch(url, redditApiGETHeaders());
.then(result => {
if (result.status === 200) { if (moreCommentsRequest.ok) {
result.json() let response = await moreCommentsRequest.json();
.then(json => {
if (json.json.data) { if (response.json.data) {
if (json.json.data.things) { if (response.json.data.things) {
let comments = json.json.data.things let comments = response.json.data.things
redis.setex(key, config.setexs.posts, JSON.stringify(comments), (error) => { await redisAsync.setex(
if (error) { key,
console.error(`Error setting the ${key} key to redis (moreComments()).`, error) config.setexs.posts,
return null; JSON.stringify(comments)
} else { );
console.log(`Fetched the JSON from Reddit (endpoint "morechildren") for URL: ${post_url}. (moreComments())`) console.log(`Fetched more comments.`);
return comments; return comments;
} }
})
} else {
return null;
} }
} else { } else {
console.error(
`Something went wrong while fetching data from Reddit:
${moreCommentsRequest.status} ${moreCommentsRequest.statusText}`
);
console.error(config.reddit_api_error_text);
return null; return null;
} }
}) } catch (error) {
} else { console.error('Error fetching more comments: ', error);
console.error(`Something went wrong while fetching data from Reddit. ${result.status} ${result.statusText} (moreComments())`)
return null; return null;
} }
}).catch(error => {
console.log(`Error fetching the JSON from Reddit (endpoint "morechildren") with url: ${url}. (moreComments())`, error)
return null;
})
}
})
} else { } else {
return null; return null;
} }
} }
module.exports = moreComments; module.exports = processMoreComments;

View File

@ -624,7 +624,7 @@ subredditRoutes.get(
let more_comments = null; let more_comments = null;
if (comment_ids) { if (comment_ids) {
let key = `${post_url}:morechildren:comment_ids:${comment_ids}`; let key = `${post_url}:morechildren:comment_ids:${comment_ids}`;
more_comments = await moreComments( more_comments = processMoreComments(
fetch, fetch,
redis, redis,
post_url, post_url,
@ -697,7 +697,7 @@ subredditRoutes.get(
let more_comments = null; let more_comments = null;
if (comment_ids) { if (comment_ids) {
let key = `${post_url}:morechildren:comment_ids:${comment_ids}`; let key = `${post_url}:morechildren:comment_ids:${comment_ids}`;
more_comments = await moreComments( more_comments = await processMoreComments(
fetch, fetch,
redis, redis,
post_url, post_url,