Skip to content

Instantly share code, notes, and snippets.

@zb3
Created January 7, 2016 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zb3/f34681eabe29d23c30db to your computer and use it in GitHub Desktop.
Save zb3/f34681eabe29d23c30db to your computer and use it in GitHub Desktop.
Log songs you've heard on shoutcast.com to a file. Requires a http server and Greasemonkey/Tampermonkey browser extensions.
// ==UserScript==
// @name ShoutCast.com song history
// @author zb3
// @include http://*shoutcast.com*
// @include https://*shoutcast.com*
// @grant none
// ==/UserScript==
(function(){
//change this if you need...
var logUrl = 'http://localhost/shoutlog.php';
var password = 'Nickelback2016'; //well, I'd suggest a better one...
function p(x) {
return ('0'+x).substr(-2);
}
function getTimestamp() {
var date = new Date();
var str = date.getFullYear() + "-" + p(date.getMonth() + 1) + "-" + p(date.getDate()) + " " + p(date.getHours()) + ":" + p(date.getMinutes()) + ":" + p(date.getSeconds());
return str;
}
function logCurrentTrack() {
if (stream && stream.CurrentTrack && stream.CurrentTrack!==logCurrentTrack.lastTrack) {
logCurrentTrack.lastTrack = stream.CurrentTrack;
var line = getTimestamp()+': '+stream.Name+": "+stream.CurrentTrack;
console.log(line);
var xhr = new XMLHttpRequest();
xhr.open('POST', logUrl, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('line='+encodeURIComponent(line)+'&password='+encodeURIComponent(password));
}
setTimeout(logCurrentTrack, 1000);
}
logCurrentTrack();
})();
<?php
//well... it only saves lines to a file, but if I have more time, I may come up with a more sophisticated solution.
$logfile = 'shoutcastcom.log';
$password = 'Nickelback2016';
//we need to enable CORS first
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
if ($_POST['password'] === $password)
file_put_contents($logfile, $_POST['line']."\n", FILE_APPEND | LOCK_EX);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment