Skip to content

Instantly share code, notes, and snippets.

@1mrat
Last active July 25, 2018 13:59
Show Gist options
  • Save 1mrat/6b9d275111691038b7fe895e90993da0 to your computer and use it in GitHub Desktop.
Save 1mrat/6b9d275111691038b7fe895e90993da0 to your computer and use it in GitHub Desktop.
Javascript code to extract Readinglist data to a csv file.
// Prepare output
var csv="datetime,readtime";
// Get timestamps for all saved posts
var posts=document.querySelectorAll('time[datetime]');
// Add line with post data to csv
posts.forEach(function(i,j) {
// Remove comma from Locale format so its compitable with Google Sheets format
var postTime = new Date(i.attributes.datetime.value).toLocaleString().replace(',','');
// Get the readingTime through time parent node
var readingTime=i.parentNode.querySelector('.readingTime').title.replace(' min read','');
// Add line
csv += "\n"+postTime+","+readingTime;
});
// Function to download data to a file
// Source: https://stackoverflow.com/a/30832210
function download(data, filename, type) {
var file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
// Download the data.csv file to default location
download(csv,"data.csv","text/csv")
// need a function to pause processing to prevent hitting rate litmits or overloading Medium
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//counter for loading next batch of posts
var counter=0, batchSize=10;
// and the async function which will process each story & remove it
async function removePost() {
console.log('Count '+counter);
// Check if its time to load next few posts
if ( counter > batchSize ) {
//first scroll to the bottom so next batch can be loaded
window.scrollTo(0,document.body.scrollHeight);
//reset counter
counter=0;
}
// Only run if there are some posts still to remove
if ( document.querySelectorAll('button[data-action="remove-from-queue"]').length > 0 ) {
//log were removing next story
console.log('Removing next saved story...');
// Get one stories Remove button and click it
document.querySelector('button[data-action="remove-from-queue"]').click();
//increment counter
counter++;
// wait then call removePost again
sleep(2000).then(() => {
removePost()
});
} else {
console.log("No more stories left!");
}
}
// Start removing posts
removePost();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment