0 ){ $lan_ip = $aAddr[0]; if( $lan_ip == $external_ip ){ $is_local = true; } } } if( $is_local ){ define( "SERVER_HOST", "192.168.0.80" ); echo "Connected to local network\n"; }else{ echo "Connected to foreign network\n"; define( "SERVER_HOST", LAN_HOSTNAME ); define( "HTTP_USER", "torrents" ); define( "HTTP_PASS", "cuntmarauder" ); } define( "RECENT_ALBUM_PATH", "/media.php?type=albums&limit=10" ); define( "RECENT_PODCASTS_PATH", "/media.php?type=podcasts&limit=50" ); define( "CUSTOM_ALBUM_PATH", "/media.php?type=playlist&name=In-car" ); define( "CP_PATH", "C:/bin/cp.exe" ); // Win32 compile of *nix cp define( "SSH_USER", "strawpmobile" ); define( "RSYNC_PATH", "C:/cwRsync/bin/rsync.exe" ); // Win32 compile of *nix rsync define( "SCP_PATH", "C:/bin/pscp.exe" ); // Win32 compile of *nix scp define( "TOUCH_PATH", "C:/bin/touch.exe" ); // Win32 compile of *nix touch define( "FIND_PATH", "C:/bin/ufind.exe" ); // Win32 compile of *nix find define( "MEDIA_DIR", "/mp3" ); define( "PLAYLIST_DIR", MEDIA_DIR."/_playlists/" ); // Yeah, inconsistent with trailing slashes there. They need to be like this though. define( "PODCAST_DIR", MEDIA_DIR."/_podcasts" ); define( "CUSTOM_DIR", MEDIA_DIR."/_custom" ); define( "SERVER_MEDIA_DIR", "/share/music" ); if( array_search( "--notweet", $argv ) === false ){ echo "Sending log to twitter\n"; define( "TWITTER_ENABLED", true ); }else{ echo "Twitter log disabled\n"; define( "TWITTER_ENABLED", false ); } function file_sort($a, $b){ if( $a["mtime"] == $b["mtime"] ) return 0; return $a["mtime"] < $b["mtime"] ? -1 : 1; } function rsync( $src, $dest ){ if( !file_exists( $dest ) ) mkdir( $dest, 0777, true ); $src = str_replace( " ", "\ ", $src ); $src = str_replace( "'", "\'", $src ); $src = str_replace( "&", "\&", $src ); $src = str_replace( "(", "\\(", $src ); $src = str_replace( ")", "\\)", $src ); $cp = RSYNC_PATH." -a ".SSH_USER."@".SERVER_HOST.":\"".$src."\" \"".$dest."\""; system( $cp ); } /** * Get tracks from a list of file locations, copy them to local and write a playlist */ function getTracks( $tracklist, $playlist, $destination, $type ){ $aTracks = split( "\n", $tracklist ); $pls = ""; $aPls = file( PLAYLIST_DIR.$playlist ); $trackcount = 1; $aNames = array(); foreach( $aTracks as $track ){ if( $track == "" ) continue; if( !preg_match( "/\.mp3$/i", $track ) ) continue; $name = array_pop( split( "/", $track ) ); $aNames[] = $name; echo $trackcount.": ".$name; rsync( SERVER_MEDIA_DIR."/".$track, $destination ); if( !file_exists( $destination."/".$name ) ) echo " failed to copy"; echo "\n"; // Touch file $touch = TOUCH_PATH." \"".$destination."/".$name."\""; if( file_exists( $destination."/".$name ) ) system( $touch ); $aPls[$trackcount-1] = "c:".$destination."/".$name; $pls .= "c:".$destination."/".$name."\n"; $trackcount++; file_put_contents( PLAYLIST_DIR.$playlist, implode("\n",$aPls) ); } if( $pls != "" ){ file_put_contents( PLAYLIST_DIR.$playlist, $pls ); if( TWITTER_ENABLED ) tweet( "Finished getting ".$type ); }else{ // Something went wrong if( TWITTER_ENABLED ) tweet( "Writing the ".$type." playlist went wrong :(" ); echo $name." playlist empty - skipping playlist write\n"; } // Delete files that don't need to be there $dh = opendir( $destination ); while( $file = readdir( $dh ) ){ $file = trim( $file ); if( !preg_match( "/\.mp3$/i", $file ) ) continue; if( false === array_search( $file, $aNames ) ){ echo "Deleting ".$file."\n"; unlink( $destination."/".$file ); } } } // What time of day is it? $hour = date( "H" ); if( $hour > 0 && $hour < 12 ){ $time = "morning"; }elseif( $hour < 18 ){ $time = "afternoon"; }else{ $time = "evening"; } if( $is_local ){ $from = " from the ".DEVICE_NAME." lair"; }else{ $from = " from some strange place"; if( TWITTER_ENABLED ) sendMessage( OWNER_NAME, "At IP: ".$external_ip ); } if( TWITTER_ENABLED ) tweet( "Good ".$time.$from ); echo date("Y-m-d H:i:s").": Syncing with media server\n"; // See if there is a custom album available if( $is_local ){ $page = @file_get_contents( "http://".SERVER_HOST.CUSTOM_ALBUM_PATH ); }else{ $page = @file_get_contents( "http://".HTTP_USER.":".HTTP_PASS."@".SERVER_HOST.CUSTOM_ALBUM_PATH ); } $count = 1; if( sizeof( $page ) > 0 ){ if( TWITTER_ENABLED ) tweet( "Getting custom mix playlist for CD1" ); getTracks( $page, "1.m3u", CUSTOM_DIR, "custom mix" ); $count = 2; } // Get recent albums if( $is_local ){ $page = @file_get_contents( "http://".SERVER_HOST.RECENT_ALBUM_PATH ); }else{ $page = @file_get_contents( "http://".HTTP_USER.":".HTTP_PASS."@".SERVER_HOST.RECENT_ALBUM_PATH ); } if( !$page ){ if( TWITTER_ENABLED ) tweet( "Hmm, couldn't find the album list" ); die( "Couldn't load album list" ); } $aAlbums = split( "\n", $page ); echo "Getting albums\n"; foreach( $aAlbums as $album ){ if( $album == "" ) continue; $a = split( "/", $album ); $name = array_pop( $a ); $artist = array_pop( $a ); $dest = $artist."/".$name; // Create destination folder if( !file_exists( MEDIA_DIR."/".$artist )){ mkdir( MEDIA_DIR."/".$artist ); } echo $count.": ".$dest."\n"; // Rsync rsync( SERVER_MEDIA_DIR."/".$album, MEDIA_DIR."/".$artist ); $aMp3s = array(); // Get list of mp3s in copied folder $dh = opendir( MEDIA_DIR."/".$dest ); while( $file = readdir( $dh ) ){ if( preg_match( "/\.mp3$/", $file ) ){ $aMp3s[] = "../".$dest."/".$file; } } if( sizeof( $aMp3s ) == 0 ) continue; // Write the playlist file sort( $aMp3s ); $mp3s = implode( "\n", $aMp3s ); $pls_name = $count.".m3u"; if( sizeof( $aMp3s ) > 0 ){ file_put_contents( PLAYLIST_DIR.$pls_name, $mp3s ); if( TWITTER_ENABLED ) tweet( "CD ".$count.": ".$dest ); // Touch dir $touch = TOUCH_PATH." \"".MEDIA_DIR."/".$dest."\""; if( file_exists( MEDIA_DIR."/".$dest ) ) system( $touch ); $count++; }else{ $message = "Skipping ".$dest." - couldn't find enough MP3s for a playlist"; echo $message."\n"; if( TWITTER_ENABLED ) tweet( $message ); } if( $count >= 6 ) break; } // Get latest podcasts echo "\nGetting podcasts\n"; if( TWITTER_ENABLED ) tweet( "Getting podcasts and assigning them to CD 6" ); if( $is_local ){ $page = file_get_contents( "http://".SERVER_HOST.RECENT_PODCASTS_PATH ); }else{ $page = @file_get_contents( "http://".SERVER_USER.":".SERVER_PASS."@".SERVER_HOST.RECENT_PODCASTS_PATH ); } getTracks( $page, "6.m3u", PODCAST_DIR, "podcasts" ); // Delete anything too old $dh = opendir( PODCAST_DIR ); $a = array(); while( $file = readdir( $dh ) ){ if( !preg_match( "/\.mp3$/", $file ) ) continue; $a[] = array( "mtime" => filemtime( PODCAST_DIR."/".$file ), "name" => $file ); } usort( $a, "file_sort" ); $a = array_reverse( $a ); echo "\n"; while( sizeof( $a ) > 50 ){ $f = array_pop( $a ); unlink( PODCAST_DIR."/".$f["name"] ); echo "Deleting ".$f["name"]."\n"; } echo date("Y-m-d H:i:s").": Finished\n"; if( TWITTER_ENABLED ) tweet( "...and I'm spent" ); ?>