1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
diff -Naur --exclude=.svn ampache.ref/lib/class/audioscrobbler.class.php ampache/lib/class/audioscrobbler.class.php
--- ampache.ref/lib/class/audioscrobbler.class.php 1969-12-31 16:00:00.000000000 -0800
+++ ampache/lib/class/audioscrobbler.class.php 2006-12-06 01:42:27.000000000 -0800
@@ -0,0 +1,210 @@
+<?php
+/*
+
+ Copyright (c) 2001 - 2006 Ampache.org
+ All rights reserved.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License v2
+ as published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+*/
+
+class scrobbler {
+
+ var $error_msg;
+ var $username;
+ var $password;
+ var $challenge;
+ var $submit_host;
+ var $submit_port;
+ var $submit_url;
+ var $queued_tracks;
+
+ /**
+ * Constructor
+ * This is the constructer it takes a username and password
+ */
+ function scrobbler($username, $password) {
+
+ $this->error_msg = '';
+ $this->username = trim($username);
+ $this->password = trim($password);
+ $this->challenge = '';
+ $this->queued_tracks = array();
+
+ } // scrobbler
+
+ /**
+ * get_error_msg
+ */
+ function get_error_msg() {
+
+ return $this->error_msg;
+
+ } // get_error_msg
+
+ /**
+ * get_queue_count
+ function get_queue_count() {
+
+ return count($this->queued_tracks);
+
+ } // get_queue_count
+
+ /**
+ * handshake
+ * This does a handshake with the audioscrobber server
+ */
+ function handshake() {
+
+ $as_socket = @fsockopen('post.audioscrobbler.com', 80, $errno, $errstr, 10);
+ if(!$as_socket) {
+ $this->error_msg = $errstr;
+ return FALSE;
+ }
+
+ $username = rawurlencode($this->username);
+
+ $get_string = "GET /?hs=true&p=1.1&c=m3a&v=0.1&u=$username HTTP/1.1\r\n";
+
+ fwrite($as_socket, $get_string);
+ fwrite($as_socket, "Host: post.audioscrobbler.com\r\n");
+ fwrite($as_socket, "Accept: */*\r\n\r\n");
+
+ $buffer = '';
+ while(!feof($as_socket)) {
+ $buffer .= fread($as_socket, 8192);
+ }
+ fclose($as_socket);
+ $split_response = preg_split("/\r\n\r\n/", $buffer);
+ if(!isset($split_response[1])) {
+ $this->error_msg = 'Did not receive a valid response';
+ return FALSE;
+ }
+ $response = explode("\n", $split_response[1]);
+ if(substr($response[0], 0, 6) == 'FAILED') {
+ $this->error_msg = substr($response[0], 7);
+ return FALSE;
+ }
+ if(substr($response[0], 0, 7) == 'BADUSER') {
+ $this->error_msg = 'Invalid Username';
+ return FALSE;
+ }
+ if(substr($response[0], 0, 6) == 'UPDATE') {
+ $this->error_msg = 'You need to update your client: '.substr($response[0], 7);
+ return FALSE;
+ }
+
+ if(preg_match('/http:\/\/(.*):(\d+)(.*)/', $response[3], $matches)) {
+ $this->submit_host = $matches[1];
+ $this->submit_port = $matches[2];
+ $this->submit_url = $matches[3];
+ } else {
+ $this->error_msg = 'Invalid POST URL returned, unable to continue';
+ return FALSE;
+ }
+
+ $this->challenge = $response[2];
+ return true;
+
+ } // handshake
+
+ function queue_track($artist, $album, $track, $timestamp, $length) {
+ $date = gmdate('Y-m-d H:i:s', $timestamp);
+ $mydate = date('Y-m-d H:i:s T', $timestamp);
+
+ if($length < 30) {
+ //printf("Skipping: %-25.25s %-25.25s %-25.25s (%-4.4d secs), too short\n", $artist, $album, $track, $length);
+ return FALSE;
+ } else {
+ //printf("Queueing: %-25.25s %-25.25s %-25.25s (%-4.4d secs)\n", $artist, $album, $track, $length);
+ //printf(" %23.23s (%23.23s)\n", $date." UTC", $mydate);
+ }
+
+ $newtrack = array();
+ $newtrack['artist'] = $artist;
+ $newtrack['album'] = $album;
+ $newtrack['track'] = $track;
+ $newtrack['length'] = $length;
+ $newtrack['time'] = $date;
+
+ $this->queued_tracks[$timestamp] = $newtrack;
+ return TRUE;
+ }
+
+ function submit_tracks() {
+ if(count($this->queued_tracks) == 0) {
+ $this->error_msg = "No tracks to submit\n";
+ return FALSE;
+ }
+
+ ksort($this->queued_tracks); //sort array by timestamp
+
+ $query_str = 'u='.rawurlencode($this->username).'&s='.rawurlencode(md5( md5($this->password).$this->challenge)).'&';
+ $i = 0;
+ foreach($this->queued_tracks as $track) {
+ $query_str .= "a[$i]=".rawurlencode($track['artist'])."&t[$i]=".rawurlencode($track['track'])."&b[$i]=".rawurlencode($track['album'])."&";
+ $query_str .= "m[$i]=&l[$i]=".rawurlencode($track['length'])."&i[$i]=".rawurlencode($track['time'])."&";
+ $i++;
+ }
+ $as_socket = @fsockopen($this->submit_host, $this->submit_port, $errno, $errstr, 10);
+ if(!$as_socket) {
+ $this->error_msg = $errstr;
+ return FALSE;
+ }
+
+ $action = "POST ".$this->submit_url." HTTP/1.0\r\n";
+ fwrite($as_socket, $action);
+ fwrite($as_socket, "Host: ".$this->submit_host."\r\n");
+ fwrite($as_socket, "Accept: */*\r\n");
+ fwrite($as_socket, "Content-type: application/x-www-form-urlencoded\r\n");
+ fwrite($as_socket, "Content-length: ".strlen($query_str)."\r\n\r\n");
+
+ fwrite($as_socket, $query_str."\r\n\r\n");
+
+ $buffer = '';
+ while(!feof($as_socket)) {
+ $buffer .= fread($as_socket, 8192);
+ }
+ fclose($as_socket);
+
+ $split_response = preg_split("/\r\n\r\n/", $buffer);
+ if(!isset($split_response[1])) {
+ $this->error_msg = 'Did not receive a valid response';
+ return FALSE;
+ }
+ $response = explode("\n", $split_response[1]);
+ if(!isset($response[0])) {
+ $this->error_msg = 'Unknown error submitting tracks'.
+ "\nDebug output:\n".$buffer;
+ return FALSE;
+ }
+ if(substr($response[0], 0, 6) == 'FAILED') {
+ $this->error_msg = $response[0];
+ return FALSE;
+ }
+ if(substr($response[0], 0, 7) == 'BADAUTH') {
+ $this->error_msg = 'Invalid username/password';
+ return FALSE;
+ }
+ if(substr($response[0], 0, 2) != 'OK') {
+ $this->error_msg = 'Unknown error submitting tracks'.
+ "\nDebug output:\n".$buffer;
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
+}
+?>
diff -Naur --exclude=.svn ampache.ref/lib/class/user.class.php ampache/lib/class/user.class.php
--- ampache.ref/lib/class/user.class.php 2006-12-08 12:41:00.000000000 -0800
+++ ampache/lib/class/user.class.php 2006-12-06 14:28:55.000000000 -0800
@@ -433,7 +433,7 @@
$song_info = new Song($song_id);
//FIXME:: User uid reference
$user = $this->uid;
-
+
if (!$song_info->file) { return false; }
$stats = new Stats();
@@ -442,6 +442,42 @@
$stats->insert('artist',$song_info->artist,$user);
$stats->insert('genre',$song_info->genre,$user);
+ /**
+ * Record this play to LastFM
+ * because it lags like the dickens try twice on everything
+ */
+ if ($this->prefs['lastfm_user'] AND $this->prefs['lastfm_pass']) {
+ $song_info->format_song();
+ $lastfm = new scrobbler($this->prefs['lastfm_user'],$this->prefs['lastfm_pass']);
+ /* Attempt handshake */
+ $handshake = $lastfm->handshake();
+
+ /* We failed, try again */
+ if (!$handshake) { sleep(1); $handshake = $lastfm->handshake(); }
+
+
+ if ($handshake) {
+ if (!$lastfm->queue_track($song_info->f_artist_full,$song_info->f_album_full,$song_info->title,time(),$song_info->time)) {
+ debug_event('LastFM','Error: Queue Failed: ' . $lastfm->error_msg,'3');
+ }
+
+ $submit = $lastfm->submit_tracks();
+
+ /* Try again if it fails */
+ if (!$submit) { sleep(1); $submit = $lastfm->submit_tracks(); }
+
+ if (!$submit) {
+ debug_event('LastFM','Error Submit Failed: ' . $lastfm->error_msg,'3');
+ }
+ } // if handshake
+ else {
+ debug_event('LastFM','Error: Handshake failed with LastFM: ' . $lastfm->error_msg,'3');
+ }
+ } // record to LastFM
+ else {
+ debug_event('plugins','Error: No Prefs','3');
+ }
+
} // update_stats
/**
diff -Naur --exclude=.svn ampache.ref/lib/init.php ampache/lib/init.php
--- ampache.ref/lib/init.php 2006-12-08 12:41:00.000000000 -0800
+++ ampache/lib/init.php 2006-12-06 12:27:23.000000000 -0800
@@ -190,6 +190,8 @@
require_once(conf('prefix') . '/lib/class/error.class.php');
require_once(conf('prefix') . '/lib/class/genre.class.php');
require_once(conf('prefix') . '/lib/class/flag.class.php');
+require_once(conf('prefix') . '/lib/class/audioscrobbler.class.php');
+
/* Set a new Error Handler */
$old_error_handler = set_error_handler("ampache_error_handler");
|