Skip to content

Instantly share code, notes, and snippets.

@beaulac
Last active July 7, 2018 19:17
Show Gist options
  • Save beaulac/40703bd014cedd4452f84f3d6e97b2e8 to your computer and use it in GitHub Desktop.
Save beaulac/40703bd014cedd4452f84f3d6e97b2e8 to your computer and use it in GitHub Desktop.
Turn Amazon reviews to shopify review CSV format
(function () {
const headerRow = 'product_handle,state,rating,title,author,email,location,body,reply,created_at,replied_at';
const escapeQuotes = s => s.replace('"', '\\"');
const fillTemplate = ({ rating, title, author, body, created_at }) => `zenstone-professional-rapid-stone-warmer,published,${rating},"${escapeQuotes(
title)}","${escapeQuotes(author)}",amzcust@amz.com,,"${escapeQuotes(body)}",,"${escapeQuotes(created_at)}",`;
function parseReview(review) {
const ratingContainer = review.querySelector('[data-hook="review-star-rating"]');
const starClass = Array.from(ratingContainer.classList).find(cls => cls.match(/a-star-(\d)/));
const rating = starClass[starClass.length - 1];
const title = review.querySelector('[data-hook="review-title"]').text;
const author = review.querySelector('a[data-hook="review-author"]').text;
const created_at = review.querySelector('[data-hook="review-date"]').textContent.replace('on ', '');
const body = review.querySelector('[data-hook="review-body"]').textContent;
return fillTemplate({ rating, title, author, body, created_at });
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
const reviews = Array.from(document.querySelectorAll('[data-hook="review"]')).map(parseReview);
const output = `${headerRow}\n${reviews.join('\n')}`;
download('reviews.csv', output);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment