From d3acd04e66e6f45084f7d6f6a3fb6e26688e5b5b Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 12 Aug 2013 15:18:31 -0400 Subject: [PATCH] store chapters in xml --- .../Providers/BaseItemXmlParser.cs | 157 ++++++++++++++++-- .../MediaBrowser.Providers.csproj | 1 + .../Movies/MovieProviderFromXml.cs | 35 +--- .../Movies/MovieXmlParser.cs | 60 +++++++ .../Savers/EpisodeXmlSaver.cs | 6 +- .../Savers/MovieXmlSaver.cs | 7 +- .../Savers/XmlSaverHelpers.cs | 24 ++- .../TV/EpisodeProviderFromXml.cs | 9 +- MediaBrowser.Providers/TV/EpisodeXmlParser.cs | 34 +++- .../Providers/ProviderManager.cs | 89 ---------- 10 files changed, 278 insertions(+), 144 deletions(-) create mode 100644 MediaBrowser.Providers/Movies/MovieXmlParser.cs diff --git a/MediaBrowser.Controller/Providers/BaseItemXmlParser.cs b/MediaBrowser.Controller/Providers/BaseItemXmlParser.cs index 357b38ecf5..89bc9f2c28 100644 --- a/MediaBrowser.Controller/Providers/BaseItemXmlParser.cs +++ b/MediaBrowser.Controller/Providers/BaseItemXmlParser.cs @@ -1,5 +1,6 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; +using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using System; @@ -9,6 +10,7 @@ using System.IO; using System.Linq; using System.Text; using System.Threading; +using System.Threading.Tasks; using System.Xml; namespace MediaBrowser.Controller.Providers @@ -266,7 +268,10 @@ namespace MediaBrowser.Controller.Providers case "TagLines": { - FetchFromTaglinesNode(reader.ReadSubtree(), item); + using (var subtree = reader.ReadSubtree()) + { + FetchFromTaglinesNode(subtree, item); + } break; } @@ -591,28 +596,58 @@ namespace MediaBrowser.Controller.Providers break; case "Genres": - FetchFromGenresNode(reader.ReadSubtree(), item); - break; + { + using (var subtree = reader.ReadSubtree()) + { + FetchFromGenresNode(subtree, item); + } + break; + } case "Tags": - FetchFromTagsNode(reader.ReadSubtree(), item); - break; + { + using (var subtree = reader.ReadSubtree()) + { + FetchFromTagsNode(subtree, item); + } + break; + } case "Persons": - FetchDataFromPersonsNode(reader.ReadSubtree(), item); - break; + { + using (var subtree = reader.ReadSubtree()) + { + FetchDataFromPersonsNode(subtree, item); + } + break; + } case "ParentalRating": - FetchFromParentalRatingNode(reader.ReadSubtree(), item); - break; + { + using (var subtree = reader.ReadSubtree()) + { + FetchFromParentalRatingNode(subtree, item); + } + break; + } case "Studios": - FetchFromStudiosNode(reader.ReadSubtree(), item); - break; + { + using (var subtree = reader.ReadSubtree()) + { + FetchFromStudiosNode(subtree, item); + } + break; + } case "MediaInfo": - FetchFromMediaInfoNode(reader.ReadSubtree(), item); - break; + { + using (var subtree = reader.ReadSubtree()) + { + FetchFromMediaInfoNode(subtree, item); + } + break; + } default: reader.Skip(); @@ -636,8 +671,13 @@ namespace MediaBrowser.Controller.Providers switch (reader.Name) { case "Video": - FetchFromMediaInfoVideoNode(reader.ReadSubtree(), item); - break; + { + using (var subtree = reader.ReadSubtree()) + { + FetchFromMediaInfoVideoNode(subtree, item); + } + break; + } default: reader.Skip(); @@ -813,9 +853,12 @@ namespace MediaBrowser.Controller.Providers case "Person": case "Actor": { - foreach (var person in GetPersonsFromXmlNode(reader.ReadSubtree())) + using (var subtree = reader.ReadSubtree()) { - item.AddPerson(person); + foreach (var person in GetPersonsFromXmlNode(subtree)) + { + item.AddPerson(person); + } } break; } @@ -828,6 +871,86 @@ namespace MediaBrowser.Controller.Providers } } + protected async Task FetchChaptersFromXmlNode(Guid itemId, XmlReader reader, IItemRepository repository, CancellationToken cancellationToken) + { + using (reader) + { + var chapters = GetChaptersFromXmlNode(reader); + + await repository.SaveChapters(itemId, chapters, cancellationToken).ConfigureAwait(false); + } + } + + private IEnumerable GetChaptersFromXmlNode(XmlReader reader) + { + var chapters = new List(); + + reader.MoveToContent(); + + while (reader.Read()) + { + if (reader.NodeType == XmlNodeType.Element) + { + switch (reader.Name) + { + case "Chapter": + { + using (var subtree = reader.ReadSubtree()) + { + chapters.Add(GetChapterInfoFromXmlNode(subtree)); + } + break; + } + + default: + reader.Skip(); + break; + } + } + } + + return chapters; + } + + private ChapterInfo GetChapterInfoFromXmlNode(XmlReader reader) + { + var chapter = new ChapterInfo(); + + reader.MoveToContent(); + + while (reader.Read()) + { + if (reader.NodeType == XmlNodeType.Element) + { + switch (reader.Name) + { + case "StartPositionMs": + { + var val = reader.ReadElementContentAsString(); + + var ms = long.Parse(val, _usCulture); + + chapter.StartPositionTicks = TimeSpan.FromMilliseconds(ms).Ticks; + + break; + } + + case "Name": + { + chapter.Name = reader.ReadElementContentAsString(); + break; + } + + default: + reader.Skip(); + break; + } + } + } + + return chapter; + } + /// /// Fetches from studios node. /// diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj index ad517cefd9..806a2f1137 100644 --- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj +++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj @@ -58,6 +58,7 @@ + diff --git a/MediaBrowser.Providers/Movies/MovieProviderFromXml.cs b/MediaBrowser.Providers/Movies/MovieProviderFromXml.cs index 63b1bdb039..d6ab77881a 100644 --- a/MediaBrowser.Providers/Movies/MovieProviderFromXml.cs +++ b/MediaBrowser.Providers/Movies/MovieProviderFromXml.cs @@ -1,9 +1,9 @@ using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; +using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Logging; -using MediaBrowser.Providers.Music; using System; using System.IO; using System.Threading; @@ -17,10 +17,12 @@ namespace MediaBrowser.Providers.Movies public class MovieProviderFromXml : BaseMetadataProvider { internal static MovieProviderFromXml Current { get; private set; } - - public MovieProviderFromXml(ILogManager logManager, IServerConfigurationManager configurationManager) + private readonly IItemRepository _itemRepo; + + public MovieProviderFromXml(ILogManager logManager, IServerConfigurationManager configurationManager, IItemRepository itemRepo) : base(logManager, configurationManager) { + _itemRepo = itemRepo; Current = this; } @@ -94,30 +96,9 @@ namespace MediaBrowser.Providers.Movies try { - var movie = item as Movie; - - if (movie != null) - { - new BaseItemXmlParser(Logger).Fetch(movie, path, cancellationToken); - } - else - { - var musicVideo = item as MusicVideo; - - if (musicVideo != null) - { - new MusicVideoXmlParser(Logger).Fetch(musicVideo, path, cancellationToken); - } - else - { - var trailer = item as Trailer; - - if (trailer != null) - { - new BaseItemXmlParser(Logger).Fetch(trailer, path, cancellationToken); - } - } - } + var video = (Video) item; + + await new MovieXmlParser(Logger, _itemRepo).FetchAsync(video, path, cancellationToken).ConfigureAwait(false); } finally { diff --git a/MediaBrowser.Providers/Movies/MovieXmlParser.cs b/MediaBrowser.Providers/Movies/MovieXmlParser.cs new file mode 100644 index 0000000000..e397e1d20c --- /dev/null +++ b/MediaBrowser.Providers/Movies/MovieXmlParser.cs @@ -0,0 +1,60 @@ +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Persistence; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Logging; +using System.Threading; +using System.Threading.Tasks; +using System.Xml; + +namespace MediaBrowser.Providers.Movies +{ + /// + /// Class EpisodeXmlParser + /// + public class MovieXmlParser : BaseItemXmlParser