<?php
/* 
    simple_api.php 
    Created: 02.14.10 by Jason "pixelat3d" snively pixelat3d@siliconfreaks.com
    Last Updated: 

    How to use ... 

    $simple_lastfm    = new simple_lastfm( 'pixelat3d' ); 
    $recent_tracks    = $simple_lastfm->recent_tracks( ); 

    $simple_twitter    = new simple_twtiter( 'pixelat3d' ); 
    $tweets        = user_timeline( );

    $simple_digg    = new_simple_digg( 'pixelat3d' ); 
    $diggs        = $simple_digg->get_dugg( ); 

    parse objects as needed. Note that last.fm uses stupid names so in order to get 
    to some of the objects you're going to need to call get_object_vars( ) on some of 
    th obejects to get the data. #text ... i'm looking at you. 

    Feel free to extend (unsimple) modify, and whatnot to your hearts extent. Would be nice
    if you credited the original in there though : )
*/

class api_cruncher 
    
protected $username
    
protected $password
    
protected $format
    
private    $version 0.5

    
public function __construct$username$password ''$format 'json' ) {
        
$this->username $username
        
$this->password $password
        
$this->format $format;    
    }

    
final protected function request_data$url$format 'json' ) { 
        
$ch    curl_init$url ); 
    
        
// If requred auth, then auth.     
        
if ( !empty( $this->username ) && !empty( $this->password ) ) { 
            
curl_setopt$chCURLOPT_USERPWD$this->username.':'.$this->password);
        }

        
curl_setopt$chCURLOPT_VERBOSE);
        
curl_setopt$chCURLOPT_NOBODY);
        
curl_setopt$chCURLOPT_HEADER); 
        
curl_setopt$chCURLOPT_TIMEOUT);
        
curl_setopt$chCURLOPT_USERAGENT'simple_api/'.$this->version );
        
curl_setopt$chCURLOPT_FOLLOWLOCATION);        
        
curl_setopt$chCURLOPT_RETURNTRANSFER); 

        
$response     curl_exec$ch ); 
        
$r_info        curl_getinfo$ch ); 
        
curl_close$ch );

        if( 
$r_info['http_code'] == 200 ) {

            switch( 
strtolower$this->format ) ) {
                case 
'json': return ( json_decode$response ) ); break; 
                case 
'xml': return( $response ); break; 
                default: return( 
$response ); break; 
            }

        } else {
            return ( 
"Error # $r_info[http_code]" ); 
        }

    }

    
public function get_user( ) { return( $this->username ); }
    
public function set_user$username ) { $this->username $username; }

    
public function get_format( ) { return( $this->format ); }
    
public function set_format$format ) { $this->format $format; }

    
public function set_password$password ) { $this->password $password; }

}

class 
simple_twitter extends api_cruncher {

    
public function user_timeline$count 20$offset ) {
        
$url 'http://twitter.com/statuses/user_timeline/'.$this->username.'.'.$this->format.'?count='.$count;
        return ( 
$this->request_data$url$this->format ) );
    }
}


class 
simple_lastfm extends api_cruncher {
    
private $apikey 'b25b959554ed76058ac220b7b2e0a026'//Taken from API spec page. Probalby should get your own ; )

    
public function recent_tracks$count 10$offset ) {
        
$url =     'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='.$this->username.
            
'&api_key='.$this->apikey.'&limit='.$count.'&nowplaying=false';
        
$url .= ( $this->format !== 'xml' ) ? "&format=$this->format" ''
        return( 
$this->request_data$url$this->format ) );
    }

    
public function set_apikey$apikey ) { $this->apikey $apikey; }
    
public function get_apikey( ) { return( $this->apikey ); }
}



class 
simple_digg extends api_cruncher 
    
private $apikey ''

    
public function get_dugg$count 5$offset ) {
        
$url =     'http://services.digg.com/1.0/endpoint?method=user.getDugg&offset=0&username='.$this->username.
            
'&type='.$this->format.'&count='.$count;
        return( 
$this->request_data$url$this->format ) );     
    }

    
public function set_apikey$apikey ) { $this->apikey $apikey; }
    
public function get_apikey( ) { return( $this->apikey ); }
}


?>