From 874469d47633851f4da75ad8640bc47dcfa810ce Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Sun, 19 Aug 2012 12:54:41 -0400 Subject: [PATCH] Made xml fetching task-based --- .../Providers/FolderProviderFromXml.cs | 15 +++---- .../Xml/BaseItemXmlParser.cs | 43 +++++++++++-------- .../Providers/MovieProviderFromXml.cs | 15 +++---- .../Providers/EpisodeProviderFromXml.cs | 17 +++----- .../Providers/SeriesProviderFromXml.cs | 17 +++----- 5 files changed, 50 insertions(+), 57 deletions(-) diff --git a/MediaBrowser.Controller/Providers/FolderProviderFromXml.cs b/MediaBrowser.Controller/Providers/FolderProviderFromXml.cs index 5ba02b38d..14067dd20 100644 --- a/MediaBrowser.Controller/Providers/FolderProviderFromXml.cs +++ b/MediaBrowser.Controller/Providers/FolderProviderFromXml.cs @@ -14,17 +14,14 @@ namespace MediaBrowser.Controller.Providers return item is Folder; } - public override Task Fetch(BaseItem item, ItemResolveEventArgs args) + public async override Task Fetch(BaseItem item, ItemResolveEventArgs args) { - return Task.Run(() => - { - var metadataFile = args.GetFileByName("folder.xml"); + var metadataFile = args.GetFileByName("folder.xml"); - if (metadataFile.HasValue) - { - new FolderXmlParser().Fetch(item as Folder, metadataFile.Value.Key); - } - }); + if (metadataFile.HasValue) + { + await new FolderXmlParser().Fetch(item as Folder, metadataFile.Value.Key); + } } } } diff --git a/MediaBrowser.Controller/Xml/BaseItemXmlParser.cs b/MediaBrowser.Controller/Xml/BaseItemXmlParser.cs index eabbae869..0e514b8b2 100644 --- a/MediaBrowser.Controller/Xml/BaseItemXmlParser.cs +++ b/MediaBrowser.Controller/Xml/BaseItemXmlParser.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Xml; using MediaBrowser.Model.Entities; +using System.Threading.Tasks; namespace MediaBrowser.Controller.Xml { @@ -16,33 +17,37 @@ namespace MediaBrowser.Controller.Xml /// /// Fetches metadata for an item from one xml file /// - public virtual void Fetch(T item, string metadataFile) + public Task Fetch(T item, string metadataFile) { - // Use XmlReader for best performance - using (XmlReader reader = XmlReader.Create(metadataFile)) + // Wrapping this for now until I have a chance to async the whole process + return Task.Run(() => { - reader.MoveToContent(); - - // Loop through each element - while (reader.Read()) + // Use XmlReader for best performance + using (XmlReader reader = XmlReader.Create(metadataFile)) { - if (reader.NodeType == XmlNodeType.Element) + reader.MoveToContent(); + + // Loop through each element + while (reader.Read()) { - FetchDataFromXmlNode(reader, item); + if (reader.NodeType == XmlNodeType.Element) + { + FetchDataFromXmlNode(reader, item); + } } } - } - // If dates weren't supplied in metadata, use values from the xml file - if (item.DateCreated == DateTime.MinValue) - { - item.DateCreated = File.GetCreationTime(metadataFile); - } + // If dates weren't supplied in metadata, use values from the xml file + if (item.DateCreated == DateTime.MinValue) + { + item.DateCreated = File.GetCreationTime(metadataFile); + } - if (item.DateModified == DateTime.MinValue) - { - item.DateModified = File.GetLastWriteTime(metadataFile); - } + if (item.DateModified == DateTime.MinValue) + { + item.DateModified = File.GetLastWriteTime(metadataFile); + } + }); } /// diff --git a/MediaBrowser.Movies/Providers/MovieProviderFromXml.cs b/MediaBrowser.Movies/Providers/MovieProviderFromXml.cs index 238e1bf76..4e0bacb05 100644 --- a/MediaBrowser.Movies/Providers/MovieProviderFromXml.cs +++ b/MediaBrowser.Movies/Providers/MovieProviderFromXml.cs @@ -16,17 +16,14 @@ namespace MediaBrowser.Movies.Providers return item is Movie; } - public override Task Fetch(BaseItem item, ItemResolveEventArgs args) + public async override Task Fetch(BaseItem item, ItemResolveEventArgs args) { - return Task.Run(() => - { - var metadataFile = args.GetFileByName("movie.xml"); + var metadataFile = args.GetFileByName("movie.xml"); - if (metadataFile.HasValue) - { - new BaseItemXmlParser().Fetch(item as Movie, metadataFile.Value.Key); - } - }); + if (metadataFile.HasValue) + { + await new BaseItemXmlParser().Fetch(item as Movie, metadataFile.Value.Key); + } } } } diff --git a/MediaBrowser.TV/Providers/EpisodeProviderFromXml.cs b/MediaBrowser.TV/Providers/EpisodeProviderFromXml.cs index 04fed2349..4c6c58459 100644 --- a/MediaBrowser.TV/Providers/EpisodeProviderFromXml.cs +++ b/MediaBrowser.TV/Providers/EpisodeProviderFromXml.cs @@ -19,21 +19,18 @@ namespace MediaBrowser.TV.Providers return item is Episode; } - public override Task Fetch(BaseItem item, ItemResolveEventArgs args) + public async override Task Fetch(BaseItem item, ItemResolveEventArgs args) { - return Task.Run(() => - { - string metadataFolder = Path.Combine(args.Parent.Path, "metadata"); + string metadataFolder = Path.Combine(args.Parent.Path, "metadata"); - string episodeFileName = Path.GetFileName(item.Path); + string episodeFileName = Path.GetFileName(item.Path); - string metadataFile = Path.Combine(metadataFolder, Path.ChangeExtension(episodeFileName, ".xml")); + string metadataFile = Path.Combine(metadataFolder, Path.ChangeExtension(episodeFileName, ".xml")); - FetchMetadata(item as Episode, args.Parent as Season, metadataFile); - }); + await FetchMetadata(item as Episode, args.Parent as Season, metadataFile); } - private void FetchMetadata(Episode item, Season season, string metadataFile) + private async Task FetchMetadata(Episode item, Season season, string metadataFile) { if (season == null) { @@ -52,7 +49,7 @@ namespace MediaBrowser.TV.Providers } } - new EpisodeXmlParser().Fetch(item, metadataFile); + await new EpisodeXmlParser().Fetch(item, metadataFile); } } } diff --git a/MediaBrowser.TV/Providers/SeriesProviderFromXml.cs b/MediaBrowser.TV/Providers/SeriesProviderFromXml.cs index 44a0dbc9f..7adfb0483 100644 --- a/MediaBrowser.TV/Providers/SeriesProviderFromXml.cs +++ b/MediaBrowser.TV/Providers/SeriesProviderFromXml.cs @@ -2,9 +2,9 @@ using System.Threading.Tasks; using MediaBrowser.Controller.Events; using MediaBrowser.Controller.Providers; -using MediaBrowser.Controller.Xml; using MediaBrowser.Model.Entities; using MediaBrowser.TV.Entities; +using MediaBrowser.TV.Metadata; namespace MediaBrowser.TV.Providers { @@ -16,17 +16,14 @@ namespace MediaBrowser.TV.Providers return item is Series; } - public override Task Fetch(BaseItem item, ItemResolveEventArgs args) + public async override Task Fetch(BaseItem item, ItemResolveEventArgs args) { - return Task.Run(() => - { - var metadataFile = args.GetFileByName("series.xml"); + var metadataFile = args.GetFileByName("series.xml"); - if (metadataFile.HasValue) - { - new BaseItemXmlParser().Fetch(item as Series, metadataFile.Value.Key); - } - }); + if (metadataFile.HasValue) + { + await new SeriesXmlParser().Fetch(item as Series, metadataFile.Value.Key); + } } } }