]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/AudioScrobbler.php
MINOR: Please don't set a+x on files which are not being executed as shell script...
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / AudioScrobbler.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie
15  * @package   Phergie_Plugin_AudioScrobbler
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie_Plugin_AudioScrobbler
20  */
21
22 /**
23  * Provides commands to look up information on tracks played by specific 
24  * users on the Last.fm and Libre.fm services.
25  *
26  * TODO: Make the "nick-binding" use an SQLite database instead of having them
27  *       hard-coded in to the config file.
28  * 
29  * Configuration settings:
30  * "audioscrobbler.lastfm_api_key":  API given by last.fm (string).
31  * "audioscrobbler.librefm_api_key": API key given by libre.fm (string).
32  * 
33  * @category Phergie
34  * @package  Phergie_Plugin_AudioScrobbler
35  * @author   Phergie Development Team <team@phergie.org>
36  * @license  http://phergie.org/license New BSD License
37  * @link     http://pear.phergie.org/package/Phergie_Plugin_AudioScrobbler
38  * @uses     Phergie_Plugin_Command pear.phergie.org
39  * @uses     Phergie_Plugin_Http pear.phergie.org
40  * @uses     extension simplexml
41  */
42 class Phergie_Plugin_AudioScrobbler extends Phergie_Plugin_Abstract
43 {
44     /**
45      * Last.FM API entry point
46      *
47      * @var string
48      */
49     protected $lastfmUrl = 'http://ws.audioscrobbler.com/2.0/';
50     
51     /**
52      * Libre.FM API entry point
53      *
54      * @var string
55      */
56     protected $librefmUrl = 'http://alpha.dev.libre.fm/2.0/';
57     
58     /**
59      * Scrobbler query string for user.getRecentTracks
60      *
61      * @var string
62      */
63     protected $query = '?method=user.getrecenttracks&user=%s&api_key=%s';
64
65     /**
66      * HTTP plugin
67      *
68      * @var Phergie_Plugin_Http
69      */
70     protected $http;
71     
72     /**
73      * Check for dependencies.
74      *
75      * @return void
76      */
77     public function onLoad()
78     {
79         if (!extension_loaded('simplexml')) {
80             $this->fail('SimpleXML php extension is required');
81         }
82         
83         $plugins = $this->getPluginHandler();
84         $plugins->getPlugin('Command');
85         $this->http = $plugins->getPlugin('Http');
86     }
87     
88     /**
89      * Command function to get a user's status on last.fm.
90      * 
91      * @param string $user User identifier
92      *
93      * @return void
94      */
95     public function onCommandLastfm($user = null)
96     {
97         if ($key = $this->config['audioscrobbler.lastfm_api_key']) {
98             $scrobbled = $this->getScrobbled($user, $this->lastfmUrl, $key);
99             if ($scrobbled) {
100                 $this->doPrivmsg($this->getEvent()->getSource(), $scrobbled);
101             }
102         }
103     }
104
105     /**
106      * Command function to get a user's status on libre.fm.
107      * 
108      * @param string $user User identifier
109      *
110      * @return void
111      */
112     public function onCommandLibrefm($user = null)
113     {
114         if ($key = $this->config['audioscrobbler.librefm_api_key']) {
115             $scrobbled = $this->getScrobbled($user, $this->librefmUrl, $key);
116             if ($scrobbled) {
117                 $this->doPrivmsg($this->getEvent()->getSource(), $scrobbled);
118             }
119         }
120     }
121
122     /**
123      * Simple Scrobbler API function to get a formatted string of the most 
124      * recent track played by a user.
125      * 
126      * @param string $user Username to look up
127      * @param string $url  Base URL of the scrobbler service
128      * @param string $key  Scrobbler service API key
129      *
130      * @return string Formatted string of the most recent track played
131      */
132     public function getScrobbled($user, $url, $key)
133     {
134         $event = $this->getEvent();
135         $user = $user ? $user : $event->getNick();
136         $url = sprintf($url . $this->query, urlencode($user), urlencode($key));
137
138         $response = $this->http->get($url);
139         if ($response->isError()) {
140             $this->doNotice(
141                 $event->getNick(),
142                 'Can\'t find status for ' . $user . ': HTTP ' . 
143                 $response->getCode() . ' ' . $response->getMessage()
144             );
145             return false; 
146         }
147         
148         $xml = $response->getContent();
149         if ($xml->error) {
150             $this->doNotice(
151                 $event->getNick(),
152                 'Can\'t find status for ' . $user . ': API ' . $xml->error
153             );
154             return false; 
155         }
156         
157         $recenttracks = $xml->recenttracks;
158         $track = $recenttracks->track[0];
159         
160         // If the user exists but has not scrobbled anything, the result will
161         // be empty.
162         if (empty($track->name) && empty($track->artist)) {
163             $this->doNotice(
164                 $event->getNick(),
165                 'Can\'t find track information for ' . $recenttracks['user']
166             );
167             return false;
168         }
169         
170         if (isset($track['nowplaying'])) {
171             $msg = sprintf(
172                 '%s is listening to %s by %s',
173                 $recenttracks['user'],
174                 $track->name,
175                 $track->artist
176             );
177         } else {
178             $msg = sprintf(
179                 '%s, %s was listening to %s by %s',
180                 date('j M Y, H:i', (int) $track->date['uts']),
181                 $recenttracks['user'],
182                 $track->name,
183                 $track->artist
184             );
185         }
186         if ($track->streamable == 1) {
187             $msg .= ' - ' . $track->url;
188         }
189         return $msg;
190     }
191 }