Exemplo n.º 1
0
        /// <summary>
        /// Parses the PLS file.
        /// </summary>
        /// <param name="textReader">TextReader representing a PLS playlist file.</param>
        /// <returns>A successfully parsed playlist.</returns>
        private IPlaylist Parse(TextReader textReader)
        {
            if (textReader == null)
            {
                throw new ArgumentNullException("textReader");
            }

            // Shoutcast.com PLS files are messed up.  The LengthX values are all Length1=-1 instead of LengthX=-1.
            IniParser iniFile = new IniParser(textReader, DuplicateNameHandling.Discard);
            Dictionary <string, Dictionary <string, string> > sections = iniFile.Sections;

            if (!sections.ContainsKey("playlist"))
            {
                throw new InvalidOperationException("playlist section not found");
            }

            PlsPlaylist playlist = new PlsPlaylist();
            ICollection <IPlaylistItem> items = playlist.Items;

            Dictionary <string, string> playlistEntries = sections["playlist"];

            int numberOfEntries;

            if ((!playlistEntries.ContainsKey("NumberOfEntries")) || (!int.TryParse(playlistEntries["NumberOfEntries"], out numberOfEntries)))
            {
                throw new InvalidOperationException("NumberOfEntries key missing or not a valid integer.");
            }

            for (int i = 1; i <= numberOfEntries; i++)
            {
                string fileKey   = string.Format(CultureInfo.InvariantCulture, "File{0}", i);
                string titleKey  = string.Format(CultureInfo.InvariantCulture, "Title{0}", i);
                string lengthKey = string.Format(CultureInfo.InvariantCulture, "Length{0}", i);

                if (!playlistEntries.ContainsKey(fileKey))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Missing file key: {0}", fileKey));
                }

                int lengthInSeconds = -1;

                if (playlistEntries.ContainsKey(lengthKey))
                {
                    // We don't really care if this works or not
                    int.TryParse(playlistEntries[lengthKey], out lengthInSeconds);
                }

                items.Add(new M3uPlaylistItem()
                {
                    DisplayName = playlistEntries.ContainsKey(titleKey) ? playlistEntries[titleKey] : string.Empty,
                    Length      = new TimeSpan(0, 0, lengthInSeconds),
                    Path        = playlistEntries[fileKey]
                });
            }

            return(playlist);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses the PLS file.
        /// </summary>
        /// <param name="textReader">TextReader representing a PLS playlist file.</param>
        /// <returns>A successfully parsed playlist.</returns>
        private IPlaylist Parse(TextReader textReader)
        {
            if (textReader == null)
            {
                throw new ArgumentNullException("textReader");
            }

            // Shoutcast.com PLS files are messed up.  The LengthX values are all Length1=-1 instead of LengthX=-1.
            IniParser iniFile = new IniParser(textReader, DuplicateNameHandling.Discard);
            Dictionary<string, Dictionary<string, string>> sections = iniFile.Sections;

            if (!sections.ContainsKey("playlist"))
            {
                throw new InvalidOperationException("playlist section not found");
            }

            PlsPlaylist playlist = new PlsPlaylist();
            ICollection<IPlaylistItem> items = playlist.Items;

            Dictionary<string, string> playlistEntries = sections["playlist"];

            int numberOfEntries;
            if ((!playlistEntries.ContainsKey("NumberOfEntries")) || (!int.TryParse(playlistEntries["NumberOfEntries"], out numberOfEntries)))
            {
                throw new InvalidOperationException("NumberOfEntries key missing or not a valid integer.");
            }

            for (int i = 1; i <= numberOfEntries; i++)
            {
                string fileKey = string.Format(CultureInfo.InvariantCulture, "File{0}", i);
                string titleKey = string.Format(CultureInfo.InvariantCulture, "Title{0}", i);
                string lengthKey = string.Format(CultureInfo.InvariantCulture, "Length{0}", i);

                if (!playlistEntries.ContainsKey(fileKey))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Missing file key: {0}", fileKey));
                }

                int lengthInSeconds = -1;

                if (playlistEntries.ContainsKey(lengthKey))
                {
                    // We don't really care if this works or not
                    int.TryParse(playlistEntries[lengthKey], out lengthInSeconds);
                }

                items.Add(new M3uPlaylistItem()
                {
                    DisplayName = playlistEntries.ContainsKey(titleKey) ? playlistEntries[titleKey] : string.Empty,
                    Length = new TimeSpan(0, 0, lengthInSeconds),
                    Path = playlistEntries[fileKey]
                });
            }

            return playlist;
        }