Last active
July 7, 2018 19:17
-
-
Save beaulac/40703bd014cedd4452f84f3d6e97b2e8 to your computer and use it in GitHub Desktop.
Turn Amazon reviews to shopify review CSV format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)}",[email protected],,"${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