quetre/fetchers/getAnswers.js
zyachel 677b846eb2 fix: fix broken answers feed
this commit fixes the issue of answers not being shown as the underlying HTML changed which broke
old scraping logic

BREAKING CHANGE: older versions will not work with answer route

fix https://github.com/zyachel/quetre/issues/96
2023-03-05 00:34:53 +05:30

75 lines
2.6 KiB
JavaScript

////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////
import { quetrefy } from '../utils/urlModifiers.js';
import answersFetcher from './answersFetcher.js';
////////////////////////////////////////////////////////
// FUNCTION
////////////////////////////////////////////////////////
const getAnswers = async (slug, lang) => {
// getting data and destructuring it in case it exists
const rawData = await answersFetcher(slug, lang);
// array containing all the answers with metadata
const ansArr = rawData.answers.map(answer => ({
text: JSON.parse(answer.content).sections,
isViewable: !!answer.viewerHasAccess,
creationTime: answer.creationTime,
updatedTime: answer.updatedTime,
numComments: answer.numDisplayComments,
numUpvotes: answer.numUpvotes,
numViews: answer.numViews,
numShares: answer.numShares,
numAnswerRequests: answer.numRequesters,
aid: answer.aid,
isBusinessAnswer: answer.businessAnswer,
author: {
uid: answer.author.uid,
isAnon: answer.author.isAnon,
image: answer.author.profileImageUrl,
isVerified: answer.author.isVerified,
url: quetrefy(answer.author.profileUrl),
name: `${answer.author.names[0].givenName} ${answer.author.names[0].familyName}`,
credential: answer.authorCredential?.translatedString,
// additionalCredentials: answer.node.answer?.credibilityFacts.map(),
},
originalQuestion: {
text: JSON.parse(answer.question.title).sections,
url: quetrefy(answer.question.url),
qid: answer.question.qid,
isDeleted: answer.question.isDeleted,
},
}));
// main data object to be returned
const data = {
question: {
text: JSON.parse(rawData.question.title).sections,
url: quetrefy(rawData.question.url),
qid: rawData.question.qid,
idDeleted: rawData.question.isDeleted,
isViewable: rawData.question.isVisibleToViewer,
askerUid: rawData.question.asker.uid,
},
numAnswers: rawData.answerCount,
answers: ansArr,
topics: rawData.question.topics.map(topicObj => ({
tid: topicObj.tid,
name: topicObj.name,
url: quetrefy(topicObj.url),
})),
relatedQuestions: rawData.related.map(questionObj => ({
qid: questionObj.qid,
url: quetrefy(questionObj.url),
text: JSON.parse(questionObj.title).sections,
})),
};
return data;
};
////////////////////////////////////////////////////////
// EXPORTS
////////////////////////////////////////////////////////
export default getAnswers;