CodeIgniter Lastfm Library

Little improvements for CodeIgniter Lastfm Library ( http://codeigniter.com/forums/viewthread/82361/ ): No double upload for images.

 (0) Написал recoilme, 2008-08-19 17:39:51  ответить



<?php
/**
 * CodeIgniter Lastfm Library (http://dirkkelly.com/
 * 
 * Author:         Dirk Kelly (http://dirkkelly.com)
 **/
    
class Lastfm
    
{
        var 
$lastfmuser;
        var 
$feed;
        var 
$start;
        var 
$amount;
        var 
$offset;
        var 
$cachetime;
        var 
$cachedir;
        var 
$cacheurl;
        var 
$debug;
        
        function 
init($config)
        {
            
$this->lastfmuser         'kellydir';    /*Last.FM Username*/
            
$this->feed         'topalbums';    /*Feed to output*/
            
$this->start        0;            /*Start of Return*/
            
$this->amount        6;            /*Amount Of Rows to Return*/
            
$this->offset        8;            /*Perth, Australia... That's Right*/
                
            
$this->cachetime     48;            /*Hours 168?*/
            
$this->cachedir        $_SERVER['DOCUMENT_ROOT']        .    "/userdata/lastfm/";
            
$this->cacheurl        base_url()    .    "userdata/lastfm/";

            
$this->debug        false;
            
            foreach (
$config as $key => $value)
            {
                
$this->$key $value;
            }
        }
        
        function 
getData()
        {            
         
//http://ws.audioscrobbler.com/1.0/user/recoilme/topartists.xml
            
$remote    "http://ws.audioscrobbler.com/1.0/user/$this->lastfmuser "/" $this->feed ".xml";
            
$local    $this->cachedir $this->lastfmuser "/" $this->feed ".xml";
                        
                    
            if(!
$this->createCache($local,$remote))
            {
                echo 
"Error <b>creating</b> cache file &#40; " $local "&#41;";
            }

            
$feed simplexml_load_file&#40;$local&#41;;

            
$return $this->createOutput($feed);

            return 
$return;
        }
        
        function 
createCache($local,$remote,$imgpref="")
        {    
            
$return true;
            
$create true;
                    
            if(!
is_dir($this->cachedir "/" $this->lastfmuser))
            {
                if(!
mkdir($this->cachedir "/" $this->lastfmuser))
                {
                    
$return false;
                    if(
$this->debug)
                    {
                        echo 
"Can't Make Cache Directory";
                    }
                }
            }
            if(
file_exists($local)) 
            {
                
$diff = (time() - filemtime($local))/60*60;
                
                if (
$diff <= $this->cachetime*60*60
                {
                    
$create false;
                }
                if (
$imgpref !=''$create false;//нехрен качать картинки 10 раз
            
}
            
//$create = true;
            
if($create)
            {
             
set_time_limit(60);
                if(!
$remoteData fopen&#40;$remote, "r"&#41;)
                
{
                    
$return false;
                    if(
$this->debug)
                    {
                        echo 
"Can't Open Remote Feed";
                    }
                }
                if(!
file_put_contents($local,$remoteData))
                {
                    
$return false;
                    if(
$this->debug)
                    {
                        echo 
"Can't Write Local Cache File";
                    }
                }
                if(!
fclose($remoteData))
                {
                    
$return false;
                    if(
$this->debug)
                    {
                        echo 
"Cant Close Cache File";
                    }
                }
            }
                            
            return 
$return;
        }
        
        function 
dataCache($dataURI,$name="")
        {
            
$remote $dataURI;
            if (
$name == "") {
             
$local    $this->cachedir $this->lastfmuser "/" $name basename($dataURI);
            } else {
             
$local    $this->cachedir "/" $name basename($dataURI);
            }
            
            if(!
$this->createCache($local,$remote,$name))
            {
                echo 
"Error <b>creating</b> cache file &#40; " $local "&#41;";
            }            
            
            if (
$name == "") return $this->cacheurl $this->lastfmuser "/" $name basename($dataURI);
            else return 
$this->cacheurl $name basename($dataURI);
        }
        
        function 
createOutput($data)
        {
            
$i $this->start;
            
            if(
$this->feed == 'recenttracks')
            {
                foreach(
$data->track as $track)
                {
                    if(
$i $this->amount)
                    {
                        
$return[$i]['name']        = $track->name;
                        
$return[$i]['artist']     = $track->artist;    
                        
$return[$i]['album']    = $track->album;
                        
$return[$i]['url']        = $track->url;
                        
$return[$i]['time']     = $track->date['uts']*$this->offset*60*60;
                        
$i++;
                    }
                }
            }
            else if(
$this->feed == 'weeklytrackchart')
            {
                foreach(
$data->track as $track)
                {
                    if(
$i $this->amount)
                    {
                        
$return[$i]['name']        = $track->name;
                        
$return[$i]['artist']     = $track->artist;
                        
$return[$i]['rank']        = $track->chartposition;
                        
$return[$i]['count']    = $track->playcount;
                        
$return[$i]['url']        = $track->url;
                        
$i++;
                    }
                }
            }
            else if(
$this->feed == 'weeklyartistchart')
            {
                foreach(
$data->artist as $track)
                {
                    if(
$i $this->amount)
                    {
                        
$return[$i]['artist']    = $track->name;
                        
$return[$i]['rank']        = $track->chartposition;
                        
$return[$i]['count']    = $track->playcount;
                        
$return[$i]['url']        = $track->url;
                        
$i++;
                    }
                }
            }
            else if(
$this->feed == 'topartists')
            {
                foreach(
$data->artist as $track)
                {
                    if(
$i $this->amount)
                    {
                        
$return[$i]['artist']    = $track->name;
                        
$return[$i]['rank']        = $track->rank;
                        
$return[$i]['count']    = $track->playcount;
                        
$return[$i]['url']        = $track->url;
                        
$return[$i]['thumb']    = $this->dataCache($track->thumbnail,'thumb_');
                        
//$track->image = str_replace('/126/','/252/',$track->image);
                        
$return[$i]['image']    = $this->dataCache($track->image,'img_');
                        
$i++;
                    }
                }
            }
            else if(
$this->feed == 'toptracks')
            {
                foreach(
$data->track as $track)
                {
                    if(
$i $this->amount)
                    {
                        
$return[$i]['name']        = $track->name;
                        
$return[$i]['artist']    = $track->artist;
                        
$return[$i]['rank']        = $track->rank;
                        
$return[$i]['count']    = $track->playcount;
                        
$return[$i]['url']        = $track->url;
                        
$i++;
                    }
                }
            }
            else 
//topalbums
            
{
             
//not work
            
}
            return 
$return;
        }
    }
?>



(0)  Написал recoilme, 2008-08-19 17:44:52  ответить