forked from Allikin/SpotifyTools.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
260 lines (236 loc) · 9.57 KB
/
functions.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
<?php
function correctPlaylistFormat($data) {
$correct = false;
$patterns = array( '#http://open.spotify.com/user/#',
'#spotify:user:#');
foreach ($patterns as $pattern) {
if (preg_match($pattern,$data)) {
$correct = true;
}
}
return $correct;
}
function correctTracksFormat($data) {
$correct = false;
$patterns = array( '#http://open.spotify.com/track/#',
'#http://open.spotify.com/local/#',
'#spotify:track:#',
'#spotify:local:#');
// Only checking if the pattern occures somewhere in the input, so pretty bad quality check. :P
foreach ($patterns as $pattern) {
if (preg_match($pattern,$data)) {
$correct = true;
}
}
return $correct;
}
function getPlaylistURI($playlist) {
$playlist = preg_replace('#http://open.spotify.com/#','spotify:',$playlist);
$playlist_uri = preg_replace('#/#',':',$playlist);
return($playlist_uri);
}
function tracksGetSpotifyURIs($tracks) {
$pattern = array( '#http://open.spotify.com/track/#',
'#http://open.spotify.com/local/#');
$substitute = array('spotify:track:',
'spotify:local:');
foreach ($tracks as $i => $track) {
$tracks[$i] = preg_replace($pattern,$substitute,trim($track));
}
return($tracks);
}
function playlistGetSpotifyURIs($playlist_uri) {
$playlist = Array(
'Name' => '',
'Tracks' => Array(
'Names' => Array(),
'Addresses' => Array(),
'Duration' => Array()
)
);
if ($testSocket = @fsockopen("embed.spotify.com", 80, $errno, $errstr, 2)) {
$url = 'https://embed.spotify.com/?uri='.$playlist_uri;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$page = curl_exec($ch);
curl_close($ch);
fclose($testSocket);
// The regex below genereates a multidimentional array of the info I need. Im sure the regex can be much more elegant, but it does the job.
preg_match_all('/data-track=\"(.*?)\"(.*?)-ms="(.*?)"(.*?)track-title(.*?)\.(.*?)\"(.*?)artist (.*?)rel="(.*?)\"/s',$page,$tracks);
// I need the indexes 1 (data-track=URI), 3 (ms=duration), 6 (track-title) and 9 (artist)
foreach ($tracks[1] as $i => $track) {
$playlist['Tracks']['Addresses'][$i] = 'spotify:track:'.$track;
$playlist['Tracks']['Duration'][$i] = $tracks[3][$i];
$playlist['Tracks']['Names'][$i] = $tracks[6][$i]." by ".$tracks[9][$i]; // will prolly rewrite to keep these seperate
}
preg_match('/title>(.*?) by/',$page,$name);
$playlist['Name'] = $name[1];
// Oops, embed.spotify.com is down
} else {
$playlist['Tracks']['Names'][0] = ' <br/><i>Unfortunately the playlist option is not possible at the moment (Spotify site down), please use the bottom option or try again later.</i>';
$playlist['Tracks']['Addresses'][0] = '999';
$playlist['Tracks']['Duration'][0] = 9999;
}
return($playlist);
}
function removeDupes($tracks) {
foreach ($tracks['Addresses'] as $i => $uri) {
$match_index = array_search($uri,$tracks['Addresses']);
// If the current track address matches with other addresses than the current it has to be a dupe, unset it
if ($match_index <> $i) {
unset($tracks['Addresses'][$match_index]);
if (isset($tracks['Names'][$match_index])) { // will not be submitted with tracklist, so need to check if it's set
unset($tracks['Names'][$match_index]);
}
if (isset($tracks['Duration'][$match_index])) { // will not be submitted with tracklist, so need to check if it's set
unset($tracks['Duration'][$match_index]);
}
}
}
return($tracks);
}
function findDupes($tracks) {
$j = 0; // index counter for our dupes array
$last_uri = ""; // for URI comparison
// If duration is set, we know we're working with a playlist
if (isset($tracks['Duration']) && ($tracks['Duration'][0] > 0)) {
$last_dur = 0; // for comparison when we're checking
$last_artist = ""; // for dupes that don't share URI
// The list is sorted by name, then duration...
array_multisort($tracks['Names'], SORT_ASC, SORT_STRING,
$tracks['Duration'], SORT_ASC, SORT_NUMERIC,
$tracks['Addresses'], SORT_ASC, SORT_STRING);
foreach ($tracks['Duration'] as $i => $dur) {
$trackname_and_artist = explode(' by ',$tracks['Names'][$i]); // seperate track name and artist (as they're not kept separated atm)
$artist = $trackname_and_artist[1]; // get the artist name
// ...and if both duration and artist is the same as for the last track, there's a good chance this is a dupe
// If the URI is the same as the last one, it MUST be a dupe
if ((($dur > 0) && ($dur == $last_dur) && ($artist == $last_artist)) || ($tracks['Addresses'][$i] == $last_uri)) {
$dupes['Names'][$j] = $tracks['Names'][$i];
$dupes['Addresses'][$j] = $tracks['Addresses'][$i];
// If the URIs don't match it's just a possible dupe, let's keep them both and mark them with pretty stars
if ($tracks['Addresses'][$i] <> $last_uri) {
$dupes['Names'][$j] = '*'.$tracks['Names'][$i];
$dupes['Names'][$j+1] = '**'.$tracks['Names'][$i-1];
$dupes['Addresses'][$j+1] = $tracks['Addresses'][$i-1];
$j++;
}
$j++;
}
$last_dur = $dur; //
$last_artist = $artist; // keep for comparison
$last_uri = $tracks['Addresses'][$i]; //
}
} else {
// If it's just a tracks list we have no names/artist and duration to compare with, so we're stuck with just matching the URI
foreach ($tracks['Addresses'] as $i => $uri) {
$match_index = array_search($uri,$tracks['Addresses']);
// If the current track address/URI matches with other addresses/URIs than the current it has to be a dupe
if ($match_index <> $i) {
$dupes['Addresses'][$j] = $tracks['Addresses'][$i];
$j++;
}
}
}
unset($i,$dur,$last_dur,$last_uri,$last_artist);
if (isset($dupes)) {
return($dupes);
} else {
return(false);
}
}
// This function is to handle local tracks. We can't look it up against the same way as with an ordinary URI,
// but we already have what we need in the address:
function getLocalTrackName($local_track) {
// this is replaced by this
$replace = Array( '#spotify:local:#', // remove this
'#/#', // :
'#\+#', // space
'#\%26#', // &
'#\%27#', // '
'#\%28#', // (
'#\%29#', // )
'#\%2C#', // ,
'#\%3A#', // ; (is really ':' but replaced with ';' for the sake of exploding correctly)
'#\%5B#', // [
'#\%5D#'); // ]
$substitutes = Array('',':',' ','&','\'','(',')',',',';','[',']');
$local_track = preg_replace($replace,$substitutes,$local_track);
$track_array = explode(':',$local_track);
// If there were any colons (':') in the track name, they would have been replaced with a ';' - we need to fix that
return(preg_replace('#;#',':',$track_array[2])." by ".preg_replace('#;#',':',$track_array[0]));
}
function tracksGetTrackNames($dupes) {
$i = 0;
if ($testSocket = @fsockopen("ws.spotify.com", 80, $errno, $errstr, 2)) {
foreach ($dupes['Addresses'] as $i => $dupe) {
if(preg_match('/:local:/', $dupe)) {
$dupes['Names'][$i] = getLocalTrackName($dupe);
} else {
$url = trim("http://ws.spotify.com/lookup/1/?uri=".$dupe);
$content = file_get_contents($url);
if ($content) {
$doc = new SimpleXmlElement($content);
$dupes['Names'][$i] = $doc->name." by ".$doc->artist->name;
} else {
$dupes['Names'][$i] = "Error looking up in the Spotify API";
}
}
}
fclose($testSocket);
array_multisort($dupes['Names'], SORT_ASC, SORT_STRING,
$dupes['Addresses'], SORT_ASC, SORT_STRING);
} else {
$dupes['Names'][0] = ' <br/><i>Unfortunately it\'s not possible to lookup tracknames at this time (Spotify site down), please try again later.</i>';
$dupes['Addresses'][0] = '999';
}
unset($i);
return($dupes);
}
function playlistGetTrackNames($dupes,$playlist) {
$i = 0;
foreach ($dupes as $i => $dupe) {
$match_index = array_search($dupe,$playlist['Addresses']);
if (!($match_index === false)) {
$dupe_tracks['Names'][$i] = $playlist['Names'][$match_index];
$dupe_tracks['Addresses'][$i] = $playlist['Addresses'][$match_index];
$dupe_tracks['Duration'][$i] = $playlist['Duration'][$match_index];
}
}
return($dupe_tracks);
}
function inPlaylists($track_uri,$playlists) {
$in_lists = Array();
foreach ($playlists as $i => $playlist) {
if (isset($playlist['Name']) && ($playlist['Tracks']['Addresses'][0] > "")) { // not sure why address would contain "", check prolly redundant
if (!(array_search($track_uri,$playlists[$i]['Tracks']['Addresses']) === false)) {
$in_lists[] = $playlists[$i]['Name'];
}
} elseif (isset($playlist['Addresses']) && ($playlist['Addresses'][0] > "")) { // not sure why address would contain "", check prolly redundant
if (!(array_search($track_uri,$playlists[$i]['Addresses']) === false)) {
$in_lists[] = "Track list ".($i+1);
}
}
}
return($in_lists);
}
// Not in use.. yet.
function findAntidupes($dupes, $tracks) {
$match_indexes = Array();
foreach ($dupes as $i => $dupe) {
$match_indexes = array_search($dupe,$tracks['Addresses']);
foreach ($match_indexes as $match_index) {
if (!($match_index === false)) { // Making sure it's actually not false as opposed to 0.
unset($tracks['Addresses'][$match_index]);
unset($tracks['Names'][$match_index]);
if (isset($tracks['Duration'])) {
unset($tracks['Duration'][$match_index]);
}
}
}
}
return($tracks);
}
?>