-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsearch.php
More file actions
98 lines (89 loc) · 3.56 KB
/
Copy pathsearch.php
File metadata and controls
98 lines (89 loc) · 3.56 KB
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
<?php
include dirname(__FILE__) . '/autoload.php';
$query = $argv[2];
$show_images = (isset($argv[1]) && trim(strtolower($argv[1])) == 'yes') ? true : false;
$tmp = explode(' ', $query);
$type = $tmp[0];
$query = trim(str_replace($type, '', $query));
$type = strtolower($type);
$thumbs_path = 'artwork';
$x = 1;
$key = $type . 's';
$max = ($show_images === true) ? 5 : 15;
if (($type != 'artist' && $type != 'album' && $type != 'track') || strlen($query) < 3) {
exit;
}
$results = array();
$json = Tools::fetch('http://ws.spotify.com/search/1/' . $type . '.json?q=' . urlencode($query));
$error = (empty($json)) ? true : false;
$json = json_decode($json);
$error = (Tools::getJsonError() !== false) ? true : $error;
if ($error === true) {
array_push($results, array(
'uid' => $type,
'arg' => null,
'title' => 'Error Found',
'subtitle' => 'Could not find or parse results',
'icon' => 'icon.png',
'autocomplete' => htmlentities($query, ENT_QUOTES, 'UTF-8')
));
}
elseif ($json->info->num_results < 1 || !isset($json->info->num_results) || !isset($json->{$key})) {
array_push($results, array(
'uid' => $type,
'arg' => null,
'title' => 'Came up empty',
'subtitle' => 'No results found for ' . htmlentities($query, ENT_QUOTES, 'UTF-8'),
'icon' => 'icon.png',
'autocomplete' => htmlentities($query, ENT_QUOTES, 'UTF-8')
));
}
else {
foreach ($json->{$key} as $k => $obj) {
if ($x <= $max) {
$name = (isset($obj->name)) ? htmlentities($obj->name, ENT_QUOTES, 'UTF-8') : null;
$album = (isset($obj->album->name)) ? htmlentities($obj->album->name, ENT_QUOTES, 'UTF-8') : null;
$artist_name = (isset($obj->artists[0]->name)) ? htmlentities($obj->artists[0]->name, ENT_QUOTES, 'UTF-8') : null;
if ($type == 'artist') {
$subtitle = 'Artist';
$autocomplete = $name;
}
elseif ($type == 'album') {
$subtitle = $artist_name;
$autocomplete = $artist_name . ' ' . $name;
}
else {
$subtitle = $artist_name . ' - ' . $album;
$autocomplete = $artist_name . ' ' . $album;
}
if ($show_images === true) {
$hrefs = explode(':', $obj->href);
$track_id = $hrefs[2];
$thumb_path = $thumbs_path . '/' . $track_id . '.png';
if (! file_exists($thumb_path)) {
$artwork = Tools::getTrackArtwork($type, $track_id);
if (! empty($artwork)) {
shell_exec('curl -s ' . $artwork . ' -o ' . $thumb_path);
}
}
$icon = (! file_exists($thumb_path)) ? 'icon.png' : $thumb_path;
}
else {
$icon = 'icon.png';
}
array_push($results, array(
'uid' => $type,
'arg' => $obj->href,
'title' => $name,
'subtitle' => $subtitle,
'icon' => $icon,
'autocomplete' => $autocomplete
));
$x += 1;
}
else {
break;
}
}
}
print Tools::arrayToXML($results);