Advertisement
TonyAR

MPD playlist generator

Feb 15th, 2022
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. // get the length of the array between file: and Id: ($mpdstatus) by getting the key for Id:
  2. // add 1 to the key as it begins at zero
  3. // then chunk the array using the value determined above
  4. // for each song, search the returned array for the fields Title: and Artist: (this is done by the array_search_partial function)
  5. // and this will return the keys
  6. // using the keys, then get the required fields ($songArtist = $song[$artist_key];)
  7. // this returns the full string (Title: Burnin’ Sky) so anything before the colon is stripped using the stripColon function
  8. //
  9.  
  10. function makePlaylist($host,$port) {
  11.                 $opt = "playlistid";
  12.                 $mpdstatus = getStatus($opt,$host,$port);
  13.                 array_shift($mpdstatus);
  14.                 array_pop($mpdstatus);
  15.                 $endArray = array_search_partial($mpdstatus, "Id:");
  16.                 $endArray = abs($endArray + "1");
  17.                 $status=(array_chunk($mpdstatus,$endArray));
  18.                 array_pop($status);
  19.                 foreach ($status as $song) {
  20.                 $artist_key=array_search_partial($song, "Artist:"); // e.g. albumartist etc.
  21.                 $title_key=array_search_partial($song, "Title:");
  22.                 $songArtist = $song[$artist_key];
  23.                 $songTitle = $song[$title_key];
  24.                 $songArtist = stripColon($songArtist);
  25.                 $songTitle = stripColon($songTitle);
  26.                 echo "$songArtist - $songTitle<br />\n";
  27.                 }
  28. }
  29.  
  30. function stripColon($opt) {
  31.         if (!empty($opt)) {
  32.         $state=explode(":",$opt);
  33.         $opt=$state['1'];
  34.         $opt=trim($opt);
  35.         } else {
  36.         $opt = "unknown";
  37.         }
  38.         return $opt;
  39. }
  40.  
  41. function array_search_partial($arr, $keyword) {
  42.         foreach($arr as $index => $string) {
  43.                 if (strpos($string, $keyword) !== FALSE)
  44.                 return $index;
  45.         }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement