コード例 #1
0
        public void Create_CreatePlaylistAndCompareWithFile_Equal()
        {
            ZplContent  content  = new ZplContent();
            ZplPlaylist playlist = new ZplPlaylist();

            playlist.Title = "Eurowizja";
            playlist.PlaylistEntries.Add(new ZplPlaylistEntry()
            {
                AlbumArtist = null,
                AlbumTitle  = "",
                Duration    = TimeSpan.FromMilliseconds(185364),
                Path        = @"D:\Muzyka\Eurowizja\Eurowizja 2014\Can-linn & Kasey Smith - Heartbeat(Irlandia).mp3",
                TrackArtist = "Can-linn & Kasey Smith",
                TrackTitle  = "Heartbeat"
            });
            playlist.PlaylistEntries.Add(new ZplPlaylistEntry()
            {
                AlbumArtist = "Elaiza",
                AlbumTitle  = "Eurovision Song Contest 2014",
                Duration    = TimeSpan.Zero,
                Path        = @"D:\Muzyka\Eurowizja\Eurowizja 2014\Elaiza - Is It Right.mp3",
                TrackArtist = "Elaiza",
                TrackTitle  = "Is It Right"
            });

            string created  = content.Create(playlist);
            string fromFile = Helpers.Read("Playlist2.zpl");

            Assert.AreEqual(created, fromFile);
        }
コード例 #2
0
        private IEnumerable <LinkedChild> GetZplItems(Stream stream)
        {
            var content  = new ZplContent();
            var playlist = content.GetFromStream(stream);

            return(playlist.PlaylistEntries.Select(i => new LinkedChild
            {
                Path = i.Path,
                Type = LinkedChildType.Manual
            }));
        }
コード例 #3
0
        public void GetFromStream_ReadEmptyPlaylistAndCompareWithObject_Equal()
        {
            ZplContent  content  = new ZplContent();
            ZplPlaylist playlist = new ZplPlaylist();

            playlist.Title = "";
            var stream = Helpers.ReadStream("Empty.zpl");
            var file   = content.GetFromStream(stream);

            Assert.AreEqual(playlist.Title, file.Title);
            Assert.AreEqual(playlist.PlaylistEntries.Count, file.PlaylistEntries.Count);
            stream.Dispose();
        }
コード例 #4
0
        public void GetFromStream_ReadPlaylistAndCompareWithObject_Equal()
        {
            ZplContent  content  = new ZplContent();
            ZplPlaylist playlist = new ZplPlaylist();

            playlist.Title = "Eurowizja";
            playlist.PlaylistEntries.Add(new ZplPlaylistEntry()
            {
                AlbumArtist = null,
                AlbumTitle  = "",
                Duration    = TimeSpan.FromMilliseconds(185364),
                Path        = @"D:\Muzyka\Eurowizja\Eurowizja 2014\Can-linn & Kasey Smith - Heartbeat(Irlandia).mp3",
                TrackArtist = "Can-linn & Kasey Smith",
                TrackTitle  = "Heartbeat"
            });
            playlist.PlaylistEntries.Add(new ZplPlaylistEntry()
            {
                AlbumArtist = "Elaiza",
                AlbumTitle  = "Eurovision Song Contest 2014",
                Path        = @"D:\Muzyka\Eurowizja\Eurowizja 2014\Elaiza - Is It Right.mp3",
                TrackArtist = "Elaiza",
                TrackTitle  = "Is It Right"
            });

            var stream = Helpers.ReadStream("Playlist.zpl");
            var file   = content.GetFromStream(stream);

            stream.Dispose();
            Assert.AreEqual(playlist.PlaylistEntries.Count, file.PlaylistEntries.Count);
            Assert.AreEqual(playlist.Title, file.Title);

            Assert.AreEqual(playlist.PlaylistEntries[0].AlbumArtist, file.PlaylistEntries[0].AlbumArtist);
            Assert.AreEqual(playlist.PlaylistEntries[1].AlbumArtist, file.PlaylistEntries[1].AlbumArtist);

            Assert.AreEqual(String.IsNullOrEmpty(playlist.PlaylistEntries[0].AlbumTitle), String.IsNullOrEmpty(file.PlaylistEntries[0].AlbumTitle));
            Assert.AreEqual(playlist.PlaylistEntries[1].AlbumTitle, file.PlaylistEntries[1].AlbumTitle);

            Assert.AreEqual(playlist.PlaylistEntries[0].TrackArtist, file.PlaylistEntries[0].TrackArtist);
            Assert.AreEqual(playlist.PlaylistEntries[1].TrackArtist, file.PlaylistEntries[1].TrackArtist);

            Assert.AreEqual(playlist.PlaylistEntries[0].TrackTitle, file.PlaylistEntries[0].TrackTitle);
            Assert.AreEqual(playlist.PlaylistEntries[1].TrackTitle, file.PlaylistEntries[1].TrackTitle);

            Assert.AreEqual(playlist.PlaylistEntries[0].Path, file.PlaylistEntries[0].Path);
            Assert.AreEqual(playlist.PlaylistEntries[1].Path, file.PlaylistEntries[1].Path);
            stream.Dispose();
        }
コード例 #5
0
            public static Playlist Read(string playlistPath)
            {
                if (!File.Exists(playlistPath))
                {
                    return(null);
                }
                var ext            = Path.GetExtension(playlistPath).ToLower();
                var playlistStream = new FileStream(playlistPath, FileMode.Open);
                IEnumerable <BasePlaylistEntry> playlistEntries;

                switch (ext)
                {
                case ".pls":
                    playlistEntries = new PlsContent().GetFromStream(playlistStream).PlaylistEntries;
                    break;

                case ".m3u":
                    playlistEntries = new M3uContent().GetFromStream(playlistStream).PlaylistEntries;
                    break;

                case ".m3u8":
                    playlistEntries = new M3u8Content().GetFromStream(playlistStream).PlaylistEntries;
                    break;

                case ".wpl":
                    playlistEntries = new WplContent().GetFromStream(playlistStream).PlaylistEntries;
                    break;

                case ".zpl":
                    playlistEntries = new ZplContent().GetFromStream(playlistStream).PlaylistEntries;
                    break;

                default:
                    throw new InvalidOperationException("unknown playlist format");
                }
                playlistStream.Close();
                if (playlistEntries.Count() == 0)
                {
                    return(null);
                }
                return(new Playlist(playlistEntries.Select(e => e.Path)));
            }
コード例 #6
0
ファイル: MediaResolver.cs プロジェクト: Tommmciu/TS3AudioBot
        private R <Playlist, LocalStr> GetPlaylistContent(Stream stream, string url, Uid owner, string mime = null)
        {
            string name = null;
            List <PlaylistItem> items;

            mime = mime.ToLowerInvariant();
            url  = url.ToLowerInvariant();
            string anyId = mime ?? url;

            switch (anyId)
            {
            case ".m3u":
            {
                var parser = new M3uContent();
                var list   = parser.GetFromStream(stream);

                items = new List <PlaylistItem>(
                    from e in list.PlaylistEntries
                    select new PlaylistItem(new AudioResource(e.Path, e.Title, ResolverFor)));
                break;
            }

            case ".m3u8":
            case "application/mpegurl":
            case "application/x-mpegurl":
            case "audio/mpegurl":
            case "audio/x-mpegurl":
            case "application/vnd.apple.mpegurl":
            case "application/vnd.apple.mpegurl.audio":
            {
                var parser = new M3u8Content();
                var list   = parser.GetFromStream(stream);

                items = new List <PlaylistItem>(
                    from e in list.PlaylistEntries
                    select new PlaylistItem(new AudioResource(e.Path, e.Title, ResolverFor)));
                break;
            }

            case ".pls":
            case "audio/x-scpls":
            case "application/x-scpls":
            case "application/pls+xml":
            {
                var parser = new PlsContent();
                var list   = parser.GetFromStream(stream);

                items = new List <PlaylistItem>(
                    from e in list.PlaylistEntries
                    select new PlaylistItem(new AudioResource(e.Path, e.Title, ResolverFor)));
                break;
            }

            case ".wpl":
            {
                var parser = new WplContent();
                var list   = parser.GetFromStream(stream);

                items = new List <PlaylistItem>(
                    from e in list.PlaylistEntries
                    select new PlaylistItem(new AudioResource(e.Path, e.TrackTitle, ResolverFor)));
                name = list.Title;
                break;
            }

            case ".zpl":
            {
                var parser = new ZplContent();
                var list   = parser.GetFromStream(stream);

                items = new List <PlaylistItem>(
                    from e in list.PlaylistEntries
                    select new PlaylistItem(new AudioResource(e.Path, e.TrackTitle, ResolverFor)));
                name = list.Title;
                break;
            }

            // ??
            case "application/jspf+json":
            // ??
            case "application/xspf+xml":
            default:
                return(new LocalStr(strings.error_media_file_not_found));                // TODO Loc "media not supported"
            }

            if (string.IsNullOrEmpty(name))
            {
                var index = url.LastIndexOfAny(new[] { '\\', '/' });
                name = index >= 0 ? url.Substring(index) : url;
            }
            return(new Playlist(name, owner, Enumerable.Empty <Uid>(), items));
        }
コード例 #7
0
        private void SavePlaylistFile(Playlist item)
        {
            // this is probably best done as a metadata provider
            // saving a file over itself will require some work to prevent this from happening when not needed
            var playlistPath = item.Path;
            var extension    = Path.GetExtension(playlistPath);

            if (string.Equals(".wpl", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new WplPlaylist();
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new WplPlaylistEntry()
                    {
                        Path       = NormalizeItemPath(playlistPath, child.Path),
                        TrackTitle = child.Name,
                        AlbumTitle = child.Album
                    };

                    var hasAlbumArtist = child as IHasAlbumArtist;
                    if (hasAlbumArtist != null)
                    {
                        entry.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
                    }

                    var hasArtist = child as IHasArtist;
                    if (hasArtist != null)
                    {
                        entry.TrackArtist = hasArtist.Artists.FirstOrDefault();
                    }

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }

                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new WplContent().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }

            if (string.Equals(".zpl", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new ZplPlaylist();
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new ZplPlaylistEntry()
                    {
                        Path       = NormalizeItemPath(playlistPath, child.Path),
                        TrackTitle = child.Name,
                        AlbumTitle = child.Album
                    };

                    var hasAlbumArtist = child as IHasAlbumArtist;
                    if (hasAlbumArtist != null)
                    {
                        entry.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
                    }

                    var hasArtist = child as IHasArtist;
                    if (hasArtist != null)
                    {
                        entry.TrackArtist = hasArtist.Artists.FirstOrDefault();
                    }

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }
                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new ZplContent().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }

            if (string.Equals(".m3u", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new M3uPlaylist();
                playlist.IsExtended = true;
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new M3uPlaylistEntry()
                    {
                        Path  = NormalizeItemPath(playlistPath, child.Path),
                        Title = child.Name,
                        Album = child.Album
                    };

                    var hasAlbumArtist = child as IHasAlbumArtist;
                    if (hasAlbumArtist != null)
                    {
                        entry.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
                    }

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }

                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new M3uContent().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }

            if (string.Equals(".m3u8", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new M3uPlaylist();
                playlist.IsExtended = true;
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new M3uPlaylistEntry()
                    {
                        Path  = NormalizeItemPath(playlistPath, child.Path),
                        Title = child.Name,
                        Album = child.Album
                    };

                    var hasAlbumArtist = child as IHasAlbumArtist;
                    if (hasAlbumArtist != null)
                    {
                        entry.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
                    }

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }

                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new M3u8Content().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }

            if (string.Equals(".pls", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new PlsPlaylist();
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new PlsPlaylistEntry()
                    {
                        Path  = NormalizeItemPath(playlistPath, child.Path),
                        Title = child.Name
                    };

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Length = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }

                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new PlsContent().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }
        }
コード例 #8
0
        /// <summary>
        /// Read a playlist file from a give path and returns a PlaylistFile (Data Model) complete with absolute paths
        /// well as reading the meta
        /// </summary>
        /// <param name="path">Path to playlist file</param>
        /// <returns>PlaylistFile (Data Model)</returns>
        public static PlaylistFile ReadPlaylistFile(string path)
        {
            var playlistFile = new PlaylistFile();

            try
            {
                string playlistExtension = Path.GetExtension(path).ToLower();
                var    fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                using (var sr = new StreamReader(fs))
                {
                    switch (playlistExtension)
                    {
                    case ".wpl":
                        var content_wpl  = new WplContent();
                        var playlist_wpl = content_wpl.GetFromStream(sr.BaseStream);
                        playlistFile.Title = playlist_wpl.Title.Trim();
                        break;

                    case ".zpl":
                        var content_zpl  = new ZplContent();
                        var playlist_zpl = content_zpl.GetFromStream(sr.BaseStream);
                        playlistFile.Title = playlist_zpl.Title.Trim();
                        break;

                    default:
                        playlistFile.Title = Path.GetFileNameWithoutExtension(path).Trim();
                        break;
                    }

                    if (string.IsNullOrEmpty(playlistFile.Title))
                    {
                        playlistFile.Title = Path.GetFileNameWithoutExtension(path).Trim();
                    }

                    sr.BaseStream.Position = 0;
                    var parser = PlaylistParserFactory.GetPlaylistParser(playlistExtension);

                    if (sr.BaseStream.Length == 0) // empty playlist
                    {
                        throw new ApplicationException("Empty Playlist");
                    }

                    var playlist = parser.GetFromStream(sr.BaseStream);
                    var paths    = playlist.GetTracksPaths();

                    if ((paths == null || paths.Count == 0) && playlistExtension.In(".m3u", ".m3u8"))
                    {
                        paths = M3uPlaylist.GetPlaylistFromCorruptM3u(path);
                    }

                    playlistFile.Path             = path;
                    playlistFile.LastModifiedDate = new FileInfo(path).LastWriteTime;

                    var musicFileRepo = new MusicFileRepo();

                    foreach (string musicFilePath in paths)
                    {
                        try
                        {
                            string absolutePath = Utils.IsAbsolutePath(musicFilePath)
                               ? musicFilePath
                               : Utils.MakeAbsolutePath(Path.GetDirectoryName(path), musicFilePath);

                            if (absolutePath.StartsWith("file://"))
                            {
                                absolutePath = new Uri(absolutePath).LocalPath;
                            }

                            if (Path.GetExtension(absolutePath).ToLower().In(Global.SupportedMusicFiles) &&
                                File.Exists(absolutePath))
                            {
                                playlistFile.PlaylistItems.Add(new PlaylistFile.PlaylistFileItem
                                {
                                    Path = absolutePath
                                });
                            }
                        }
                        catch (Exception e)
                        {
                            // Invalid path - Ignore - Don't add
                            var _ = e;
                            Logger.LogWarning(
                                "ReadPlaylistFile",
                                "Invalid path detected in playlist file and will be ignored: " + path);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogError(
                    "ReadPlaylistFile",
                    "Error reading playlist file: " + path + ": " + e.Message);
            }

            return(playlistFile);
        }
コード例 #9
0
        /// <summary>
        /// Read a playlist file from a give path and returns a PlaylistFile (Data Model) complete with absolute paths
        /// well as reading the meta
        /// </summary>
        /// <param name="path">Path to playlist file</param>
        /// <returns>PlaylistFile (Data Model)</returns>
        public static PlaylistFile ReadPlaylistFile(string path)
        {
            var    playlistFile      = new PlaylistFile();
            string playlistExtension = Path.GetExtension(path).ToLower();

            var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            using (var sr = new StreamReader(fs))
            {
                switch (playlistExtension)
                {
                case ".wpl":
                    var content_wpl  = new WplContent();
                    var playlist_wpl = content_wpl.GetFromStream(sr.BaseStream);
                    playlistFile.Title = playlist_wpl.Title.Trim();
                    break;

                case ".zpl":
                    var content_zpl  = new ZplContent();
                    var playlist_zpl = content_zpl.GetFromStream(sr.BaseStream);
                    playlistFile.Title = playlist_zpl.Title.Trim();
                    break;

                default:
                    playlistFile.Title = Path.GetFileNameWithoutExtension(path).Trim();
                    break;
                }

                if (string.IsNullOrEmpty(playlistFile.Title))
                {
                    playlistFile.Title = Path.GetFileNameWithoutExtension(path).Trim();
                }

                sr.BaseStream.Position = 0;
                var parser   = PlaylistParserFactory.GetPlaylistParser(playlistExtension);
                var playlist = parser.GetFromStream(sr.BaseStream);
                var paths    = playlist.GetTracksPaths();

                if ((paths == null || paths.Count == 0) && playlistExtension.In(".m3u", ".m3u8"))
                {
                    paths = M3uPlaylist.GetPlaylistFromCorruptM3u(path);
                }

                playlistFile.Path             = path;
                playlistFile.LastModifiedDate = new FileInfo(path).LastWriteTime;

                var musicFileRepo = new MusicFileRepo();

                foreach (string musicFilePath in paths)
                {
                    string absolutePath = Utils.IsAbsolutePath(musicFilePath)
                                                ? musicFilePath
                                                : Utils.MakeAbsolutePath(Path.GetDirectoryName(path), musicFilePath);

                    if (absolutePath.StartsWith("file://"))
                    {
                        absolutePath = new Uri(absolutePath).LocalPath;
                    }

                    if (Path.GetExtension(absolutePath).ToLower().In(Global.SupportedMusicFiles) &&
                        File.Exists(absolutePath))
                    {
                        playlistFile.PlaylistItems.Add(new PlaylistFile.PlaylistFileItem
                        {
                            Path = absolutePath
                        });
                    }
                }
            }

            return(playlistFile);
        }