Skip to content

Instantly share code, notes, and snippets.

@timelf123
Created September 25, 2024 12:50
Show Gist options
  • Save timelf123/c2651fdb22edb99845668243c08f1239 to your computer and use it in GitHub Desktop.
Save timelf123/c2651fdb22edb99845668243c08f1239 to your computer and use it in GitHub Desktop.
summarize amazon review colors.js
// Global object to track color frequency across all pages
const allColorsFrequency = {};
// Function to extract color information from a review element
function extractColorFromReview(review) {
// Attempt to find the color link within the review (if available)
const colorElement = review.querySelector('a[data-hook="format-strip"]');
if (colorElement) {
// Extract the text (e.g., "Color: Multicam - Black")
const colorText = colorElement.textContent.trim().match(/Color:\s(.+)/);
if (colorText && colorText[1]) {
return colorText[1].trim(); // Return the color name
} else {
console.log('No recognizable color format found in review.');
}
} else {
console.log('No color element found in this review.');
}
return null; // No color found
}
// Function to process reviews on the current page
function processReviews() {
// Query all the reviews on the page
const reviews = document.querySelectorAll('div[data-hook="review"]');
if (reviews.length === 0) {
console.log("No reviews found on the page.");
return;
}
// Iterate over each review and extract color info
reviews.forEach((review, index) => {
const color = extractColorFromReview(review);
if (color) {
// Increment the frequency count for this color globally
if (allColorsFrequency[color]) {
allColorsFrequency[color]++;
} else {
allColorsFrequency[color] = 1;
}
} else {
console.log(`No color found in review #${index + 1}`);
}
});
}
// Function to summarize the color data after all pages are processed
function summarizeColors() {
console.log("Color Summary:", allColorsFrequency);
// Display the most popular color
if (Object.keys(allColorsFrequency).length > 0) {
const mostPopularColor = Object.keys(allColorsFrequency).reduce((a, b) => allColorsFrequency[a] > allColorsFrequency[b] ? a : b);
console.log("Most Popular Color:", mostPopularColor);
} else {
console.log("No colors were found in the reviews.");
}
}
// Function to click the "Next" button for pagination
function clickNextButton() {
const nextButton = document.querySelector('li.a-last a');
if (nextButton) {
nextButton.click();
return true;
}
return false;
}
// Main function to handle reviews on all pages
function handleAllReviews() {
// Process the reviews on the current page
processReviews();
// Check if there is a "Next" button for pagination
const hasNext = clickNextButton();
if (hasNext) {
// Wait for the new page to load before processing again
setTimeout(handleAllReviews, 3000); // Adjust the delay based on page load time
} else {
// If no more pages, summarize the color data
summarizeColors();
}
}
// Start the review handling process
handleAllReviews();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment