teddit/inc/processMoreComments.js
2021-09-09 19:19:52 +02:00

52 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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