Scrapper

 avatar
unknown
javascript
5 months ago
2.2 kB
4
Indexable
const jsonData = {};

// Function to format the data
function formatData(data) {
  const formattedData = [];

  // Initialize a counter for question numbers
  let questionNumber = 1;

  // Loop through each question
  data.data.questionsResponses.forEach((question) => {
    if (question.typeTitle !== "Multiple Choice Question") {
      let imageURL = "";
      let content = question.content;

      // Check if the content contains an image URL
      const imgRegex = /<img[^>]*src="([^"]+)"[^>]*>/g;
      const match = imgRegex.exec(content);
      if (match) {
        imageURL = match[1];
        content = content.replace(imgRegex, "");
      }

      // Include solution video only if it is on youtube
      let solutionVideo = "";
      const videoUrl = question.solutions[0].videoSolution.url;
      if (videoUrl.includes('youtube.com') || videoUrl.includes('youtu.be')) {
        solutionVideo = videoUrl;
      }

      // Find the correct answer
      const correctOption = question.options.find((option) => option.isCorrect);
      if (correctOption) {
        const correctAnswer = correctOption.text;

        // Create the formatted question
        const formattedQuestion = {
          'questionNumber': String(questionNumber),
          'type': String(question.typeTitle === "Single Choice Question" ? "Single" : question.typeTitle),
          'content': String(content),
          'imageURL': String(imageURL),
          'correctAnswer': String(correctAnswer),
          'solutionVideo': String(solutionVideo),
          'options': question.options.map((option, index) => {
            const optionIndex = String.fromCharCode(65 + index); // Convert index to ABCD
            return `<p>${optionIndex}. ${option.text.replace(/<p>/g, '').replace(/<\/p>/g, '')}</p>`;
          }),
        };

        // Add the formatted question to the array
        formattedData.push(formattedQuestion);

        // Increment the question number
        questionNumber++;
      }
    }
  });

  return formattedData;
}

// Call the function to format the data
const formattedData = formatData(jsonData);
console.log(JSON.stringify(formattedData, null, 2));
Editor is loading...
Leave a Comment