Created
December 12, 2018 18:43
-
-
Save hussainweb/42aa37179c913476414c7f31838a133e to your computer and use it in GitHub Desktop.
Get a few statistics about episode duration on syntax.fm
This file contains hidden or 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
const fs = require('fs'); | |
const rssParser = require('rss-parser'); | |
const stats = require("stats-lite"); | |
(async () => { | |
const parser = new rssParser(); | |
const feed = await parser.parseURL('https://feed.syntax.fm/rss'); | |
// Or you can download the file manually with wget, like so: | |
// wget -O rss.rss https://feed.syntax.fm/rss | |
// comment the line above with parseURL | |
// and uncomment the following two lines | |
// const xmlString = fs.readFileSync('./rss.rss'); | |
// const feed = await parser.parseString(xmlString); | |
const durations = feed.items.map(item => { | |
const duration = item.itunes.duration || '00:00'; | |
return duration.split(':').reduce((acc, time) => (60 * acc) + +time); | |
}); | |
const formatDuration = duration => { | |
let seconds = Math.floor(duration % 60); | |
duration /= 60; | |
let minutes = Math.floor(duration % 60); | |
duration /= 60; | |
let hours = Math.floor(duration % 24); | |
duration /= 24 | |
let days = Math.floor(duration); | |
return `${days} days ${hours}:${minutes}:${seconds}`; | |
}; | |
console.log("sum: %s", formatDuration(stats.sum(durations))); | |
console.log("mean: %s", formatDuration(stats.mean(durations))); | |
console.log("median: %s", formatDuration(stats.median(durations))); | |
console.log("variance: %s", formatDuration(stats.variance(durations))); | |
console.log("standard deviation: %s", formatDuration(stats.stdev(durations))); | |
console.log("sample standard deviation: %s", formatDuration(stats.sampleStdev(durations))); | |
console.log("85th percentile: %s", formatDuration(stats.percentile(durations, 0.85))); | |
console.log("histogram:", stats.histogram(durations, 10)); | |
})(); |
This file contains hidden or 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
{ | |
"dependencies": { | |
"rss-parser": "^3.5.4", | |
"stats-lite": "^2.2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment