/// <summary> /// Executes the specified param. /// </summary> /// <param name="param">The param.</param> /// <param name="result">The result.</param> /// <returns></returns> public OpResult Execute(string param) { OpResult opResult = new OpResult(); bool bFirst = !queue; bool bByIndex = false; int idx = 0; opResult.StatusCode = OpStatusCode.Ok; try { if (Player == null) { Player = new WMPLib.WindowsMediaPlayer(); } if (param.IndexOf("-help") >= 0) { opResult = showHelp(opResult); return(opResult); } DateTime startTime = DateTime.Now; // Use custom format? Match custom_match = custom_regex.Match(param); if (custom_match.Success) { showWhat = System.Convert.ToInt32(custom_match.Groups["index"].Value, 10); param = custom_match.Groups["remainder"].Value; } Match match = index_regex.Match(param); if (match.Success) { idx = System.Convert.ToInt32(match.Groups["index"].Value, 10); bByIndex = true; param = ""; } else { idx = 0; param = param.ToLower(); } if (showWhat == 0) { opResult = listArtistsOnly(opResult, param); TimeSpan duration = DateTime.Now - startTime; opResult.AppendFormat("elapsed_time={0}", duration.TotalSeconds); return(opResult); } if (param.Length > 0) { mediaPlaylist = byArtist(param); } else { mediaPlaylist = Player.mediaCollection.getByAttribute("MediaType", "Audio"); } bool bArtistMatch = false; the_state.init(); int iCount = mediaPlaylist.count; string keyArtist = ""; string keyAlbum = ""; // Header: templateOut(String.Format("{0}H", showWhat), opResult, 0, -1); for (int x = idx; x < iCount; x++) { media = mediaPlaylist.get_Item(x); the_state.nextArtist = media.getItemInfo("WM/AlbumArtist"); if (the_state.nextArtist == "") { the_state.nextArtist = media.getItemInfo("Author"); } the_state.nextAlbum = media.getItemInfo("WM/AlbumTitle"); if (bByIndex && x == idx) { keyArtist = the_state.nextArtist; keyAlbum = the_state.nextAlbum; } else { if (showBy == by_track && keyArtist.Length > 0 && x != idx) { break; } if (showBy == by_artist && keyArtist.Length > 0 && the_state.nextArtist != keyArtist) { break; } if (showBy == by_album && keyArtist.Length > 0 && (the_state.nextAlbum != keyAlbum || the_state.nextArtist != keyArtist)) { break; } } if (the_state.nextArtist != the_state.artist) // New artist? { if (bArtistMatch) // Did last artist match? { // Close album: if (the_state.album.Length > 0) { if (!templateOut(String.Format("{0}.{1}-", showWhat, show_albums), opResult, x, -1) && ((showWhat & show_albums) != 0) && the_state.albumTrackCount > 0) { opResult.AppendFormat(" Album_song_count: {0}", the_state.albumTrackCount); } } the_state.resetAlbum(); // Close artist" if (!templateOut(String.Format("{0}.{1}-", showWhat, show_artists), opResult, x, -1)) { if (the_state.artistAlbumCount > 0) { opResult.AppendFormat(" Artist_album_count: {0}", the_state.artistAlbumCount); } if (the_state.artistTrackCount > 0) { opResult.AppendFormat(" Artist_song_count: {0}", the_state.artistTrackCount); } } the_state.resetArtist(); } if (the_state.nextArtist.ToLower().StartsWith(param)) { bArtistMatch = true; the_state.artistCount += 1; the_state.artistIndex = x; if (keyArtist.Length <= 0) { keyArtist = the_state.nextArtist; keyAlbum = the_state.nextAlbum; } } else { bArtistMatch = false; } the_state.artist = the_state.nextArtist; if (bArtistMatch) { // Open Artist if (!templateOut(String.Format("{0}.{1}+", showWhat, show_artists), opResult, x, -1) && ((showWhat & show_artists) != 0)) { opResult.AppendFormat("Artist:<{0}> \"{1}\"", x, the_state.artist); } } } if (bArtistMatch) { the_state.artistTrackCount += 1; the_state.trackCount += 1; if (the_state.nextAlbum != the_state.album) { if (the_state.album.Length > 0) { // Close album if (!templateOut(String.Format("{0}.{1}-", showWhat, show_albums), opResult, x, -1) && ((showWhat & show_albums) != 0) && the_state.albumTrackCount > 0) { opResult.AppendFormat(" Album_song_count: {0}", the_state.albumTrackCount); } } the_state.resetAlbum(); the_state.album = the_state.nextAlbum; if (the_state.nextAlbum.Length > 0) { the_state.albumIndex = x; the_state.albumCount += 1; the_state.artistAlbumCount += 1; the_state.albumList_add(the_state.nextAlbum); the_state.albumYear = media.getItemInfo("WM/Year"); the_state.albumGenre = media.getItemInfo("WM/Genre"); the_state.findAlbumCover(media.sourceURL); // Open album if (!templateOut(String.Format("{0}.{1}+", showWhat, show_albums), opResult, x, -1) && ((showWhat & show_albums) != 0)) { opResult.AppendFormat(" Album:<{0}> \"{1}\"", x, the_state.nextAlbum); } } } the_state.album = the_state.nextAlbum; the_state.albumTrackCount += 1; the_state.song = media.getItemInfo("Title"); the_state.songLocation = media.sourceURL; the_state.songTrackNumber = media.getItemInfo("WM/TrackNumber"); the_state.songLength = media.durationString; if (the_state.albumYear == "" || the_state.albumYear.Length < 4) { the_state.albumYear = media.getItemInfo("WM/OriginalReleaseYear"); } the_state.albumGenre_add(media.getItemInfo("WM/Genre")); the_state.findAlbumCover(the_state.songLocation); // Open / close song if (!templateOut(String.Format("{0}.{1}-", showWhat, show_songs), opResult, x, -1) && ((showWhat & show_songs) != 0) && the_state.song.Length > 0) { opResult.AppendFormat(" Song:<{0}> \"{1}\" ({2})", x, the_state.song, the_state.songLength); } templateOut(String.Format("{0}.{1}+", showWhat, show_songs), opResult, x, -1); // Play / queue song? if (play) { PlayMediaCmd pmc; pmc = new PlayMediaCmd(MediaType.Audio, !bFirst); bFirst = false; opResult = pmc.Execute(media.sourceURL); } } } if (play) { opResult.AppendFormat("Queued {0} songs to play", the_state.trackCount); } else { // Close album: if (the_state.album.Length > 0) { if (!templateOut(String.Format("{0}.{1}-", showWhat, show_albums), opResult, the_state.trackCount, -1) && ((showWhat & show_albums) != 0) && the_state.albumTrackCount > 0) { opResult.AppendFormat(" Album_song_count: {0}", the_state.albumTrackCount); } } the_state.resetAlbum(); // Close artist: if (!templateOut(String.Format("{0}.{1}-", showWhat, show_artists), opResult, the_state.trackCount, -1)) { if (the_state.artistAlbumCount > 0) { opResult.AppendFormat(" Artist_album_count: {0}", the_state.artistAlbumCount); } if (the_state.artistTrackCount > 0) { opResult.AppendFormat(" Artist_song_count: {0}", the_state.artistTrackCount); } } the_state.resetArtist(); } // Footer: TimeSpan elapsed_duration = DateTime.Now - startTime; if (!templateOut(String.Format("{0}F", showWhat), opResult, the_state.trackCount, elapsed_duration.TotalSeconds)) { if (the_state.artistCount > 0) { opResult.AppendFormat("Artist_count: {0}", the_state.artistCount); } if (the_state.albumCount > 0) { opResult.AppendFormat("Album_count: {0}", the_state.albumCount); } if (the_state.trackCount > 0) { opResult.AppendFormat("Song_count: {0}", the_state.trackCount); } opResult.AppendFormat("elapsed_time={0}", elapsed_duration.TotalSeconds); } } catch (Exception ex) { opResult.StatusCode = OpStatusCode.Exception; opResult.StatusText = ex.Message; } return(opResult); }