-
-
Save Jonasdero/af51ff3e9fa929e2aee8888b08530109 to your computer and use it in GitHub Desktop.
Export Medium Stats Stores to CSV
This file contains 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 Sleep(milliseconds) { | |
return new Promise(resolve => setTimeout(resolve, milliseconds)); | |
} | |
function download(filename, text) { | |
var pom = document.createElement('a'); | |
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); | |
pom.setAttribute('download', filename); | |
if (document.createEvent) { | |
var event = document.createEvent('MouseEvents'); | |
event.initEvent('click', true, true); | |
pom.dispatchEvent(event); | |
} | |
else { | |
pom.click(); | |
} | |
} | |
async function getStats() { | |
for (let i = 0; i < 10; i++) { | |
window.scrollTo(0, document.body.scrollHeight); | |
await Sleep(1000); | |
} | |
content = "date;link;id;title;view;reads;readRatio;fans\n"; | |
rows = document.querySelectorAll(".sortableTable-row.js-statsTableRow") | |
rows.forEach(function (row) { | |
cells = row.getElementsByTagName('td'); | |
date = new Date().toISOString().slice(0, 10); | |
title = cells[0].children[cells[0].children.length - 2].innerText; | |
link = cells[0].children[cells[0].children.length - 2].children[0].href.split('?')[0]; | |
id = cells[0].children[cells[0].children.length - 2].children[0].href.split('?')[0].split('/')[cells[0].children[cells[0].children.length - 2].children[0].href.split('?')[0].split('/').length - 1]; | |
views = cells[1].children[0].innerText; | |
reads = cells[2].children[0].innerText; | |
readRatio = (+cells[3].children[0].innerText).toFixed(5).toString().replace('.', ','); | |
recommends = cells[4].children[0].innerText; | |
content += date + ";" + link + ";" + id + ";" + title + ";" + views + ";" + reads + ";" + readRatio + ";" + recommends + "\n"; | |
}); | |
download("medium-metrics-" + new Date().toISOString().slice(0, 10) + ".csv", content) | |
} | |
getStats() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment