Skip to content

Instantly share code, notes, and snippets.

@danuw
Created August 30, 2024 18:36
Show Gist options
  • Save danuw/28acf3de86276f5c452bcaadd01b257c to your computer and use it in GitHub Desktop.
Save danuw/28acf3de86276f5c452bcaadd01b257c to your computer and use it in GitHub Desktop.
Export prime video history to CSV (TSV actually) - mostly gpt generated
// Function to extract data from the list of nodes
function extractData() {
// Select all top-level <li> elements containing each date section
const dateNodes = document.querySelectorAll('li > div[data-automation-id^="wh-date-"]');
const data = [];
Array.from(dateNodes).forEach((dateNode) => {
// Extract the date from the 'data-automation-id' attribute and the inner text
const date = dateNode.getAttribute('data-automation-id').replace('wh-date-', '');
const dateText = dateNode.innerText.trim();
// Select all movie <li> elements under the current date section
const movieNodes = dateNode.nextElementSibling.querySelectorAll('li[data-automation-id^="wh-item-"]');
// Extract movie details from each movie node
Array.from(movieNodes).forEach((movieNode) => {
const titleElement = movieNode.querySelector('a._1NNx6V');
const title = titleElement ? titleElement.innerText : '';
const imageElement = movieNode.querySelector('img');
const imageUrl = imageElement ? imageElement.src : '';
data.push({
date: dateText,
title: title,
imageUrl: imageUrl,
});
});
});
return data;
}
// Function to convert data to CSV and download with tab separator
function downloadCSV(data) {
// Convert array of objects to CSV string with tab separators
const headers = ['Date', 'Title', 'Image URL'];
const csvRows = data.map(item => [item.date, item.title, item.imageUrl].join('\t'));
const csvString = [headers.join('\t'), ...csvRows].join('\n');
// Create a Blob from the CSV string and generate a download link
const blob = new Blob([csvString], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('hidden', '');
a.setAttribute('href', url);
a.setAttribute('download', 'movies.tsv'); // Using .tsv extension for tab-separated values
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
// Extract data and download as CSV
const extractedData = extractData();
downloadCSV(extractedData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment