예제 #1
0
        /// <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)
        {
            debug_last_action = "Execute: Start";

            DateTime startTime = DateTime.Now;

            OpResult opResult = new OpResult();
            opResult.StatusCode = OpStatusCode.Ok;

            bool bFirst = false;
            int size_x = 0;
            int size_y = 0;

            string template = "";
            string cache_fn = make_cache_fn(String.Format("{0}-{1}.txt", which_command, param));
            string cache_body = "";
            bool is_cached = false;

            try
            {
                if (param.IndexOf("-help") >= 0)
                {
                    opResult = showHelp(opResult);
                    return opResult;
                }
                //if (Player == null) Player = new WMPLib.WindowsMediaPlayer();

                WMPLib.IWMPMediaCollection2 collection = (WMPLib.IWMPMediaCollection2)Player.mediaCollection;
                int ver = Player.mediaCollection.getByAttribute("MediaType", "Audio").count;//.GetHashCode();
                string cache_ver = String.Format("{0}", ver);
                WMPLib.IWMPQuery query = collection.createQuery();
                WMPLib.IWMPPlaylist mediaPlaylist = null;
                WMPLib.IWMPMedia media_item;

                ArrayList a_idx = new ArrayList();

                bool b_query = false;

                string recent_text = "";
                string recent_text_type = "";

                debug_last_action = "Execution: Parsing params";

                request_params = HttpUtility.UrlEncode(param);

                if (param.Contains("exact-genre:"))
                {
                    string genre = param.Substring(param.IndexOf("exact-genre:") + "exact-genre:".Length);
                    genre = trim_parameter(genre);
                    query.addCondition("Genre", "Equals", genre);
                    genre_filter = genre;
                    recent_text = genre;
                    recent_text_type = "Genre";
                    b_query = true;
                }
                else if (param.Contains("genre:*"))
                {
                    string genre = param.Substring(param.IndexOf("genre:*") + "genre:*".Length);
                    genre = trim_parameter(genre);
                    query.addCondition("Genre", "BeginsWith", genre);
                    query.beginNextGroup();
                    query.addCondition("Genre", "Contains", " " + genre);
                    genre_filter = genre;
                    recent_text = genre;
                    recent_text_type = "Genre";
                    b_query = true;
                }
                else if (param.Contains("genre:"))
                {
                    string genre = param.Substring(param.IndexOf("genre:") + "genre:".Length);
                    genre = trim_parameter(genre);
                    query.addCondition("Genre", "BeginsWith", genre);
                    genre_filter = genre;
                    recent_text = genre;
                    recent_text_type = "Genre";
                    b_query = true;
                }

                if (param.Contains("exact-artist:"))
                {
                    string artist = param.Substring(param.IndexOf("exact-artist:") + "exact-artist:".Length);
                    artist = trim_parameter(artist);
                    query.addCondition("Artist", "Equals", artist);
                    artist_filter = artist;
                    if (recent_text.Length > 0)
                    {
                        recent_text += ": ";
                        recent_text_type += "/";
                    }
                    recent_text += artist;
                    recent_text_type += "Artist";
                    b_query = true;
                }
                else if (param.Contains("artist:*"))
                {
                    string artist = param.Substring(param.IndexOf("artist:*") + "artist:*".Length);
                    artist = trim_parameter(artist);
                    query.addCondition("Artist", "BeginsWith", artist);
                    query.beginNextGroup();
                    query.addCondition("Artist", "Contains", " " + artist);
                    artist_filter = artist;
                    if (recent_text.Length > 0)
                    {
                        recent_text += ": ";
                        recent_text_type += "/";
                    }
                    recent_text += artist;
                    recent_text_type += "Artist";
                    b_query = true;
                }
                else if (param.Contains("artist:"))
                {
                    string artist = param.Substring(param.IndexOf("artist:") + "artist:".Length);
                    artist = trim_parameter(artist);
                    query.addCondition("Artist", "BeginsWith", artist);
                    artist_filter = artist;
                    if (recent_text.Length > 0)
                    {
                        recent_text += ": ";
                        recent_text_type += "/";
                    }
                    recent_text += artist;
                    recent_text_type += "Artist";
                    b_query = true;
                }

                if (param.Contains("exact-album:"))
                {
                    string album = param.Substring(param.IndexOf("exact-album:") + "exact-album:".Length);
                    album = trim_parameter(album);
                    query.addCondition("WM/AlbumTitle", "Equals", album);
                    album_filter = album;
                    if (recent_text.Length > 0)
                    {
                        recent_text += ": ";
                        recent_text_type += "/";
                    }
                    recent_text += album;
                    recent_text_type += "Album";
                    b_query = true;
                }
                //else if (param.Contains("album:*"))
                //{
                //    string artist = param.Substring(param.IndexOf("album:*") + "album:*".Length);
                //    if (album.IndexOf(" ") >= 0) album = album.Substring(0, album.IndexOf(" "));
                //    query.addCondition("WM/AlbumTitle", "BeginsWith", album);
                //    query.beginNextGroup();
                //    query.addCondition("WM/AlbumTitle", "Contains", " " + album);
                //}
                else if (param.Contains("album:"))
                {
                    string album = param.Substring(param.IndexOf("album:") + "album:".Length);
                    album = trim_parameter(album);
                    query.addCondition("WM/AlbumTitle", "BeginsWith", album);
                    album_filter = album;
                    if (recent_text.Length > 0)
                    {
                        recent_text += ": ";
                        recent_text_type += "/";
                    }
                    recent_text += album;
                    recent_text_type += "Album";
                    b_query = true;
                }

                // Indexes specified?
                if (param.Contains("indexes:"))
                {
                    string indexes = param.Substring(param.IndexOf("indexes:") + "indexes:".Length);
                    if (indexes.IndexOf(" ") >= 0) indexes = indexes.Substring(0, indexes.IndexOf(" "));
                    string[] s_idx = indexes.Split(',');
                    foreach (string s in s_idx)
                    {
                        if (s.Length > 0) a_idx.Add(Int16.Parse(s));
                    }
                    if (recent_text.Length > 0)
                    {
                        recent_text += ": ";
                        recent_text_type += "/";
                    }
                    recent_text_type += "Tracks";
                    b_query = true;
                }
                if (!b_query) recent_text_type = recent_text = "All";

                // Cover size specified?
                if (param.Contains("size-x:"))
                {
                    string tmp_size = param.Substring(param.IndexOf("size-x:") + "size-x:".Length);
                    if (tmp_size.IndexOf(" ") >= 0) tmp_size = tmp_size.Substring(0, tmp_size.IndexOf(" "));
                    size_x = Convert.ToInt32(tmp_size);
                }
                if (param.Contains("size-y:"))
                {
                    string tmp_size = param.Substring(param.IndexOf("size-y:") + "size-y:".Length);
                    if (tmp_size.IndexOf(" ") >= 0) tmp_size = tmp_size.Substring(0, tmp_size.IndexOf(" "));
                    size_y = Convert.ToInt32(tmp_size);
                }
                // Use Custom Template?
                if (param.Contains("template:"))
                {
                    template = param.Substring(param.IndexOf("template:") + "template:".Length);
                    if (template.IndexOf(" ") >= 0) template = template.Substring(0, template.IndexOf(" "));
                }

                if (which_command == PLAY) bFirst = true;

                switch (which_command)
                {
                    case CLEAR_CACHE:
                        clear_cache();
                        return opResult;
                        break;
                    case LIST_GENRES:
                        cache_body = get_cached(cache_fn, cache_ver);
                        if (cache_body.Length == 0)
                        {
                            WMPLib.IWMPStringCollection genres = collection.getStringCollectionByQuery("Genre", query, "Audio", "Genre", true);
                            do_basic_list(opResult, genres, "genre", template, "genre=%genre%");
                            result_count = genres.count;
                        }
                        else
                        {
                            is_cached = true;
                            opResult.ContentText = cache_body;
                        }
                        break;
                    case LIST_ARTISTS:
                        cache_body = get_cached(cache_fn, cache_ver);
                        if (cache_body.Length == 0)
                        {
                            WMPLib.IWMPStringCollection artists = collection.getStringCollectionByQuery("Artist", query, "Audio", "Artist", true);
                            do_basic_list(opResult, artists, "artist", template, "artist=%artist%");
                            result_count = artists.count;
                        }
                        else
                        {
                            is_cached = true;
                            opResult.ContentText = cache_body;
                        }
                        break;
                    case LIST_ALBUM_ARTISTS:
                        cache_body = get_cached(cache_fn, cache_ver);
                        if (cache_body.Length == 0)
                        {
                            WMPLib.IWMPStringCollection artists = collection.getStringCollectionByQuery("WM/AlbumArtist", query, "Audio", "WM/AlbumArtist", true);
                            do_basic_list(opResult, artists, "artist", template, "album_artist=%artist%");
                            result_count = artists.count;
                        }
                        else
                        {
                            is_cached = true;
                            opResult.ContentText = cache_body;
                        }
                        break;
                    case LIST_ALBUMS:
                        cache_body = get_cached(cache_fn, cache_ver);
                        if (cache_body.Length == 0)
                        {
                            WMPLib.IWMPStringCollection albums = collection.getStringCollectionByQuery("WM/AlbumTitle", query, "Audio", "WM/AlbumTitle", true);
                            do_basic_list(opResult, albums, "album", template, "album=%album%");
                            result_count = albums.count;
                        }
                        else
                        {
                            is_cached = true;
                            opResult.ContentText = cache_body;
                        }
                        break;
                    case LIST_SONGS:
                        cache_body = get_cached(cache_fn, cache_ver);
                        if (cache_body.Length == 0)
                        {
                            WMPLib.IWMPStringCollection songs = collection.getStringCollectionByQuery("Title", query, "Audio", "Title", true);
                            do_basic_list(opResult, songs, "song", template, "index=%index%, song=%song%");
                            result_count = songs.count;
                        }
                        else
                        {
                            is_cached = true;
                            opResult.ContentText = cache_body;
                        }
                        break;
                    case LIST_RECENT:
                        if (param.Contains("count:"))
                        {
                            string scount = param.Substring(param.IndexOf("count:") + "count:".Length);
                            if (scount.IndexOf(" ") >= 0) scount = scount.Substring(0, scount.IndexOf(" "));
                            int count = Convert.ToInt32(scount);
                            list_recent(opResult, template, count);
                        }
                        else list_recent(opResult, template);
                        return opResult;
                        break;
                    case LIST_STATS:
                        cache_body = get_cached(cache_fn, cache_ver);
                        if (cache_body.Length == 0)
                        {
                            list_stats(opResult, template);
                        }
                        else
                        {
                            is_cached = true;
                            opResult.ContentText = cache_body;
                        }
                        break;
                    case LIST_DETAILS:
                    case PLAY:
                    case QUEUE:
                    case SERV_COVER:
                        if (which_command == LIST_DETAILS)
                        {
                            cache_body = get_cached(cache_fn, cache_ver);
                            if (cache_body.Length > 0)
                            {
                                is_cached = true;
                                opResult.ContentText = cache_body;
                                break;
                            }
                        }
                        if (which_command == SERV_COVER || which_command == LIST_DETAILS) the_state.init();

                        if (b_query) mediaPlaylist = collection.getPlaylistByQuery(query, "Audio", "Artist", true);
                        else mediaPlaylist = Player.mediaCollection.getByAttribute("MediaType", "Audio");

                        if (a_idx.Count > 0) result_count = a_idx.Count;
                        else result_count = mediaPlaylist.count;

                        // Header
                        opResult.AppendFormat("{0}", basic_replacer(getTemplate(template + ".H", ""), "", "", result_count, -1));

                        if (a_idx.Count > 0)
                        {
                            result_count = 0;
                            for (int j = 0; j < a_idx.Count; j++)
                            {
                                try { media_item = mediaPlaylist.get_Item((Int16)a_idx[j]); }
                                catch (Exception) { media_item = null; }
                                if (media_item != null)
                                {
                                    result_count++;
                                    if (which_command == LIST_DETAILS || which_command == SERV_COVER) // Display it
                                    {
                                        do_detailed_list(opResult, media_item, (Int16)a_idx[j], template);
                                        if (which_command == SERV_COVER)
                                        {
                                            photoCmd pc;
                                            pc = new photoCmd(photoCmd.SERV_PHOTO);
                                            if (the_state.albumImage.Length == 0)
                                                return pc.getPhoto(DEFAULT_IMAGE, "jpeg", size_x, size_y);
                                            else return pc.getPhoto(the_state.albumImage, "jpeg", size_x, size_y);
                                        }
                                    }
                                    else // Play / Queue it
                                    {
                                        recent_text += ((j == 0) ? "" : ", ") + (Int16)a_idx[j] + ". " + media_item.getItemInfo("Title");
                                        PlayMediaCmd pmc;
                                        pmc = new PlayMediaCmd(MediaType.Audio, !bFirst);
                                        bFirst = false;
                                        opResult = pmc.Execute(media_item.sourceURL);
                                    }
                                }
                            }
                        }
                        else
                        {
                            for (int j = 0; j < mediaPlaylist.count; j++)
                            {
                                media_item = mediaPlaylist.get_Item(j);
                                if (which_command == LIST_DETAILS || which_command == SERV_COVER) // Display it
                                {
                                    do_detailed_list(opResult, media_item, j, template);
                                    if (which_command == SERV_COVER)
                                    {
                                        photoCmd pc;
                                        pc = new photoCmd(photoCmd.SERV_PHOTO);
                                        if (the_state.albumImage.Length == 0)
                                            return pc.getPhoto(DEFAULT_IMAGE, "jpeg", size_x, size_y);
                                        else return pc.getPhoto(the_state.albumImage, "jpeg", size_x, size_y);
                                    }
                                }
                                else // Play / Queue it
                                {
                                    PlayMediaCmd pmc;
                                    pmc = new PlayMediaCmd(MediaType.Audio, !bFirst);
                                    bFirst = false;
                                    opResult = pmc.Execute(media_item.sourceURL);
                                }
                            }
                        }
                        if (which_command == LIST_DETAILS) do_detailed_list(opResult, null, -1, template);

                        if ((which_command == PLAY || which_command == QUEUE) && result_count > 0)
                        {
                            // Type, Artist, Album, Track, param, count
                            add_to_mrp(recent_text_type, recent_text, param, result_count); //Add to recent played list
                        }
                        break;
                }

                // Footer
                if (!is_cached)
                {
                    if (which_command != LIST_DETAILS) opResult.AppendFormat("{0}", basic_replacer(getTemplate(template + ".F", "result_count=%resultCount%"), "", "", result_count, -1));
                    else opResult.AppendFormat("{0}", replacer(getTemplate(template + ".F", "result_count=%index%"), result_count));
                    //opResult.AppendFormat("result_count={0}", result_count);
                    save_to_cache(cache_fn, opResult.ToString(), cache_ver);
                }
                string sub_footer = basic_replacer(getTemplate(template + ".C", "from_cache=%wasCached%\r\nellapsed_time=%ellapsedTime%"), "wasCached", is_cached.ToString(), -1, -1);
                TimeSpan duration = DateTime.Now - startTime;
                sub_footer = basic_replacer(sub_footer, "ellapsedTime", String.Format("{0}", duration.TotalSeconds), -1, -1);
                opResult.AppendFormat("{0}", sub_footer);
            }
            catch (Exception ex)
            {
                opResult = new OpResult();
                opResult.StatusCode = OpStatusCode.Exception;
                opResult.StatusText = ex.Message;
                opResult.AppendFormat("{0}", debug_last_action);
                opResult.AppendFormat("{0}", ex.Message);
            }

            debug_last_action = "Execute: End";

            return opResult;
        }
예제 #2
0
        /// <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, NowPlayingList nowPlaying, MediaItem currentMedia)
        {
            OpResult opResult = new OpResult();
            opResult.StatusCode = OpStatusCode.Json;

            if (currentMedia != null)
            {
                opResult.ContentText = JsonConvert.SerializeObject(currentMedia, Formatting.Indented);
                return opResult;
            }
            else if (param.IndexOf("-help") >= 0)
            {
                opResult.StatusCode = OpStatusCode.Ok;
                opResult = showHelp(opResult);
                return opResult;
            }

            debug_last_action = "Execute: Start";
            bool should_enqueue = true;
            int size_x = 0;
            int size_y = 0;
            string create_playlist_name = null;
            string playlist_query = null;
            string template = "";
            string cache_fn = make_cache_fn(String.Format("{0}-{1}.txt", which_command, param));
            string cache_body = "";
            try
            {
                IWMPMediaCollection2 collection = (IWMPMediaCollection2)Player.mediaCollection;
                IWMPPlaylistCollection playlistCollection = (IWMPPlaylistCollection)Player.playlistCollection;

                int ver = Player.mediaCollection.getByAttribute("MediaType", "Audio").count;
                string cache_ver = String.Format("{0}", ver);
                cache_body = get_cached(cache_fn, cache_ver);
                if (cache_body.Length != 0 && create_playlist_name == null && playlist_query == null && !m_stats_only)
                {
                    opResult.ContentText = setCachedFlag(cache_body);
                    return opResult;
                }

                IWMPQuery query = collection.createQuery();
                IWMPPlaylistArray playlists = null;
                IWMPPlaylist mediaPlaylist = null;
                IWMPMedia media_item;

                ArrayList query_indexes = new ArrayList();

                Library metadata = new Library(m_stats_only);

                bool has_query = false;
                bool has_exact_query = false;

                string query_text = "";
                string query_type = "";

                debug_last_action = "Execution: Parsing params";

                request_params = HttpUtility.UrlEncode(param);

                if (param.Contains("create-playlist:"))
                {
                    create_playlist_name = param.Substring(param.IndexOf("create-playlist:") + "create-playlist:".Length);
                    create_playlist_name = trim_parameter(create_playlist_name);
                }

                if (param.Contains("exact-genre:"))
                {
                    string genre = param.Substring(param.IndexOf("exact-genre:") + "exact-genre:".Length);
                    genre = trim_parameter(genre);
                    query.addCondition("WM/Genre", "Equals", genre);
                    genre_filter = genre;
                    query_text = genre;
                    query_type = "Genre";
                    has_query = true;
                    has_exact_query = true;
                }
                else if (param.Contains("genre:*"))
                {
                    string genre = param.Substring(param.IndexOf("genre:*") + "genre:*".Length);
                    genre = trim_parameter(genre);
                    query.addCondition("WM/Genre", "BeginsWith", genre);
                    query.beginNextGroup();
                    query.addCondition("WM/Genre", "Contains", " " + genre);
                    genre_filter = genre;
                    query_text = genre;
                    query_type = "Genre";
                    has_query = true;
                }
                else if (param.Contains("genre:"))
                {
                    string genre = param.Substring(param.IndexOf("genre:") + "genre:".Length);
                    genre = trim_parameter(genre);
                    query.addCondition("WM/Genre", "BeginsWith", genre);
                    genre_filter = genre;
                    query_text = genre;
                    query_type = "Genre";
                    has_query = true;
                }

                if (param.Contains("exact-artist:"))
                {
                    string artist = param.Substring(param.IndexOf("exact-artist:") + "exact-artist:".Length);
                    artist = trim_parameter(artist);
                    query.addCondition("Author", "Equals", artist);
                    artist_filter = artist;
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_text += artist;
                    query_type += "Artist";
                    has_query = true;
                    has_exact_query = true;
                }
                else if (param.Contains("exact-album-artist:"))
                {
                    string album_artist = param.Substring(param.IndexOf("exact-album-artist:") + "exact-album-artist:".Length);
                    album_artist = trim_parameter(album_artist);
                    query.addCondition("WM/AlbumArtist", "Equals", album_artist);
                    artist_filter = album_artist;
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_text += album_artist;
                    query_type += "Album Artist";
                    has_query = true;
                    has_exact_query = true;
                }
                else if (param.Contains("album-artist:*"))
                {
                    string album_artist = param.Substring(param.IndexOf("album-artist:*") + "album-artist:*".Length);
                    album_artist = trim_parameter(album_artist);
                    query.addCondition("WM/AlbumArtist", "BeginsWith", album_artist);
                    query.beginNextGroup();
                    query.addCondition("WM/AlbumArtist", "Contains", " " + album_artist);
                    artist_filter = album_artist;
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_text += album_artist;
                    query_type += "Album Artist";
                    has_query = true;
                }
                else if (param.Contains("album-artist:"))
                {
                    string album_artist = param.Substring(param.IndexOf("album-artist:") + "album-artist:".Length);
                    album_artist = trim_parameter(album_artist);
                    query.addCondition("WM/AlbumArtist", "BeginsWith", album_artist);
                    artist_filter = album_artist;
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_text += album_artist;
                    query_type += "Album Artist";
                    has_query = true;
                }
                else if (param.Contains("artist:*"))
                {
                    string artist = param.Substring(param.IndexOf("artist:*") + "artist:*".Length);
                    artist = trim_parameter(artist);
                    query.addCondition("Author", "BeginsWith", artist);
                    query.beginNextGroup();
                    query.addCondition("Author", "Contains", " " + artist);
                    artist_filter = artist;
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_text += artist;
                    query_type += "Artist";
                    has_query = true;
                }
                else if (param.Contains("artist:"))
                {
                    string artist = param.Substring(param.IndexOf("artist:") + "artist:".Length);
                    artist = trim_parameter(artist);
                    query.addCondition("Author", "BeginsWith", artist);
                    artist_filter = artist;
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_text += artist;
                    query_type += "Artist";
                    has_query = true;
                }

                if (param.Contains("exact-album:"))
                {
                    string album = param.Substring(param.IndexOf("exact-album:") + "exact-album:".Length);
                    album = trim_parameter(album);
                    query.addCondition("WM/AlbumTitle", "Equals", album);
                    album_filter = album;
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_text += album;
                    query_type += "Album";
                    has_query = true;
                    has_exact_query = true;
                }
                else if (param.Contains("album:"))
                {
                    string album = param.Substring(param.IndexOf("album:") + "album:".Length);
                    album = trim_parameter(album);
                    query.addCondition("WM/AlbumTitle", "BeginsWith", album);
                    album_filter = album;
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_text += album;
                    query_type += "Album";
                    has_query = true;
                }

                //This is not for a query but rather for playing/enqueing exact songs
                if (param.Contains("exact-song:"))
                {
                    string song = param.Substring(param.IndexOf("exact-song:") + "exact-song:".Length);
                    song = trim_parameter(song);
                    song_filter = song;
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_text += song;
                    query_type += "Song";
                    has_exact_query = true;
                }

                if (param.Contains("exact-playlist:"))
                {
                    playlist_query = param.Substring(param.IndexOf("exact-playlist:") + "exact-playlist:".Length);
                    playlist_query = trim_parameter(playlist_query);
                }

                // Indexes specified?
                if (param.Contains("indexes:"))
                {
                    string indexes = param.Substring(param.IndexOf("indexes:") + "indexes:".Length);
                    if (indexes.IndexOf(" ") >= 0) indexes = indexes.Substring(0, indexes.IndexOf(" "));
                    string[] s_idx = indexes.Split(',');
                    foreach (string s in s_idx)
                    {
                        if (s.Length > 0) query_indexes.Add(Int16.Parse(s));
                    }
                    if (query_text.Length > 0)
                    {
                        query_text += ": ";
                        query_type += "/";
                    }
                    query_type += "Tracks";
                    has_query = true;
                }

                if (!has_query) query_type = query_text = "All";

                // Cover size specified?
                if (param.Contains("size-x:"))
                {
                    string tmp_size = param.Substring(param.IndexOf("size-x:") + "size-x:".Length);
                    if (tmp_size.IndexOf(" ") >= 0) tmp_size = tmp_size.Substring(0, tmp_size.IndexOf(" "));
                    size_x = Convert.ToInt32(tmp_size);
                }
                if (param.Contains("size-y:"))
                {
                    string tmp_size = param.Substring(param.IndexOf("size-y:") + "size-y:".Length);
                    if (tmp_size.IndexOf(" ") >= 0) tmp_size = tmp_size.Substring(0, tmp_size.IndexOf(" "));
                    size_y = Convert.ToInt32(tmp_size);
                }
                // Use Custom Template?
                if (param.Contains("template:"))
                {
                    template = param.Substring(param.IndexOf("template:") + "template:".Length);
                    if (template.IndexOf(" ") >= 0) template = template.Substring(0, template.IndexOf(" "));
                }

                if (which_command == PLAY) should_enqueue = false;

                switch (which_command)
                {
                    case CLEAR_CACHE:
                        opResult.StatusCode = OpStatusCode.Ok;
                        clear_cache();
                        return opResult;
                    case LIST_GENRES:
                        if (create_playlist_name != null)
                        {
                            IWMPPlaylist genre_playlist = collection.getPlaylistByQuery(query, "Audio", "WM/Genre", true);
                            add_to_playlist(opResult, has_query, genre_playlist, create_playlist_name);
                            opResult.StatusCode = OpStatusCode.Ok;
                        }
                        else
                        {
                            IWMPStringCollection genres;
                            if (has_query)
                            {
                                genres = collection.getStringCollectionByQuery("WM/Genre", query, "Audio", "WM/Genre", true);
                            }
                            else
                            {
                                genres = collection.getAttributeStringCollection("WM/Genre", "Audio");
                            }
                            if (genres != null && genres.count > 0)
                            {
                                result_count = 0;
                                for (int k = 0; k < genres.count; k++)
                                {
                                    string item = genres.Item(k);
                                    if (item != null && !item.Equals(""))
                                    {
                                        if (!m_stats_only) metadata.addGenre(item);
                                        else result_count++;
                                    }
                                }
                                opResult.ResultCount = result_count;
                                metadata.trimToSize();
                                opResult = serializeObject(opResult, metadata, cache_fn, cache_ver);
                            }
                            else
                            {
                                opResult.StatusCode = OpStatusCode.BadRequest;
                                opResult.StatusText = "No genres found!";
                            }
                        }
                        return opResult;
                    case LIST_ARTISTS:
                        if (create_playlist_name != null)
                        {
                            IWMPPlaylist artists_playlist = collection.getPlaylistByQuery(query, "Audio", "Author", true);
                            add_to_playlist(opResult, has_query, artists_playlist, create_playlist_name);
                            opResult.StatusCode = OpStatusCode.Ok;
                        }
                        else
                        {
                            IWMPStringCollection artists;
                            if (has_query)
                            {
                                artists = collection.getStringCollectionByQuery("Author", query, "Audio", "Author", true);
                            }
                            else
                            {
                                artists = collection.getAttributeStringCollection("Author", "Audio");
                            }
                            if (artists != null && artists.count > 0)
                            {
                                result_count = 0;
                                for (int k = 0; k < artists.count; k++)
                                {
                                    string item = artists.Item(k);
                                    if (item != null && !item.Equals(""))
                                    {
                                        if (!m_stats_only) metadata.addArtist(item);
                                        else result_count++;
                                    }
                                }
                                opResult.ResultCount = result_count;
                                metadata.trimToSize();
                                opResult = serializeObject(opResult, metadata, cache_fn, cache_ver);
                            }
                            else
                            {
                                opResult.StatusCode = OpStatusCode.BadRequest;
                                opResult.StatusText = "No artists found!";
                            }
                        }
                        return opResult;
                    case LIST_ALBUM_ARTISTS:
                        if (create_playlist_name != null)
                        {
                            IWMPPlaylist album_artists_playlist = collection.getPlaylistByQuery(query, "Audio", "WM/AlbumArtist", true);
                            add_to_playlist(opResult, has_query, album_artists_playlist, create_playlist_name);
                            opResult.StatusCode = OpStatusCode.Ok;
                        }
                        else
                        {
                            IWMPStringCollection album_artists;
                            if (has_query)
                            {
                                album_artists = collection.getStringCollectionByQuery("WM/AlbumArtist", query, "Audio", "WM/AlbumArtist", true);
                            }
                            else
                            {
                                album_artists = collection.getAttributeStringCollection("WM/AlbumArtist", "Audio");
                            }
                            if (album_artists != null && album_artists.count > 0)
                            {
                                result_count = 0;
                                for (int k = 0; k < album_artists.count; k++)
                                {
                                    string item = album_artists.Item(k);
                                    if (item != null && !item.Equals("") && !metadata.containsAlbumArtist(item))
                                    {
                                        if (!m_stats_only) metadata.addAlbumArtist(item);
                                        else result_count++;
                                    }
                                }
                                opResult.ResultCount = result_count;
                                metadata.trimToSize();
                                opResult = serializeObject(opResult, metadata, cache_fn, cache_ver);
                            }
                            else
                            {
                                opResult.StatusCode = OpStatusCode.BadRequest;
                                opResult.StatusText = "No album artists found!";
                            }
                        }
                        return opResult;
                    case LIST_ALBUMS:
                        if (create_playlist_name != null)
                        {
                            IWMPPlaylist albums_playlist = collection.getPlaylistByQuery(query, "Audio", "WM/AlbumTitle", true);
                            add_to_playlist(opResult, has_query, albums_playlist, create_playlist_name);
                            opResult.StatusCode = OpStatusCode.Ok;
                        }
                        else
                        {
                            IWMPStringCollection albums;
                            if (has_query)
                            {
                                albums = collection.getStringCollectionByQuery("WM/AlbumTitle", query, "Audio", "WM/AlbumTitle", true);
                            }
                            else
                            {
                                albums = collection.getAttributeStringCollection("WM/AlbumTitle", "Audio");
                            }
                            if (albums != null && albums.count > 0)
                            {
                                result_count = 0;
                                for (int k = 0; k < albums.count; k++)
                                {
                                    string item = albums.Item(k);
                                    if (item != null && !item.Equals(""))
                                    {
                                        if (!m_stats_only) metadata.addAlbum(new Album(item, collection.getByAlbum(item), m_stats_only));
                                        else result_count++;
                                    }
                                }
                                opResult.ResultCount = result_count;
                                metadata.trimToSize();
                                opResult = serializeObject(opResult, metadata, cache_fn, cache_ver);
                            }
                            else
                            {
                                opResult.StatusCode = OpStatusCode.BadRequest;
                                opResult.StatusText = "No albums found!";
                            }
                        }
                        return opResult;
                    case LIST_SONGS:
                        IWMPStringCollection songs;
                        if (has_query)
                        {
                            songs = collection.getStringCollectionByQuery("Title", query, "Audio", "Title", true);
                        }
                        else
                        {
                            songs = collection.getAttributeStringCollection("Title", "Audio");
                        }
                        if (songs != null && songs.count > 0)
                        {
                            for (int k = 0; k < songs.count; k++)
                            {
                                IWMPPlaylist playlist = collection.getByName(songs.Item(k));
                                if (playlist != null && playlist.count > 0)
                                {
                                    if (create_playlist_name != null)
                                    {
                                        add_to_playlist(opResult, has_query, playlist, create_playlist_name);
                                        opResult.StatusCode = OpStatusCode.Ok;
                                    }
                                    else
                                    {
                                        metadata.addSongs(playlist);
                                    }
                                }
                            }
                            if (create_playlist_name == null)
                            {
                                metadata.trimToSize();
                                opResult = serializeObject(opResult, metadata, cache_fn, cache_ver);
                            }
                        }
                        else
                        {
                            opResult.StatusCode = OpStatusCode.BadRequest;
                            opResult.StatusText = "No songs found!";
                        }
                        return opResult;
                    case LIST_PLAYLISTS:
                        result_count = 0;
                        playlists = getAllUserPlaylists(playlistCollection);
                        result_count = metadata.addPlaylists(playlistCollection, playlists);
                        metadata.trimToSize();
                        opResult.ResultCount = result_count;
                        opResult.ContentText = JsonConvert.SerializeObject(metadata, Formatting.Indented);
                        return opResult;
                    case LIST_RECENT:
                        if (param.Contains("count:"))
                        {
                            string scount = param.Substring(param.IndexOf("count:") + "count:".Length);
                            if (scount.IndexOf(" ") >= 0) scount = scount.Substring(0, scount.IndexOf(" "));
                            int count = Convert.ToInt32(scount);
                            list_recent(opResult, template, count);
                        }
                        else list_recent(opResult, template);
                        opResult.StatusCode = OpStatusCode.Ok;
                        return opResult;
                    case LIST_NOWPLAYING:
                        if (nowPlaying != null)
                        {
                            opResult.ContentText = JsonConvert.SerializeObject(nowPlaying, Formatting.Indented);
                        }
                        else
                        {
                            opResult.StatusCode = OpStatusCode.BadRequest;
                            opResult.StatusText = "Now playing is null!";
                        }
                        return opResult;
                    case DELETE_PLAYLIST:
                        if (playlist_query != null)
                        {
                            playlists = getUserPlaylistsByName(playlist_query, playlistCollection);
                            if (playlists.count > 0)
                            {
                                IWMPPlaylist mod_playlist = playlists.Item(0);
                                if (query_indexes.Count > 0)
                                {
                                    // Delete items indicated by indexes instead of deleting playlist
                                    for (int j = 0; j < query_indexes.Count; j++)
                                    {
                                        mod_playlist.removeItem(mod_playlist.get_Item((Int16)query_indexes[j]));
                                    }
                                    opResult.ContentText = "Items removed from playlist " + mod_playlist + ".";
                                }
                                else
                                {
                                    ((IWMPPlaylistCollection)Player.playlistCollection).remove(mod_playlist);
                                    opResult.ContentText = "Playlist " + mod_playlist + " deleted.";
                                }
                                opResult.StatusCode = OpStatusCode.Success;
                            }
                            else
                            {
                                opResult.StatusCode = OpStatusCode.BadRequest;
                                opResult.StatusText = "Playlist does not exist!";
                            }
                        }
                        else
                        {
                            opResult.StatusCode = OpStatusCode.BadRequest;
                            opResult.StatusText = "Must specify the exact playlist!";
                        }
                        return opResult;
                    case LIST_DETAILS:
                        // Get  query as a playlist
                        if (playlist_query != null)
                        {
                            //Return a specific playlist when music-list-details with exact-playlist is queried
                            playlists = getUserPlaylistsByName(playlist_query, playlistCollection);
                            if (playlists.count > 0)
                            {
                                Playlist aPlaylist = new Playlist(playlist_query);
                                mediaPlaylist = playlists.Item(0);
                                //Or return a playlist query
                                if (mediaPlaylist != null)
                                {
                                    aPlaylist.addItems(mediaPlaylist);
                                }
                                metadata.playlists.Add(aPlaylist);
                            }
                        }
                        else if (has_exact_query)
                        {
                            mediaPlaylist = getPlaylistFromExactQuery(query_text, query_type, collection);
                        }
                        else if (has_query)
                        {
                            string type = getSortAttributeFromQueryType(query_type);
                            mediaPlaylist = collection.getPlaylistByQuery(query, "Audio", type, true);
                        }

                        if (mediaPlaylist != null)
                        {
                            //Create playlist from query if supplied with playlist name
                            if (create_playlist_name != null)
                            {
                                add_to_playlist(opResult, has_query, mediaPlaylist, create_playlist_name);
                                return opResult;
                            }
                            else if (query_indexes.Count > 0)
                            {
                                for (int j = 0; j < query_indexes.Count; j++)
                                {
                                    media_item = mediaPlaylist.get_Item((Int16)query_indexes[j]);
                                    if (media_item != null)
                                    {
                                        metadata.addSong(media_item);
                                    }
                                }
                            }
                            else
                            {
                                if (query_type.Equals("Album"))
                                {
                                    Album album = new Album(query_text, m_stats_only);
                                    album.addTracks(mediaPlaylist);
                                    metadata.addAlbum(album);
                                }
                                else
                                {
                                    metadata.addSongs(mediaPlaylist);
                                }
                            }
                        }
                        else
                        {
                            if (logger != null)
                            {
                                logger.Write("Creating library metadata object");
                            }
                            //No query supplied so entire detailed library requested
                            //Parse all albums and return, no value album will be added as songs
                            IWMPStringCollection album_collection = collection.getAttributeStringCollection("WM/AlbumTitle", "Audio");
                            if (album_collection.count > 0)
                            {
                                result_count = 0;
                                for (int j = 0; j < album_collection.count; j++)
                                {
                                    if (album_collection.Item(j) != null)
                                    {
                                        //The collection seems to represent the abcense of an album as an "" string value
                                        IWMPPlaylist album_playlist = collection.getByAlbum(album_collection.Item(j));
                                        if (album_playlist != null)
                                        {
                                            if (!album_collection.Item(j).Equals(""))
                                            {
                                                Album album = new Album(album_collection.Item(j), m_stats_only);
                                                result_count += album.addTracks(album_playlist);
                                                metadata.addAlbum(album);
                                            }
                                            else
                                            {
                                                result_count += metadata.addSongs(album_playlist);
                                            }
                                        }
                                    }
                                }
                                metadata.trimToSize();
                            }
                        }
                        if (logger != null)
                        {
                            logger.Write("Starting serialization of metadata object.");
                        }
                        opResult.ResultCount = result_count;
                        opResult = serializeObject(opResult, metadata, cache_fn, cache_ver);
                        return opResult;
                    case PLAY:
                    case QUEUE:
                        if (has_exact_query)
                        {
                            mediaPlaylist = getPlaylistFromExactQuery(query_text, query_type, collection);
                        }
                        else if (has_query)
                        {
                            string type = getSortAttributeFromQueryType(query_type);
                            mediaPlaylist = collection.getPlaylistByQuery(query, "Audio", type, true);
                        }
                        else
                        {
                            mediaPlaylist = collection.getByAttribute("MediaType", "Audio");
                        }
                        //Play or enqueue
                        PlayMediaCmd pmc;
                        if (query_indexes.Count > 0)
                        {
                            result_count = query_indexes.Count;
                            for (int j = 0; j < query_indexes.Count; j++)
                            {
                                media_item = mediaPlaylist.get_Item(j);
                                if (media_item != null)
                                {
                                    query_text += ((j == 0) ? "" : ", ") + (Int16)query_indexes[j] + ". " + media_item.getItemInfo("Title");
                                }
                            }
                            pmc = new PlayMediaCmd(remotePlayer, mediaPlaylist, query_indexes, should_enqueue);
                        }
                        else
                        {
                            result_count = mediaPlaylist.count;
                            pmc = new PlayMediaCmd(remotePlayer, mediaPlaylist, should_enqueue);
                        }
                        opResult = pmc.Execute(null);

                        // Type, Artist, Album, Track, param, count
                        add_to_mrp(query_type, query_text, param, result_count); //Add to recent played list
                        return opResult;
                    case SERV_COVER:
                        if (has_exact_query)
                        {
                            mediaPlaylist = getPlaylistFromExactQuery(query_text, query_type, collection);
                        }
                        else if (has_query)
                        {
                            string type = getSortAttributeFromQueryType(query_type);
                            mediaPlaylist = collection.getPlaylistByQuery(query, "Audio", type, true);
                        }
                        else
                        {
                            mediaPlaylist = collection.getByAttribute("MediaType", "Audio");
                        }

                        try
                        {
                            if (query_indexes.Count > 0)
                            {
                                for (int j = 0; j < query_indexes.Count; j++)
                                {
                                    media_item = mediaPlaylist.get_Item((Int16)query_indexes[j]);
                                    if (media_item != null)
                                    {
                                        string album_path = findAlbumPath(media_item.sourceURL);
                                        photoCmd pc = new photoCmd(photoCmd.SERV_PHOTO);
                                        if (album_path.Length == 0) return pc.getPhoto(DEFAULT_IMAGE, "jpeg", size_x, size_y);
                                        else return pc.getPhoto(album_path, "jpeg", size_x, size_y);
                                    }
                                }
                            }
                            else
                            {
                                for (int j = 0; j < mediaPlaylist.count; j++)
                                {
                                    media_item = mediaPlaylist.get_Item(j);
                                    if (media_item != null)
                                    {
                                        string album_path = findAlbumPath(media_item.sourceURL);
                                        photoCmd pc = new photoCmd(photoCmd.SERV_PHOTO);
                                        if (album_path.Length == 0) return pc.getPhoto(DEFAULT_IMAGE, "jpeg", size_x, size_y);
                                        else return pc.getPhoto(album_path, "jpeg", size_x, size_y);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            opResult.StatusCode = OpStatusCode.Exception;
                            opResult.StatusText = ex.Message;
                        }
                        return opResult;
                }
            }
            catch (Exception ex)
            {
                opResult = new OpResult();
                opResult.StatusCode = OpStatusCode.Exception;
                opResult.StatusText = ex.Message;
                opResult.AppendFormat("{0}", debug_last_action);
                opResult.AppendFormat("{0}", ex.Message);
            }

            debug_last_action = "Execute: End";

            return opResult;
        }
예제 #3
0
        /// <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);
        }
예제 #4
0
        /// <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;
        }