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
|
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
/*
* Recommendation Class
*
* PHP version 5
*
* LICENSE: GNU General Public License, version 2 (GPLv2)
* Copyright (c) 2001 - 2011 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.
*
* @category Recommendation
* @package Ampache
* @author Karl Vollmer <vollmer@ampache.org>
* @copyright 2001 - 2011 Ampache.org
* @license http://opensource.org/licenses/gpl-2.0 GPLv2
* @version PHP 5.2
* @link http://www.ampache.org/
* @since File available since Release 1.0
*/
/**
* Recommendation Class
*
* Description here...
*
* @category Recommendation
* @package Ampache
* @author Karl Vollmer <vollmer@ampache.org>
* @copyright 2001 - 2011 Ampache.org
* @license http://opensource.org/licenses/gpl-2.0 GPLv2
* @version Release: 3.6
* @link http://www.ampache.org/
* @since Class available since Release 1.0
*/
class Recommendation {
/**
* Constructor
* Not on my watch, boyo.
*/
private function __construct() {
return false;
} //constructor
/**
* get_lastfm_results
* Runs a last.fm query and returns the parsed results
*/
private static function get_lastfm_results($method, $query) {
$api_key = Config::get('lastfm_api_key');
$api_base = "http://ws.audioscrobbler.com/2.0/?method=";
$url = $api_base . $method . '&api_key=' . $api_key . '&' . $query;
debug_event('Recommendation', 'search url : ' . $url, 5);
$snoopy = new Snoopy();
if(Config::get('proxy_host') AND Config::get('proxy_port')) {
$snoopy->proxy_user = Config::get('proxy_host');
$snoopy->proxy_port = Config::get('proxy_port');
$snoopy->proxy_user = Config::get('proxy_user');
$snoopy->proxy_pass = Config::get('proxy_pass');
}
$snoopy->fetch($url);
$content = $snoopy->results;
return simplexml_load_string($content);
} // get_lastfm_results
/**
* get_songs_like
* Returns a list of similar songs
*/
public static function get_songs_like($song_id, $limit = 5, $local_only = true) {
$song = new Song($song_id);
if (isset($song->mbid)) {
$query = 'mbid=' . rawurlencode($song->mbid);
}
else {
$query = 'track=' . rawurlencode($song->title);
}
if ($limit && !$local_only) {
$query .= "&limit=$limit";
}
$xml = self::get_lastfm_results('track.getsimilar', $query);
foreach ($xml->similartracks->children() as $child) {
$name = $child->name;
$local_id = null;
$artist_name = $child->artist->name;
$s_artist_name = Catalog::trim_prefix($artist_name);
$s_artist_name = Dba::escape($s_artist_name['string']);
$sql = "SELECT `song`.`id` FROM `song` " .
"LEFT JOIN `artist` ON " .
"`song`.`artist`=`artist`.`id` WHERE " .
"`song`.`title`='" . Dba::escape($name) .
"' AND `artist`.`name`='$s_artist_name'";
$db_result = Dba::read($sql);
if ($result = Dba::fetch_assoc($db_result)) {
$local_id = $result['id'];
}
if (is_null($local_id)) {
debug_event('Recommendation', "$name did not match any local song", 5);
if (! $local_only) {
$results[] = array(
'id' => null,
'title' => $name,
'artist' => $artist_name
);
}
}
else {
debug_event('Recommendation', "$name matched local song $local_id", 5);
$results[] = array(
'id' => $local_id,
'title' => $name
);
}
if ($limit && count($results) >= $limit) {
break;
}
}
if (isset($results)) {
return $results;
}
return false;
}
/**
* get_artists_like
* Returns a list of similar artists
*/
public static function get_artists_like($artist_id, $limit = 5, $local_only = true) {
$artist = new Artist($artist_id);
$query = 'artist=' . rawurlencode($artist->name);
if ($limit && !$local_only) {
$query .= "&limit=$limit";
}
$xml = self::get_lastfm_results('artist.getsimilar', $query);
foreach ($xml->similarartists->children() as $child) {
$name = $child->name;
$local_id = null;
// First we check by MBID
if ((string)$child->mbid) {
$mbid = Dba::escape($child->mbid);
$sql = "SELECT `id` FROM `artist` WHERE `mbid`='$mbid'";
$db_result = Dba::read($sql);
if ($result = Dba::fetch_assoc($db_result)) {
$local_id = $result['id'];
}
}
// Then we fall back to the less likely to work exact
// name match
if (is_null($local_id)) {
$searchname = Catalog::trim_prefix($name);
$searchname = Dba::escape($searchname['string']);
$sql = "SELECT `id` FROM `artist` WHERE `name`='$searchname'";
$db_result = Dba::read($sql);
if ($result = Dba::fetch_assoc($db_result)) {
$local_id = $result['id'];
}
}
// Then we give up
if (is_null($local_id)) {
debug_event('Recommendation', "$name did not match any local artist", 5);
if (! $local_only) {
$results[] = array(
'id' => null,
'name' => $name
);
}
}
else {
debug_event('Recommendation', "$name matched local artist " . $local_id, 5);
$results[] = array(
'id' => $local_id,
'name' => $name
);
}
// Don't do more work than we have to
if ($limit && count($results) >= $limit) {
break;
}
}
if (isset($results)) {
return $results;
}
return false;
} // get_artists_like
} // end of recommendation class
?>
|