# Copyright (C) 2005 Brad Cavanagh. All Rights Reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA function display_lastfm_info( $username, $max = 5, $list = "recenttracks", $prefix = "", $suffix = "", $stats = 0 ) { # Get the list. It can be one of "topartists", "topalbums", # "toptracks", "tags", "friends", "neighbours", or "recenttracks". switch( strtolower( $list ) ) { case "topartists": $filtlist = "topartists"; break; case "topalbums": $filtlist = "topalbums"; break; case "toptracks": $filtlist = "toptracks"; break; case "tags": $filtlist = "tags"; break; case "friends": $filtlist = "friends"; break; case "neighbours": $filtlist = "neighbours"; break; default: $filtlist = "recenttracks"; } if( $stats ) { $displaystats = 1; } else { $displaystats = 0; } if( strlen( $prefix . '' ) == 0 ) { $prefix = ''; } if( strlen( $suffix . '' ) == 0 ) { $suffix = ''; } if( $max > 10 ) { $max = 10; } elseif( $max < 0 ) { $max = 5; } # Set the cache filename. $cachefile = "as_" . $filtlist . ".txt"; # Set the URL to the audioscrobbler webservice. $url = "http://ws.audioscrobbler.com/1.0/user/$username/$filtlist.txt"; # Check to see if the cache file exists. If it doesn't, or it # does and its last modified time is more than thirty seconds # ago, download a new one. if( ! file_exists( $cachefile ) || ( ( time() - filemtime( $cachefile ) > 30 ) ) ) { $fp = fopen( $url, "r" ); if( $fp ) { $cache_fp = fopen( $cachefile, "w" ); if( $cache_fp ) { while( ! feof( $fp ) ) { $string = fgets( $fp, 4096 ); fwrite( $cache_fp, $string ); } } else { echo "Unable to open cache file for writing"; } } else { echo "Unable to contact audioscrobbler.com"; return; } # Download a new copy, saving it to the cache file. /* $ch = curl_init( $url ); $fp = fopen( $cachefile, "w" ); curl_setopt( $ch, CURLOPT_FILE, $fp ); curl_setopt( $ch, CURLOPT_HEADER, 0 ); curl_exec( $ch ); curl_close( $ch ); fclose( $fp ); */ } # We've got the cache file, so open it up and write out however # many tracks the user requested. $fp = fopen( $cachefile, "r" ); if( $fp ) { $linenumber = 1; while( ! feof( $fp ) && $linenumber <= $max ) { switch( $filtlist ) { case "topartists": case "topalbums": case "toptracks": $pieces = explode( ",", fgets( $fp, 4096 ), 3 ); if( $displaystats ) { echo $prefix . rtrim( $pieces[2] ) . " (" . $pieces[1] . ")" . $suffix . "\n"; } else { echo $prefix . rtrim( $pieces[2] ) . $suffix . "\n"; } break; case "tags": $pieces = explode( ",", fgets( $fp, 4096 ), 3 ); if( $displaystats ) { echo $prefix . rtrim( $pieces[1] ) . " (" . $pieces[0] . ")" . $suffix . "\n"; } else { echo $prefix . rtrim( $pieces[1] ) . $suffix . "\n"; } break; case "friends": $pieces = fgets( $fp, 4096 ); echo $prefix . rtrim( $pieces ) . $suffix . "\n"; break; case "neighbours": $pieces = explode( ",", fgets( $fp, 4096 ), 2 ); if( $displaystats ) { echo $prefix . rtrim( $pieces[1] ) . " (" . $pieces[0] . "%)" . $suffix . "\n"; } else { echo $prefix . rtrim( $pieces[1] ) . $suffix . "\n"; } break; case "recenttracks": $pieces = explode( ',', fgets( $fp, 4096 ), 2 ); echo $prefix . rtrim( $pieces[1] ) . $suffix . "\n"; break; } $linenumber++; } } else { echo "Unable to open audioscrobbler cachefile $cachefile for reading"; return; } fclose( $fp ); } ?>