Welcome Guest, Not a member yet? Register   Sign In
Last FM Scrobbler CI Library
#1

[eluser]Mushex Antaranian[/eluser]
There are some CI libraries to retrieve information from Last FM, there is a nice one developed by Elliot Haughin, but none of them has scrobble functionality, that was needed for my project (CMS for musicians), and I developed one

Source of library Lastfm_lib.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Lastfm_lib {
    public $scrobbler;

    function  __construct() {
        $this->scrobbler = new scrobbler();
    }
    function auth($data) {
        $auth = $this->scrobbler->auth($data['username'], $data['password']);
        return $auth;
    }
    function call($action, $data) {
        return $this->scrobbler->call($action, $data);
    }
}


class scrobbler
{
    // curl timeout
    const TIMEOUT       = 15;

    private $scrobbler_url = 'http://post.audioscrobbler.com/?hs=true&p=1.2.1';
    private $client_id = 'tst';
    private $client_ver = '1.0';

    private $session_id  = '';
    private $url = array();

    private $data = array('artist'         => array('name' => 'a', 'value' => '', 'action' => 'both', 'urlencode' => TRUE),
                          'track'          => array('name' => 't', 'value' => '', 'action' => 'both', 'urlencode' => TRUE),
                          'album'          => array('name' => 'b', 'value' => '', 'action' => 'both', 'urlencode' => TRUE),
                          'track_number'   => array('name' => 'n', 'value' => '', 'action' => 'both', 'urlencode' => FALSE),
                          'track_duration' => array('name' => 'l', 'value' => '150', 'action' => 'both', 'urlencode' => FALSE),
                          'rating'         => array('name' => 'r', 'value' => '', 'action' => 'both', 'urlencode' => FALSE),
                          'mb_track_id'    => array('name' => 'm', 'value' => '', 'action' => 'both', 'urlencode' => FALSE),
                          'source'         => array('name' => 'o', 'value' => 'P', 'action' => 'submit', 'urlencode' => FALSE),
                          'scrobble_time'  => array('name' => 'i', 'value' => '', 'action' => 'submit', 'urlencode' => FALSE)
                          );

    public function __construct(){
    }

    public function auth($username, $password) {
         if(empty($username) or empty($password)){
            return false;
        }
        $timestamp = time();
        $auth = md5(md5($password) . $timestamp);
        $this->scrobbler_url .= '&c=' . $this->client_id . '&v=' . $this->client_ver . '&u=' . $username . '&t=' . $timestamp . '&a=' . $auth;
        $curl = curl_init($this->scrobbler_url);
        curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $result = curl_exec($curl);
        curl_close($curl);
        $result = explode("\n", $result);
        if($result[0] != 'OK') {
            return false;
        } else {
            $this->session_id        = trim($result[1]);
            $this->url['play']    = trim($result[2]);
            $this->url['submit']    = trim($result[3]);
            return true;
        }
        return false;
    }

    public function call($action, $data) {
        if(empty($data)){
            return FALSE;
        }
        if(empty($this->session_id)){
            return FALSE;
        }
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_URL, $this->url[$action]);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $this->generate_url($action, $data));
        $result = curl_exec($curl);
        curl_close ($curl);

        $result = explode("\n", $result);
        if($result[0] != 'OK'){
            return FALSE;
        } else {
            return TRUE;
        }
        return FALSE;
    }

    /**
     * Initialising request url
     *
     * @param string $action can set to 'submit' for submit url generation and anything (and even 'play') for play url
     * @param array $data can have keys (artist, track, album, track_number, track_duration, rating, mb_track_id, source, scrobble_time)
     * @return string generated url
     */
    private function generate_url($action, $data) {
        $url = 's=' . $this->session_id . '&';
        if(!isset($data['scrobble_time']) || empty($data['scrobble_time'])){
            $this->data['scrobble_time']['value'] = time();
        }
        foreach ($this->data as $field => $field_data) {
            if (isset($data[$field])) {
                $field_data['value'] = $data[$field];
            }
            if ($field_data['action'] == $action || $field_data['action'] == 'both') {
                $field_data['value'] = $field_data['urlencode'] ? urlencode($field_data['value']) : $field_data['value'];
                $field_data['name'] = ($action == 'submit') ? $field_data['name'] . '[0]' : $field_data['name'];
                $url .= $field_data['name'] . '=' . $field_data['value'] . '&';
            }
        }
        return $url;
    }
}
?>
#2

[eluser]Mushex Antaranian[/eluser]
Basic controller will look like
Code:
<?php
class Welcome extends Controller {
    function Welcome()
    {
       parent::Controller();
       $this->load->library('lastfm_lib');
    }
    var $facebook;

    function lastfm($action = 'submit', $data = array('artist' => 'Moby', 'track' => 'Love Should')) {
       $user = array('username' => 'YOUR_USERNAME', 'password' => 'YOUR_PASSWORD');
       $this->lastfm_lib->auth($user);
       $this->lastfm_lib->call($action, $data);
    }
}

Thats all, folks )

If any questions or bug found, feel free to contact me at jesirobendebua@gmail




Theme © iAndrew 2016 - Forum software by © MyBB