Skip to content

Instantly share code, notes, and snippets.

@megawertz
Created May 15, 2013 14:09
Show Gist options
  • Save megawertz/5584250 to your computer and use it in GitHub Desktop.
Save megawertz/5584250 to your computer and use it in GitHub Desktop.
Node script that retrieves Libsyn podcast download stats and displays them on Panic's Status Board app and as HTML. Displays LES and total downloads. Based on Marco Arment's PHP script at https://gist.github.com/marcoarment/5393014
// Node script to retrieve Libsyn stats and display on Panic's Status Board app & as HTML
// This is rough but functional
//
// Thanks to Marco Arment for doing the hard work
// Based on https://gist.github.com/marcoarment/5393014
var sys = require('sys');
var fs = require('fs');
var exec = require('child_process').exec;
var authChild, statChild;
var numberOfShowsToDisplay = 6;
var authCurlCmd = 'curl -s -L -c ./libsyn-stats-cookie --data-urlencode "[email protected]" --data-urlencode "password=123456789" https://three.libsyn.com/auth/login';
// Check the source of the main dashboard page to get the show id
var getStatsCurlCmd = 'curl -L -b ./libsyn-stats-cookie "https://three.libsyn.com//lite/statistics/ajax-table-data/show_id/12345/type/three-month/target/show/id/12345"';
var statusBoardOutputFile = "/Path/To/Your/Dropbox/Public/podcaststats.json";
var htmlOutputFile = "/Path/To/Your/Dropbox/Public/podcaststats.html";
authChild = exec(authCurlCmd, function(error, stdout, stderr) {
// If we auth OK, then get the data
if(error === null)
{
statChild = exec(getStatsCurlCmd, function(error, stdout, stderr) {
// If we get the data parse it
if(error === null)
{
var rawStatsObj = JSON.parse(stdout);
var rawShowDataArray = rawStatsObj.data;
var graphdatapointsLES = new Array(); // Non-duplicated downloads
var graphdatapointsTOTAL = new Array(); // All downloads
for(var i = 0 ; i < rawShowDataArray.length && i < numberOfShowsToDisplay ; i++) {
var tmp = {};
tmp.title = rawShowDataArray[i]["item_title"];
tmp.value = rawShowDataArray[i]["all_time_les"];
graphdatapointsLES.push(tmp);
var tmp2 = {}; // could clone, lazy
tmp2.title = rawShowDataArray[i]["item_title"];
tmp2.value = rawShowDataArray[i]["all_time_total"];
graphdatapointsTOTAL.push(tmp2);
}
var d = new Date();
var formattedDate = (d.getMonth()+1) + "/" + d.getDate() + " - " + ("0" + d.getHours()).slice(-2) + ":" + ("0" + d.getMinutes()).slice(-2) + ":" + ("0" + d.getSeconds()).slice(-2);
// Output for status board
var outputObj = {
graph: {
title: formattedDate,
datasequences:
[
{
title: "LES",
datapoints: graphdatapointsLES
},
{
title: "Total",
datapoints: graphdatapointsTOTAL
}
]
}
};
fs.writeFile(statusBoardOutputFile, JSON.stringify(outputObj), function(err) {
if(err) {
console.log(err);
}
});
// Also output html (bad html)
// Doing this manually to avoid module dependencies
var htmlOutput = "<html><body><h2>Stats - " + formattedDate + "</h2>";
htmlOutput += "<table border=2 cellpadding=2><tr><th>Show</th><th>LES</th><th>Total</th></tr>";
for(var i = 0 ; i < graphdatapointsLES.length ; i++) {
htmlOutput += "<tr><td>" + graphdatapointsLES[i]["title"] + "</td><td>" + graphdatapointsLES[i]["value"] + "</td><td>" + graphdatapointsTOTAL[i]["value"] + "</td></tr>";
}
htmlOutput += "</table></body></html>";
fs.writeFile(htmlOutputFile, htmlOutput, function(err) {
if(err) {
console.log(err);
}
});
}
else {
console.log(error);
}
});
}
else {
console.log(error);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment