20
18
* along with this program; if not, write to the Free Software
21
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
* @category AmpacheLastfm
25
* @copyright 2001 - 2011 Ampache.org
26
* @license http://opensource.org/licenses/gpl-2.0 GPLv2
27
* @link http://www.ampache.org/
35
* @category AmpacheLastfm
37
* @copyright 2001 - 2011 Ampache.org
38
* @license http://opensource.org/licenses/gpl-2.0 GPLv2
39
* @link http://www.ampache.org/
41
23
class AmpacheLastfm {
43
public $name ='Last.FM';
44
public $description ='Records your played songs to your Last.FM Account';
46
public $version ='000004';
47
public $min_ampache ='360003';
48
public $max_ampache ='999999';
50
// These are internal settings used by this class, run this->load to
62
* This function does nothing...
64
public function __construct() {
72
* This is a required plugin function. It inserts our preferences
75
public function install() {
77
// Check and see if it's already installed (they've just hit refresh, those dorks)
78
if (Preference::exists('lastfm_user')) { return false; }
80
Preference::insert('lastfm_user','Last.FM Username','','25','string','plugins');
81
Preference::insert('lastfm_md5_pass','Last.FM Password','','25','string','plugins');
82
Preference::insert('lastfm_port','Last.FM Submit Port','','25','string','internal');
83
Preference::insert('lastfm_host','Last.FM Submit Host','','25','string','internal');
84
Preference::insert('lastfm_url','Last.FM Submit URL','','25','string','internal');
85
Preference::insert('lastfm_challenge','Last.FM Submit Challenge','','25','string','internal');
93
* This is a required plugin function. It removes our preferences from
94
* the database returning it to its original form
96
public function uninstall() {
98
Preference::delete('lastfm_md5_pass');
99
Preference::delete('lastfm_user');
100
Preference::delete('lastfm_url');
101
Preference::delete('lastfm_host');
102
Preference::delete('lastfm_port');
103
Preference::delete('lastfm_challenge');
109
* This is a recommended plugin function
111
public function upgrade() {
112
$from_version = Plugin::get_plugin_version($this->name);
113
if ($from_version < 4) {
114
Preference::rename('lastfm_pass', 'lastfm_md5_pass');
121
* This takes care of queueing and then submitting the tracks.
123
public function save_songplay($song) {
125
// Let's pull the last song submitted by this user
126
$previous = Stats::get_last_song($this->user_id);
128
$diff = time() - $previous['date'];
130
// Make sure it wasn't within the last min
132
debug_event($this->name,'Last song played within ' . $diff . ' seconds, not recording stats','3');
136
if ($song->time < 30) {
137
debug_event($this->name,'Song less then 30 seconds not queueing','3');
141
// Make sure there's actually a username and password before we keep going
142
if (!$this->username || !$this->password) {
143
debug_event($this->name,'Username or password missing','3');
147
// Create our scrobbler with everything this time and then queue it
148
$scrobbler = new scrobbler($this->username,$this->password,$this->hostname,$this->port,$this->path,$this->challenge);
150
// Check to see if the scrobbling works
151
if (!$scrobbler->queue_track($song->f_artist_full,$song->f_album_full,$song->title,time(),$song->time,$song->track)) {
152
// Depending on the error we might need to do soemthing here
156
// Go ahead and submit it now
157
if (!$scrobbler->submit_tracks()) {
158
debug_event($this->name,'Error Submit Failed: ' . $scrobbler->error_msg,'3');
159
if ($scrobbler->reset_handshake) {
160
debug_event($this->name,'Re-running Handshake due to error','3');
161
$this->set_handshake($this->user_id);
163
if ($scrobbler->submit_tracks()) {
164
debug_event($this->name,'Submission Successful','5');
171
debug_event($this->name,'Submission Successful','5');
179
* This runs a handshake and properly updates the preferences as needed.
180
* It returns the data as an array so we don't have to requery the db.
181
* This requires a userid so it knows whose crap to update.
183
public function set_handshake($user_id) {
185
$scrobbler = new scrobbler($this->username,$this->password);
186
$data = $scrobbler->handshake();
189
debug_event($this->name,'Handshake Failed: ' . $scrobbler->error_msg,'3');
193
$this->hostname = $data['submit_host'];
194
$this->port = $data['submit_port'];
195
$this->path = $data['submit_url'];
196
$this->challenge = $data['challenge'];
198
// Update the preferences
199
Preference::update('lastfm_port',$user_id,$data['submit_port']);
200
Preference::update('lastfm_host',$user_id,$data['submit_host']);
201
Preference::update('lastfm_url',$user_id,$data['submit_url']);
202
Preference::update('lastfm_challenge',$user_id,$data['challenge']);
210
* This loads up the data we need into this object, this stuff comes
211
* from the preferences.
213
public function load() {
215
$GLOBALS['user']->set_preferences();
216
$data = $GLOBALS['user']->prefs;
218
if (strlen(trim($data['lastfm_user']))) {
219
$this->username = trim($data['lastfm_user']);
222
debug_event($this->name,'No Username, not scrobbling','3');
225
if (strlen(trim($data['lastfm_md5_pass']))) {
226
$this->password = trim($data['lastfm_md5_pass']);
229
debug_event($this->name,'No Password, not scrobbling','3');
233
$this->user_id = $GLOBALS['user']->id;
235
// If we don't have the other stuff try to get it before giving up
236
if (!$data['lastfm_host'] || !$data['lastfm_port'] || !$data['lastfm_url'] || !$data['lastfm_challenge']) {
237
debug_event($this->name,'Running Handshake, missing information','3');
238
if (!$this->set_handshake($this->user_id)) {
239
debug_event($this->name,'Handshake failed, you lose','3');
244
$this->hostname = $data['lastfm_host'];
245
$this->port = $data['lastfm_port'];
246
$this->path = $data['lastfm_url'];
247
$this->challenge = $data['lastfm_challenge'];
25
public $name ='Last.FM';
26
public $description ='Records your played songs to your Last.FM Account';
28
public $version ='000004';
29
public $min_ampache ='360003';
30
public $max_ampache ='999999';
32
// These are internal settings used by this class, run this->load to
44
* This function does nothing...
46
public function __construct() {
54
* This is a required plugin function. It inserts our preferences
57
public function install() {
59
// Check and see if it's already installed (they've just hit refresh, those dorks)
60
if (Preference::exists('lastfm_user')) { return false; }
62
Preference::insert('lastfm_user','Last.FM Username','','25','string','plugins');
63
Preference::insert('lastfm_md5_pass','Last.FM Password','','25','string','plugins');
64
Preference::insert('lastfm_port','Last.FM Submit Port','','25','string','internal');
65
Preference::insert('lastfm_host','Last.FM Submit Host','','25','string','internal');
66
Preference::insert('lastfm_url','Last.FM Submit URL','','25','string','internal');
67
Preference::insert('lastfm_challenge','Last.FM Submit Challenge','','25','string','internal');
75
* This is a required plugin function. It removes our preferences from
76
* the database returning it to its original form
78
public function uninstall() {
80
Preference::delete('lastfm_md5_pass');
81
Preference::delete('lastfm_user');
82
Preference::delete('lastfm_url');
83
Preference::delete('lastfm_host');
84
Preference::delete('lastfm_port');
85
Preference::delete('lastfm_challenge');
91
* This is a recommended plugin function
93
public function upgrade() {
94
$from_version = Plugin::get_plugin_version($this->name);
95
if ($from_version < 4) {
96
Preference::rename('lastfm_pass', 'lastfm_md5_pass');
103
* This takes care of queueing and then submitting the tracks.
105
public function save_songplay($song) {
107
// Let's pull the last song submitted by this user
108
$previous = Stats::get_last_song($this->user_id);
110
$diff = time() - $previous['date'];
112
// Make sure it wasn't within the last min
114
debug_event($this->name,'Last song played within ' . $diff . ' seconds, not recording stats','3');
118
if ($song->time < 30) {
119
debug_event($this->name,'Song less then 30 seconds not queueing','3');
123
// Make sure there's actually a username and password before we keep going
124
if (!$this->username || !$this->password) {
125
debug_event($this->name,'Username or password missing','3');
129
// Create our scrobbler with everything this time and then queue it
130
$scrobbler = new scrobbler($this->username,$this->password,$this->hostname,$this->port,$this->path,$this->challenge);
132
// Check to see if the scrobbling works
133
if (!$scrobbler->queue_track($song->f_artist_full,$song->f_album_full,$song->title,time(),$song->time,$song->track)) {
134
// Depending on the error we might need to do soemthing here
138
// Go ahead and submit it now
139
if (!$scrobbler->submit_tracks()) {
140
debug_event($this->name,'Error Submit Failed: ' . $scrobbler->error_msg,'3');
141
if ($scrobbler->reset_handshake) {
142
debug_event($this->name,'Re-running Handshake due to error','3');
143
$this->set_handshake($this->user_id);
145
if ($scrobbler->submit_tracks()) {
146
debug_event($this->name,'Submission Successful','5');
153
debug_event($this->name,'Submission Successful','5');
161
* This runs a handshake and properly updates the preferences as needed.
162
* It returns the data as an array so we don't have to requery the db.
163
* This requires a userid so it knows whose crap to update.
165
public function set_handshake($user_id) {
167
$scrobbler = new scrobbler($this->username,$this->password);
168
$data = $scrobbler->handshake();
171
debug_event($this->name,'Handshake Failed: ' . $scrobbler->error_msg,'3');
175
$this->hostname = $data['submit_host'];
176
$this->port = $data['submit_port'];
177
$this->path = $data['submit_url'];
178
$this->challenge = $data['challenge'];
180
// Update the preferences
181
Preference::update('lastfm_port',$user_id,$data['submit_port']);
182
Preference::update('lastfm_host',$user_id,$data['submit_host']);
183
Preference::update('lastfm_url',$user_id,$data['submit_url']);
184
Preference::update('lastfm_challenge',$user_id,$data['challenge']);
192
* This loads up the data we need into this object, this stuff comes
193
* from the preferences.
195
public function load() {
197
$GLOBALS['user']->set_preferences();
198
$data = $GLOBALS['user']->prefs;
200
if (strlen(trim($data['lastfm_user']))) {
201
$this->username = trim($data['lastfm_user']);
204
debug_event($this->name,'No Username, not scrobbling','3');
207
if (strlen(trim($data['lastfm_md5_pass']))) {
208
$this->password = trim($data['lastfm_md5_pass']);
211
debug_event($this->name,'No Password, not scrobbling','3');
215
$this->user_id = $GLOBALS['user']->id;
217
// If we don't have the other stuff try to get it before giving up
218
if (!$data['lastfm_host'] || !$data['lastfm_port'] || !$data['lastfm_url'] || !$data['lastfm_challenge']) {
219
debug_event($this->name,'Running Handshake, missing information','3');
220
if (!$this->set_handshake($this->user_id)) {
221
debug_event($this->name,'Handshake failed, you lose','3');
226
$this->hostname = $data['lastfm_host'];
227
$this->port = $data['lastfm_port'];
228
$this->path = $data['lastfm_url'];
229
$this->challenge = $data['lastfm_challenge'];
254
236
} // end AmpacheLastfm