Monday, October 5, 2009

RhythmToWeb Updated

I've recently received an e-mail from Aaron Hill, about some modifications he's done to RhythmToWeb. I took his idea of storing information about more than one song, and adapted it in my own way. You can now use the buttons under the song information on the right to browse the last 5 songs I've played. It will also automatically refresh, so if you wait around long enough you'll see it switch to a new song when I start playing one (I really don't know who I'm kidding, no one will ever wait on my blog to see when my song changes :P). Anyway, on to the code:
PHP, this gets called by Rhythmbox:<?phpdefine('CALLBACK','rtw_callback');define('JS_FILE','nowplaying.js');define('MAX_ENTRIES',5);define('SERIALIZE_FILE','./data');functiontest_value($var){if(strlen($var)&&mb_strtolower($var)!='unknown'&&$var!='0'){returntrue;}returnfalse;}$song_info=array();foreach($_GETas$key=>$value){if(test_value($value)){$song_info[$key]=$value;}}$last_songs=unserialize(file_get_contents(SERIALIZE_FILE));if($last_songs===false)$last_songs=array();elseif(count($last_songs)>=MAX_ENTRIES){while(count($last_songs)>=MAX_ENTRIES){array_shift($last_songs);}}$last_songs[]=$song_info;file_put_contents(SERIALIZE_FILE,serialize($last_songs));file_put_contents(JS_FILE,CALLBACK.'('.json_encode($last_songs).')');?>