Skip to content

Instantly share code, notes, and snippets.

@triktron
Created July 20, 2017 09:33
Show Gist options
  • Save triktron/1655064f9a4a4577504a92769fc92d71 to your computer and use it in GitHub Desktop.
Save triktron/1655064f9a4a4577504a92769fc92d71 to your computer and use it in GitHub Desktop.
this is a small module to get Monstercat podcast tracklist from reddit.
/**
* this is a small module to get Monstercat podcast tracklist from reddit.
*
* I plan to use this to get what track is playing live!
*
* thanks to @thefourtheye https://stackoverflow.com/questions/22581979/how-to-return-only-captured-groups-in-a-javascript-regex-with-multiple-matches
*
* Monstercat and reddit is not mine (obviously)
* Feel free to use this code however you want but be so kind to link me if you use it.
*/
const regexs = {
tracks: /((?:\d+:?)+) (.+?(?= - )) - (.+)/g,
throwback: /\* (?:\*\*)?(.+) - (.+) \[(.+)\]/g,
winner: /Next upcoming throwback: \*\*.+ - (.+)\*\*/g
}
/**
* it gets the tracks of the podcast.
* @method getPodcastTracklist
* @param {String|Number} n - the number of the podcast, if none is supplied its gets the latest
* @return {Promise} - a promise that resolves the tracklist
*/
function getPodcastTracklist(n) {
var name = encodeURIComponent("Monstercat: Call of the Wild" + (n ? " Ep. " + n : ""))
var url = "https://www.reddit.com/search.json?q=" + name + "&sort=new&limit=1";
return get(url).then(function(data) {
var stream = JSON.parse(data).data.children[0].data;
var text = stream.selftext.replace(/&/g,"&");
var match = regexs.tracks.exec(text);
var tracks = [];
while (match !== null) {
tracks.push({
timestamp: match[1],
artist: match[2],
title: match[3].trim().replace(/\*/g,"")
});
match = regexs.tracks.exec(text);
}
var match = regexs.throwback.exec(text);
var throwback = [];
while (match !== null) {
throwback.push({
artist: match[1],
title: match[2],
date: match[3]
});
match = regexs.throwback.exec(text);
}
var match = regexs.winner.exec(text);
var throwback_winner = {};
if (match !== null) {
throwback_winner = throwback.find(function(a) {return a.title == match[1]})
}
return {
tracks: tracks,
throwback: throwback,
throwback_winner: throwback_winner
}
})
}
/**
* this is just a simple reqest module
* @method
* @param {String} url the url to load
* @return {string} the content of that url
*/
const https = require("https");
const http = require("http");
var get = function(url) {
return new Promise((resolve, reject) => {
var lib = url.startsWith('https') ? https : http;
var request = lib.get(url, function(response) {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}
var body = [];
response.on('data', function(chunk) {body.push(chunk)});
response.on('end', function() {resolve(body.join(''))});
});
request.on('error', function(err) {reject(err)})
})
};
/**
* the most simple example i could come up with
*/
getPodcastTracklist().then(console.log).catch(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment