Skip to content

Commit

Permalink
Station switching! We're only using the keyboard right now. Adding mo…
Browse files Browse the repository at this point in the history
…use integration next. First implementation of #2
  • Loading branch information
jacroe committed Feb 19, 2013
1 parent 62fe068 commit 17ce225
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ albumart/*.*
ctl
curSong
msg
stationList
pianobar.csv
28 changes: 26 additions & 2 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,27 @@
else
{
file_put_contents("ctl", "$c\n");
if ($_GET['control'] == "n") file_put_contents("msg", "Skipped");

if ($c == "n") file_put_contents("msg", "Skipped");
if ($c[0] == "s") file_put_contents("msg", "Changing stations");
$return = "ok";
}
}
elseif ($_GET['station'] != null)
{
$i = $_GET['station']*10;
$max = $i+10;
$arrayStations = explode("|", file_get_contents("stationList"));

if ($i > 0) $return = "B - Back<br />";
for($i; ($i < $max) && ($i < count($arrayStations) ); $i++)
{
$stationRaw = $arrayStations[$i];
$station = explode("=", $stationRaw);
$return .= substr($station[0], -1)." - ".$station[1]."<br />";
}
if (count($arrayStations) > $max) $return .= "N - Next<br />";
}
else $return = getSong();

echo $return;
Expand Down Expand Up @@ -67,9 +84,16 @@ function getDetails($url = NULL)
#preg_match("#features of this track(.*?)\<p\>These are just a#is", $data, $matches); // uncomment this if explanations act funny
preg_match("#features of this track(.*?)\</div\>#is", $data, $matches);
$strip = array("Features of This Track</h2>", "<div style=\"display: none;\">", "</div>", "<p>These are just a");
if (!$matches[0]) return "We were unable to get the song's explanation. Sorry about that.";
$data = explode("<br>", str_replace($strip, "", $matches[0]));
unset($data[count($data)-1]);
if (trim($data[count($data)-1]) == "many other comedic similarities")
{
$ending = "many other comedic similarities";
unset($data[count($data)-1]);
}
else $ending = "many other similarites as identified by the Music Genome Project";
$data = implode(", ", array_map('trim', $data));
return "We're playing this track because it features $data, and many other similarites as identified by the Music Genome Project.";
return "We're playing this track because it features $data, and $ending.";
}
?>
51 changes: 42 additions & 9 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<script src="mousetrap.js"></script>
<script>
$(document).ready(function(){

oldData = $('#content').html();
window.setInterval(function(){
$.get("api.php", function(newData)
Expand All @@ -15,18 +16,39 @@
{
oldData = newData;
$('#content').fadeOut('slow', function(){$(this).html(newData).fadeIn('slow')});
setMousetraps();
}
});
}, 3000);


Mousetrap.bind(['p', 'space'], function() { control('p'); });
Mousetrap.bind('n', function() { control('n'); });
Mousetrap.bind('l', function() { control('+'); });
Mousetrap.bind('b', function() { control('-'); });
Mousetrap.bind('t', function() { control('t'); });
Mousetrap.bind('e', function() { explain(); });
});
function stationSetup()
{
index = 0;
getStations(index);
Mousetrap.reset();
Mousetrap.bind('0', function() { control('s'.concat(index,'0')); setMousetraps(); });
Mousetrap.bind('1', function() { control('s'.concat(index,'1')); setMousetraps(); });
Mousetrap.bind('2', function() { control('s'.concat(index,'2')); setMousetraps(); });
Mousetrap.bind('3', function() { control('s'.concat(index,'3')); setMousetraps(); });
Mousetrap.bind('4', function() { control('s'.concat(index,'4')); setMousetraps(); });
Mousetrap.bind('5', function() { control('s'.concat(index,'5')); setMousetraps(); });
Mousetrap.bind('6', function() { control('s'.concat(index,'6')); setMousetraps(); });
Mousetrap.bind('7', function() { control('s'.concat(index,'7')); setMousetraps(); });
Mousetrap.bind('8', function() { control('s'.concat(index,'8')); setMousetraps(); });
Mousetrap.bind('9', function() { control('s'.concat(index,'9')); setMousetraps(); });
Mousetrap.bind('n', function() { getStations(++index); })
Mousetrap.bind('b', function() { getStations(--index); })
Mousetrap.bind('esc', function()
{
$('#content').fadeOut('slow', function(){$(this).html(oldData).fadeIn('slow')});
setMousetraps();
});
}
function getStations(index)
{
$.get("api.php", {station:index})
.done(function(stationList) {$('#content').fadeOut('slow', function(){$(this).html(stationList).fadeIn('slow')}); });
}
function control(action)
{
$.get("api.php", {control:action});
Expand All @@ -38,10 +60,21 @@ function explain()
{
$('p.details').html("Grabbing explanation...").fadeToggle("slow");
$.get("api.php",{control:"e"})
.done(function(data) { $('p.details').fadeOut('slow', function(){$(this).html(data).fadeIn('slow')}); });
.done(function(data) { $('p.details').fadeOut('slow', function(){$(this).html(data).fadeIn('slow')}); });
}
else {$('p.details').fadeToggle("slow");}
};
function setMousetraps()
{
Mousetrap.reset();
Mousetrap.bind(['p', 'space'], function() { control('p'); });
Mousetrap.bind('n', function() { control('n'); });
Mousetrap.bind('l', function() { control('+'); });
Mousetrap.bind('b', function() { control('-'); });
Mousetrap.bind('t', function() { control('t'); });
Mousetrap.bind('e', function() { explain(); });
Mousetrap.bind('s', function() { stationSetup(); });
}
</script>
</head>
<body>
Expand Down

0 comments on commit 17ce225

Please sign in to comment.