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
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
23 * Provides commands to look up information on tracks played by specific
24 * users on the Last.fm and Libre.fm services.
26 * TODO: Make the "nick-binding" use an SQLite database instead of having them
27 * hard-coded in to the config file.
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).
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
42 class Phergie_Plugin_AudioScrobbler extends Phergie_Plugin_Abstract
45 * Last.FM API entry point
49 protected $lastfmUrl = 'http://ws.audioscrobbler.com/2.0/';
52 * Libre.FM API entry point
56 protected $librefmUrl = 'http://alpha.dev.libre.fm/2.0/';
59 * Scrobbler query string for user.getRecentTracks
63 protected $query = '?method=user.getrecenttracks&user=%s&api_key=%s';
68 * @var Phergie_Plugin_Http
73 * Check for dependencies.
77 public function onLoad()
79 if (!extension_loaded('simplexml')) {
80 $this->fail('SimpleXML php extension is required');
83 $plugins = $this->getPluginHandler();
84 $plugins->getPlugin('Command');
85 $this->http = $plugins->getPlugin('Http');
89 * Command function to get a user's status on last.fm.
91 * @param string $user User identifier
95 public function onCommandLastfm($user = null)
97 if ($key = $this->config['audioscrobbler.lastfm_api_key']) {
98 $scrobbled = $this->getScrobbled($user, $this->lastfmUrl, $key);
100 $this->doPrivmsg($this->getEvent()->getSource(), $scrobbled);
106 * Command function to get a user's status on libre.fm.
108 * @param string $user User identifier
112 public function onCommandLibrefm($user = null)
114 if ($key = $this->config['audioscrobbler.librefm_api_key']) {
115 $scrobbled = $this->getScrobbled($user, $this->librefmUrl, $key);
117 $this->doPrivmsg($this->getEvent()->getSource(), $scrobbled);
123 * Simple Scrobbler API function to get a formatted string of the most
124 * recent track played by a user.
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
130 * @return string Formatted string of the most recent track played
132 public function getScrobbled($user, $url, $key)
134 $event = $this->getEvent();
135 $user = $user ? $user : $event->getNick();
136 $url = sprintf($url . $this->query, urlencode($user), urlencode($key));
138 $response = $this->http->get($url);
139 if ($response->isError()) {
142 'Can\'t find status for ' . $user . ': HTTP ' .
143 $response->getCode() . ' ' . $response->getMessage()
148 $xml = $response->getContent();
152 'Can\'t find status for ' . $user . ': API ' . $xml->error
157 $recenttracks = $xml->recenttracks;
158 $track = $recenttracks->track[0];
160 // If the user exists but has not scrobbled anything, the result will
162 if (empty($track->name) && empty($track->artist)) {
165 'Can\'t find track information for ' . $recenttracks['user']
170 if (isset($track['nowplaying'])) {
172 '%s is listening to %s by %s',
173 $recenttracks['user'],
179 '%s, %s was listening to %s by %s',
180 date('j M Y, H:i', (int) $track->date['uts']),
181 $recenttracks['user'],
186 if ($track->streamable == 1) {
187 $msg .= ' - ' . $track->url;