Example #1
0
        /// <summary>
        /// Parses an M3U playlist and returns the tracks of it.
        /// </summary>
        /// <param name="reader">The data stream of the playlist</param>
        /// <param name="path">The relative path of the tracks in the playlist</param>
        /// <returns>A collection of tracks represented by the playlist</returns>
        private static ObservableCollection <TrackData> ParseM3U(StreamReader reader, string path = "")
        {
            ObservableCollection <TrackData> ret = new ObservableCollection <TrackData>();
            string line;
            bool   ext = false;
            string inf = "";
            int    nr  = 0;

            while ((line = reader.ReadLine()) != null)
            {
                nr++;
                if (line == "#EXTM3U")
                {
                    ext = true;
                }
                else if (ext && line.StartsWith("#EXTINF:"))
                {
                    inf = line.Substring(8);
                }
                else if (line.StartsWith("#") || line == "")
                {
                    continue;
                }
                else
                {
                    string    p    = line;
                    TrackType type = MediaManager.GetType(p);
                    TrackData track;

                    switch (type)
                    {
                    case TrackType.File:

                        if (!File.Exists(p) && File.Exists(Path.Combine(path, p)))
                        {
                            p = Path.Combine(path, p);
                        }

                        if (File.Exists(path))
                        {
                            string length = "";
                            string artist = "";
                            string title  = "";
                            if (inf != "")
                            {
                                if (!inf.Contains(','))
                                {
                                    U.L(LogLevel.Warning, "PLAYLIST", "Bad format on line "
                                        + nr + ": expecting ','");
                                    continue;
                                }
                                string[] split = inf.Split(',');
                                length = split[0];
                                if (split[1].Contains('-'))
                                {
                                    artist = split[1].Split('-')[0];
                                    title  = split[1].Split('-')[1];
                                }
                                else
                                {
                                    title = split[1];
                                }
                            }
                            if (!FilesystemManager.PathIsAdded(path))
                            {
                                FilesystemManager.AddSource(p);
                            }
                            foreach (TrackData t in SettingsManager.FileTracks)
                            {
                                if (t.Path == path)
                                {
                                    if (!ret.Contains(t))
                                    {
                                        ret.Add(t);
                                    }
                                    break;
                                }
                            }
                            inf = "";
                        }
                        break;

                    case TrackType.WebRadio:
                        track = MediaManager.ParseURL(p);
                        if (track != null && !ret.Contains(track))
                        {
                            ret.Add(track);
                        }
                        break;

                    case TrackType.YouTube:
                        track = YouTubeManager.CreateTrack(p);
                        if (track != null && !ret.Contains(track))
                        {
                            ret.Add(track);
                        }
                        break;

                    case TrackType.SoundCloud:
                        track = SoundCloudManager.CreateTrack(p);
                        if (track != null && !ret.Contains(track))
                        {
                            ret.Add(track);
                        }
                        break;
                    }
                }
            }
            return(ret);
        }
Example #2
0
        /// <summary>
        /// Parses a PLS playlist and returns the tracks of it.
        /// </summary>
        /// <param name="reader">The data stream of the playlist</param>
        /// <param name="path">The relative path of the tracks in the playlist</param>
        /// <returns>A collection of tracks represented by the playlist</returns>
        private static ObservableCollection <TrackData> ParsePLS(StreamReader reader, string path = "")
        {
            ObservableCollection <TrackData> ret = new ObservableCollection <TrackData>();
            bool   hdr     = false;
            string version = "";
            int    noe     = 0;
            int    nr      = 0;
            string line;

            List <string> lines = new List <string>();

            while ((line = reader.ReadLine()) != null)
            {
                lines.Add(line);
                nr++;
                if (line == "[playlist]")
                {
                    hdr = true;
                }
                else if (!hdr)
                {
                    U.L(LogLevel.Warning, "PLAYLIST", "Bad format on line "
                        + nr + ": expecting '[playlist]'");
                }
                else if (line.StartsWith("NumberOfEntries="))
                {
                    noe = Convert.ToInt32(line.Split('=')[1]);
                }
                else if (line.StartsWith("Version="))
                {
                    version = line.Split('=')[1];
                }
            }

            if (!hdr)
            {
                U.L(LogLevel.Warning, "PLAYLIST", "No header found");
            }


            // It seems there's many Internet radios that doesn't specify a version,
            // so we can't be too picky about this one.
            //else if (version != "2")
            //    U.L(LogLevel.Warning, "PLAYLIST", "Unsupported version '" +
            //        version + "'");

            else
            {
                string[,] tracks = new string[noe, 3];
                nr = 0;
                foreach (string l in lines)
                {
                    if (l.StartsWith("File") || l.StartsWith("Title") || l.StartsWith("Length"))
                    {
                        int tmp   = 4;
                        int index = 0;
                        if (l.StartsWith("Title"))
                        {
                            tmp = 5; index = 1;
                        }
                        else if (l.StartsWith("Length"))
                        {
                            tmp = 6; index = 2;
                        }

                        string[] split  = l.Split('=');
                        int      number = Convert.ToInt32(split[0].Substring(tmp));

                        if (number > noe)
                        {
                            U.L(LogLevel.Warning, "PLAYLIST", "Bad format on line "
                                + nr + ": entry number is '" + number + "' but NumberOfEntries is '" + noe + "'");
                        }
                        else
                        {
                            tracks[number - 1, index] = split[1];
                        }
                    }
                    else if (!l.StartsWith("NumberOfEntries") && l != "[playlist]" && !l.StartsWith("Version="))
                    {
                        U.L(LogLevel.Warning, "PLAYLIST", "Bad format on line "
                            + nr + ": unexpected '" + l + "'");
                    }
                }
                for (int i = 0; i < noe; i++)
                {
                    string p = tracks[i, 0];

                    TrackType type = MediaManager.GetType(p);
                    TrackData track;

                    switch (type)
                    {
                    case TrackType.File:
                        if (!File.Exists(p) && File.Exists(Path.Combine(path, p)))
                        {
                            p = Path.Combine(path, p);
                        }

                        if (File.Exists(p))
                        {
                            if (!FilesystemManager.PathIsAdded(p))
                            {
                                FilesystemManager.AddSource(p);
                            }
                            foreach (TrackData t in SettingsManager.FileTracks)
                            {
                                if (t.Path == p)
                                {
                                    if (!ret.Contains(t))
                                    {
                                        ret.Add(t);
                                    }
                                    break;
                                }
                            }
                        }
                        break;

                    case TrackType.WebRadio:
                        track = MediaManager.ParseURL(p);
                        if (track != null && !ret.Contains(track))
                        {
                            if (String.IsNullOrWhiteSpace(track.Title))
                            {
                                track.Title = tracks[i, 1];
                            }
                            if (String.IsNullOrWhiteSpace(track.URL))
                            {
                                track.URL = p;
                            }
                            ret.Add(track);
                        }
                        break;

                    case TrackType.YouTube:
                        track = YouTubeManager.CreateTrack(p);
                        if (track != null && !ret.Contains(track))
                        {
                            ret.Add(track);
                        }
                        break;

                    case TrackType.SoundCloud:
                        track = SoundCloudManager.CreateTrack(p);
                        if (track != null && !ret.Contains(track))
                        {
                            ret.Add(track);
                        }
                        break;
                    }
                }
            }
            return(ret);
        }