forked from jameshartig/GroovesharkAPI-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsSearch.php
404 lines (360 loc) · 15.1 KB
/
gsSearch.php
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
* @author James Hartig
* @copyright 2011
*/
class gsSearch extends gsAPI{
const MAX_PARALLEL_SEARCHES = 10;
private $parent;
private $id = null;
private $artist = null;
private $album = null;
private $title = null;
private $changed = true;
private $exact;
private $results = null;
private $called = "";
function gsSearch(&$parent=null){
if (!$parent) {
$this->parent = gsAPI::getInstance();
} else {
$this->parent = $parent;
}
}
public function setArtist($artist){
$this->changed = true;
$this->artist = $artist;
}
public function setTitle($title){
$this->changed = true;
$this->title = $title;
}
public function setAlbum($album){
$this->changed = true;
$this->album = $album;
}
public function setResults($results) {
$this->results = $results;
$this->changed = false;
}
public function clear(){ //clears all the above search params
$this->album = null;
$this->artist = null;
$this->title = null;
$this->listing = null;
$this->results = null;
$this->changed = true;
}
//normalizes search and gets an ID
public function getUniqueID() {
if (!$this->id || $this->changed) {
$query_str = "";
if (!empty($this->title)){
$query_str .= " s:".$this->title;
}
if (!empty($this->artist)){
$query_str .= " a:".$this->artist;
}
if (!empty($this->album)){
$query_str .= " l:".$this->album;
}
$query_str =
preg_replace("/([\!\?\.\,])[\!\?\.\,]+/", "{1}", //remove multiple !?,. characters
str_replace(array("{", "}", "<", ">", "@", "$", "%", "~", "#", "*", "|", "/", "_", ";", "^"), "",//remove stupid characters
preg_replace("/[\s]{2,}/", " ", //replace multiple spaces
strtolower( //lowercase
trim($query_str) //trim duh
)
)
)
);
$this->id = md5($query_str);
}
return $this->id;
}
/*public function singleSongSearch() {
//todo: this
if (count($songs['songs'])==1 && $page==1){
return $songs['songs'][0];
}
if (!$this->exact) {
foreach ($songs['songs'] AS $song){
//check for exact match
if (!empty($this->title) && !empty($this->artist) && !empty($this->album)) {
if ((strtolower($this->title) === strtolower($song['SongName']) || ((int)$this->title && (int)$this->title === (int)$song['SongID']))
&& (strtolower($this->album) === strtolower($song['AlbumName']) || ((int)$this->album && (int)$this->album === (int)$song['AlbumID']))
&& (strtolower($this->artist) === strtolower($song['ArtistName']) || ((int)$this->artist && (int)$this->artist === (int)$song['ArtistID']))) {
$this->exact = $song;
break;
}
} elseif (!empty($this->title) && !empty($this->artist)) {
if ((strtolower($this->title) === strtolower($song['SongName']) || ((int)$this->title && (int)$this->title === (int)$song['SongID']))
&& (strtolower($this->artist) === strtolower($song['ArtistName']) || ((int)$this->artist && (int)$this->artist === (int)$song['ArtistID']))) {
$this->exact = $song;
break;
}
} elseif (!empty($this->title) && !empty($this->album)) {
if ((strtolower($this->title) === strtolower($song['SongName']) || ((int)$this->title && (int)$this->title === (int)$song['SongID']))
&& (strtolower($this->album) === strtolower($song['AlbumName']) || ((int)$this->album && (int)$this->album === (int)$song['AlbumID']))) {
$this->exact = $song;
break;
}
} elseif (!empty($this->artist) && !empty($this->album)) {
if ((strtolower($this->album) === strtolower($song['AlbumName']) || ((int)$this->album && (int)$this->album === (int)$song['AlbumID']))
&& (strtolower($this->artist) === strtolower($song['ArtistName']) || ((int)$this->artist && (int)$this->artist === (int)$song['ArtistID']))) {
$this->exact = $song;
break;
}
} elseif (!empty($this->title)) {
if (strtolower($this->title) === strtolower($song['SongName']) || ((int)$this->title && (int)$this->title === (int)$song['SongID'])) {
$this->exact = $song;
break;
}
} elseif (!empty($this->artist)) {
if (strtolower($this->artist) === strtolower($song['ArtistName']) || ((int)$this->artist && (int)$this->artist === (int)$song['ArtistID'])) {
$this->exact = $song;
break;
}
} elseif (!empty($this->album)) {
if (strtolower($this->album) === strtolower($song['AlbumName']) || ((int)$this->album && (int)$this->album === (int)$song['AlbumID'])) {
$this->exact = $song;
break;
}
}
}
}
}*/
private static function performSearch($method, $query, $country=null, $max=null){
$results = array();
for($page=1;$page<=2;$page++){
switch($method) {
case "getSongSearchResults":
$searchResults = parent::getSongSearchResults($query, $country, ($max ? $max : 91), ($page-1)*90);
break;
case "getArtistSearchResults":
case "getAlbumSearchResults":
$searchResults = call_user_func(array(parent::getInstance(), $method), $query, ($max ? $max : 91), ($page-1)*90);
break;
default:
return false;
break;
}
if ($searchResults === false || count($searchResults)<1) {
break;
}
if (count($searchResults) > 90 && (!$max || $max > 100)){
array_pop($searchResults); //we need to check if there are more results
}
self::appendResults($searchResults, $results);
if (count($searchResults) < 90 || ($max && count($results) > $max)) {
break;
}
}
if ($max) {
return array_slice($results, 0, $max, true);
} else {
return $results;
}
}
public function songSearchResults($max = null)
{
//build request
$query_str = $this->buildQuery("song");
if (empty($query_str)) {
return array();
}
if ($this->called != __METHOD__) {
$this->changed = true;
}
//we always get at least 50
if ($this->changed || (count($this->results) >= 50 && $max > count($this->results))) {
$this->results = null;
$this->changed = false;
$this->results = self::performSearch("getSongSearchResults", $query_str, $this->parent->country, max($max, 50));
if ($this->results === false) {
return false;
}
$this->called = __METHOD__;
return array_slice($this->results, 0, $max, true);
} else {
if ($max) {
return array_slice($this->results, 0, $max, true);
} else {
return $this->results;
}
}
}
public function artistSearchResults()
{
//build request
$query_str = $this->buildQuery("artist");
if (empty($query_str)) {
return array();
}
if ($this->called != __METHOD__) {
$this->changed = true;
}
//we always get at least 50
if ($this->changed || (count($this->results) >= 50 && $max > count($this->results))) {
$this->results = null;
$this->changed = false;
$this->results = self::performSearch("getArtistSearchResults", $query_str, max($max, 50));
if ($this->results === false) {
return false;
}
$this->called = __METHOD__;
return array_slice($this->results, 0, $max, true);
} else {
if ($max) {
return array_slice($this->results, 0, $max, true);
} else {
return $this->results;
}
}
}
public function albumSearchResults()
{
//build request
$query_str = $this->buildQuery("album");
if (empty($query_str)) {
return array();
}
if ($this->called != __METHOD__) {
$this->changed = true;
}
//we always get at least 50
if ($this->changed || (count($this->results) >= 50 && $max > count($this->results))) {
$this->results = null;
$this->changed = false;
$this->results = self::performSearch("getAlbumSearchResults", $query_str, max($max, 50));
if ($this->results === false) {
return false;
}
$this->called = __METHOD__;
return array_slice($this->results, 0, $max, true);
} else {
if ($max) {
return array_slice($this->results, 0, $max, true);
} else {
return $this->results;
}
}
}
private function buildQuery($type) {
$query_str = "";
if (!empty($this->title) && (!empty($this->artist) || !empty($this->album) || $type == "artist" || $type == "album")){
$query_str .= " song:".$this->title;
if (!empty($this->artist)){
$query_str .= " artist:".$this->artist;
}
if (!empty($this->album)){
$query_str .= " album:".$this->album;
}
} else if (!empty($this->artist)) {
if (!empty($this->album)){
$query_str .= " artist:".$this->artist;
$query_str .= " album:".$this->album;
} else {
$query_str .= ($type == "song" || $type == "album" ? " artist:" : "").$this->artist;
}
} else {
$query_str .= ($this->title ? ($type == "artist" || $type == "album" ? " song:" : "").$this->title :
($this->album ? ($type == "song" || $type == "artist" ? " album:" : "").$this->album : ""));
}
$query_str = trim($query_str);
return $query_str;
}
private static function appendResults($results, &$toResults){
if (!is_array($toResults)) {
$toResults = $results;
} else {
$start = count($toResults);
$i = 0;
foreach($results AS $v) {
$toResults[($i++)+$start] = $v;
}
}
}
/*
//todo build support for >255 chars
private static function calculateScore($search,$result,$rank=1,$lesssub=false){
$len = strlen($search[0]);
$words = count($search[1]);
if (isset($result)){
$dist = (($dist = levenshtein(strtolower($result),strtolower($search[0])))==0 ? 1 : ($dist>1?(($len*1.3-$dist)/$len):0));
if ($words > 1){
foreach($search[1] AS $word){
if (strlen($word) > 2){
$imp = (($len/$len)+(1/$words)); //importance of word
if(strpos($result,$word)!==false){
if (!$lesssub)
$dist += $imp*.1;
else
$dist += $imp*.2;
}else{
if (!$lesssub)
$dist -= $imp*.3;
else
$dist -= $imp*.15;
}
}
}
}
return $dist*$rank;
}
return 0;
}
public static function parallelCalls($urls) {
// Create get requests for each URL
$mh = curl_multi_init();
foreach($urls as $i => $url)
{
if ($url) {
$ch[$i] = curl_init($url);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch[$i], CURLOPT_CONNECTTIMEOUT, 6);
curl_setopt($ch[$i], CURLOPT_TIMEOUT, 10);
curl_multi_add_handle($mh, $ch[$i]);
}
}
// Start performing the request
do {
$execReturnValue = curl_multi_exec($mh, $runningHandles);
} while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
// Loop and continue processing the request
while ($runningHandles && $execReturnValue == CURLM_OK) {
// Wait forever for network
$numberReady = curl_multi_select($mh);
if ($numberReady != -1) {
// Pull in any new data, or at least handle timeouts
do {
$execReturnValue = curl_multi_exec($mh, $runningHandles);
} while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
}
}
// Check for any errors
if ($execReturnValue != CURLM_OK) {
error_log("Curl multi read error $execReturnValue\n", E_USER_WARNING);
}
// Extract the content
foreach($urls as $i => $url) {
// Check for errors
if ($url && $ch[$i]) {
$curlError = curl_error($ch[$i]);
if($curlError == "") {
$res[$i] = curl_multi_getcontent($ch[$i]);
} else {
$res[$i] = null;
}
// Remove and close the handle
curl_multi_remove_handle($mh, $ch[$i]);
curl_close($ch[$i]);
}
}
// Clean up the curl_multi handle
curl_multi_close($mh);
return $res;
}
*/
}
?>