示例#1
0
        public static IEnumerable <Playlist> ReadPlaylists(Stream stream)
        {
            IEnumerable <Song> songs = ReadSongs(stream);

            stream.Position = 0;

            var playlists = XDocument.Load(stream)
                            .Descendants("Root")
                            .Descendants("Playlists")
                            .Elements("Playlist")
                            .Select
                            (
                playlist =>
                new
            {
                Name    = playlist.Attribute("Name").Value,
                Entries = playlist
                          .Descendants("Entries")
                          .Elements("Entry")
                          .Select
                          (
                    entry =>
                {
                    var type = entry.Attribute("Type").Value == "Local" ? typeof(LocalSong) : typeof(YoutubeSong);

                    TimeSpan?duration = null;
                    string title      = null;

                    if (type == typeof(YoutubeSong))
                    {
                        duration = TimeSpan.FromTicks(Int64.Parse(entry.Attribute("Duration").Value));
                        title    = entry.Attribute("Title").Value;
                    }

                    return(new
                    {
                        Path = entry.Attribute("Path").Value,
                        Type = type,
                        Duration = duration,
                        Title = title
                    });
                }
                          )
            }
                            );

            return(playlists.Select
                   (
                       p =>
            {
                var playlist = new Playlist(p.Name);

                var s = p.Entries
                        .Select
                        (
                    entry =>
                {
                    if (entry.Type == typeof(YoutubeSong))
                    {
                        return new YoutubeSong(entry.Path, AudioType.Mp3, entry.Duration.Value, CoreSettings.Default.StreamYoutube)
                        {
                            Title = entry.Title
                        };
                    }

                    return songs.First(song => song.OriginalPath == entry.Path);
                }
                        );

                playlist.AddSongs(s);

                return playlist;
            }
                   )
                   .ToList());
        }
示例#2
0
        public static IReadOnlyList <Playlist> DeserializePlaylists(JObject json, IReadOnlyList <Song> songCache = null)
        {
            IEnumerable <Song> songs = songCache ?? DeserializeSongs(json);

            var playlists = json["playlists"].Select(playlist => new
            {
                Name    = playlist["name"].ToObject <string>(),
                Entries = playlist["entries"].Select(entry =>
                {
                    var typeString = entry["type"].ToObject <string>();
                    Type type;

                    if (typeString == "Local")
                    {
                        type = typeof(LocalSong);
                    }

                    else if (typeString == "YouTube")
                    {
                        type = typeof(YoutubeSong);
                    }

                    else if (typeString == "SoundCloud")
                    {
                        type = typeof(SoundCloudSong);
                    }

                    else
                    {
                        throw new NotImplementedException("Type not implemented.");
                    }

                    TimeSpan?duration = null;
                    string title      = null;

                    if (type == typeof(YoutubeSong) || type == typeof(SoundCloudSong))
                    {
                        duration = TimeSpan.FromTicks(entry["duration"].ToObject <long>());
                        title    = entry["title"].ToObject <string>();
                    }

                    string artist       = null;
                    string playbackPath = null;

                    if (type == typeof(SoundCloudSong))
                    {
                        artist       = entry["artist"].ToObject <string>();
                        playbackPath = entry["playbackPath"].ToObject <string>();
                    }

                    return(new
                    {
                        OriginalPath = entry["path"].ToObject <string>(),
                        PlaybackPath = playbackPath,
                        Type = type,
                        Duration = duration,
                        Title = title,
                        Artist = artist
                    });
                })
            });

            return(playlists.Select(p =>
            {
                var playlist = new Playlist(p.Name);

                var s = p.Entries.Select(entry =>
                {
                    if (entry.Type == typeof(YoutubeSong))
                    {
                        return new YoutubeSong(entry.OriginalPath, entry.Duration.Value)
                        {
                            Title = entry.Title
                        };
                    }

                    if (entry.Type == typeof(SoundCloudSong))
                    {
                        return new SoundCloudSong(entry.OriginalPath, entry.PlaybackPath)
                        {
                            Artist = entry.Artist,
                            Title = entry.Title,
                            Duration = entry.Duration.Value
                        };
                    }

                    if (entry.Type == typeof(LocalSong))
                    {
                        // We search for the path locally, so we don't have to serialize data about
                        // local songs
                        return songs.First(song => song.OriginalPath == entry.OriginalPath);
                    }

                    throw new NotImplementedException();
                });

                playlist.AddSongs(s);

                return playlist;
            }).ToList());
        }