Skip to content

Instantly share code, notes, and snippets.

@beala
Last active April 27, 2020 20:08
Show Gist options
  • Save beala/5cfdba332784bf113a745b38e2e856e1 to your computer and use it in GitHub Desktop.
Save beala/5cfdba332784bf113a745b38e2e856e1 to your computer and use it in GitHub Desktop.
Worldometers Country Data Extractor

What is this

This bookmarklet extracts data from worlometers country pages into a downloadable csv file.

Installation

To install it:

  1. If your bookmarks bar is hidden, display it: Ctrl + Shift + b.
  2. Copy the following code into your clipboard. You'll need it in a sec.
javascript:(function(){function dump_data(){return{x:Highcharts.charts[0].series[0].data.map(d=>d.category),y:Highcharts.charts.map(c=>{return{name:c.renderTo.id,y:c.series[0].data.map(d=>d.y)}})}}
function data2csv(data){var csv='Date,'+data.y.map(y=>y.name).join(',')+'\n';var i;for(i=0;i<data.x.length;i++){csv+=data.x[i]+','+data.y.map(y=>y.y[i]).join(',')+'\n'}
return csv}
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)}
download('data.csv',data2csv(dump_data()))}())
  1. Right click the bookmarks bar and select "Add Page".
  2. Name it whatever you like.
  3. Replace the URL field with the code you copied in step (2).

The bookmarklet is installed.

How to use it

To use it:

  1. Go to a Countries page on Worldometers: US
  2. Click the bookmarklet.
  3. It should trigger a download for a CSV file that contains the extracted data.

The code

Here is the un-minified code if you want to see what's going on.

(function() {
    function dump_data() {
        return { 
            x: Highcharts.charts[0].series[0].data.map(d => d.category),
            y: Highcharts.charts.map(c => {return {name: c.renderTo.id, y: c.series[0].data.map(d=>d.y)}}) 
        };
    }
    function data2csv(data) {
        var csv = 'Date,' + data.y.map(y => y.name).join(',') + '\n';
        var i;
        for (i=0; i<data.x.length; i++) {
            csv += data.x[i] + ',' + data.y.map(y => y.y[i]).join(',') + '\n';
        }
        return csv;
    }
    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);
    }
    download('data.csv', data2csv(dump_data()))
}())

This probably only works for country pages, but should be easy to modify to extract data from any worldometers graph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment