Made xml fetching task-based

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-08-19 12:54:41 -04:00
parent c658ea7feb
commit 874469d476
5 changed files with 50 additions and 57 deletions

View File

@ -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);
}
}
}
}

View File

@ -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
/// <summary>
/// Fetches metadata for an item from one xml file
/// </summary>
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);
}
});
}
/// <summary>

View File

@ -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<Movie>().Fetch(item as Movie, metadataFile.Value.Key);
}
});
if (metadataFile.HasValue)
{
await new BaseItemXmlParser<Movie>().Fetch(item as Movie, metadataFile.Value.Key);
}
}
}
}

View File

@ -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);
}
}
}

View File

@ -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<Series>().Fetch(item as Series, metadataFile.Value.Key);
}
});
if (metadataFile.HasValue)
{
await new SeriesXmlParser().Fetch(item as Series, metadataFile.Value.Key);
}
}
}
}