update providers to use core interfaces

This commit is contained in:
Luke Pulverenti 2016-10-27 14:30:20 -04:00
parent 872aec9352
commit 099b4d3e5b
9 changed files with 219 additions and 137 deletions

View File

@ -70,23 +70,26 @@ namespace MediaBrowser.Providers.ImagesByName
.Replace("/", string.Empty);
}
public static IEnumerable<string> GetAvailableImages(string file)
public static IEnumerable<string> GetAvailableImages(string file, IFileSystem fileSystem)
{
using (var reader = new StreamReader(file))
using (var fileStream = fileSystem.GetFileStream(file, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
{
var lines = new List<string>();
while (!reader.EndOfStream)
using (var reader = new StreamReader(fileStream))
{
var text = reader.ReadLine();
var lines = new List<string>();
if (!string.IsNullOrWhiteSpace(text))
while (!reader.EndOfStream)
{
lines.Add(text);
}
}
var text = reader.ReadLine();
return lines;
if (!string.IsNullOrWhiteSpace(text))
{
lines.Add(text);
}
}
return lines;
}
}
}
}

View File

@ -174,14 +174,14 @@ namespace MediaBrowser.Providers.Manager
try
{
var currentFile = new FileInfo(currentPath);
var currentFile = _fileSystem.GetFileInfo(currentPath);
// This will fail if the file is hidden
if (currentFile.Exists)
{
if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
if (currentFile.IsHidden)
{
currentFile.Attributes &= ~FileAttributes.Hidden;
_fileSystem.SetHidden(currentFile.FullName, false);
}
_fileSystem.DeleteFile(currentFile.FullName);
@ -256,14 +256,14 @@ namespace MediaBrowser.Providers.Manager
_fileSystem.CreateDirectory(Path.GetDirectoryName(path));
// If the file is currently hidden we'll have to remove that or the save will fail
var file = new FileInfo(path);
var file = _fileSystem.GetFileInfo(path);
// This will fail if the file is hidden
if (file.Exists)
{
if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
if (file.IsHidden)
{
file.Attributes &= ~FileAttributes.Hidden;
_fileSystem.SetHidden(file.FullName, false);
}
}
@ -275,10 +275,7 @@ namespace MediaBrowser.Providers.Manager
if (_config.Configuration.SaveMetadataHidden)
{
file.Refresh();
// Add back the attribute
file.Attributes |= FileAttributes.Hidden;
_fileSystem.SetHidden(file.FullName, true);
}
}
finally

View File

@ -372,14 +372,14 @@ namespace MediaBrowser.Providers.Manager
}
// Delete the source file
var currentFile = new FileInfo(image.Path);
var currentFile = _fileSystem.GetFileInfo(image.Path);
// Deletion will fail if the file is hidden so remove the attribute first
if (currentFile.Exists)
{
if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
if (currentFile.IsHidden)
{
currentFile.Attributes &= ~FileAttributes.Hidden;
_fileSystem.SetHidden(currentFile.FullName, false);
}
_fileSystem.DeleteFile(currentFile.FullName);
@ -613,7 +613,7 @@ namespace MediaBrowser.Providers.Manager
{
try
{
if (item.GetImages(imageType).Any(i => new FileInfo(i.Path).Length == response.ContentLength.Value))
if (item.GetImages(imageType).Any(i => _fileSystem.GetFileInfo(i.Path).Length == response.ContentLength.Value))
{
response.Content.Dispose();
continue;

View File

@ -87,11 +87,11 @@ namespace MediaBrowser.Providers.MediaInfo
var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false);
File.Copy(tempFile, path, true);
_fileSystem.CopyFile(tempFile, path, true);
try
{
File.Delete(tempFile);
_fileSystem.DeleteFile(tempFile);
}
catch
{

View File

@ -118,7 +118,7 @@ namespace MediaBrowser.Providers.Movies
{
// No biggie. Don't blow up
}
catch (DirectoryNotFoundException)
catch (IOException)
{
// No biggie. Don't blow up
}

View File

@ -14,6 +14,7 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Providers.Movies
{
@ -21,11 +22,13 @@ namespace MediaBrowser.Providers.Movies
{
private readonly IJsonSerializer _jsonSerializer;
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
public MovieDbImageProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient)
public MovieDbImageProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, IFileSystem fileSystem)
{
_jsonSerializer = jsonSerializer;
_httpClient = httpClient;
_fileSystem = fileSystem;
}
public string Name
@ -196,7 +199,7 @@ namespace MediaBrowser.Providers.Movies
if (!string.IsNullOrEmpty(path))
{
var fileInfo = new FileInfo(path);
var fileInfo = _fileSystem.GetFileInfo(path);
if (fileInfo.Exists)
{

View File

@ -82,7 +82,7 @@ namespace MediaBrowser.Providers.People
};
result.SetProviderId(MetadataProviders.Tmdb, info.id.ToString(_usCulture));
result.SetProviderId(MetadataProviders.Imdb, info.imdb_id.ToString(_usCulture));
result.SetProviderId(MetadataProviders.Imdb, info.imdb_id);
return new[] { result };
}

View File

@ -15,6 +15,8 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Xml;
namespace MediaBrowser.Providers.People
{
@ -23,12 +25,16 @@ namespace MediaBrowser.Providers.People
private readonly IServerConfigurationManager _config;
private readonly ILibraryManager _libraryManager;
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
private readonly IXmlReaderSettingsFactory _xmlSettings;
public TvdbPersonImageProvider(IServerConfigurationManager config, ILibraryManager libraryManager, IHttpClient httpClient)
public TvdbPersonImageProvider(IServerConfigurationManager config, ILibraryManager libraryManager, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlSettings)
{
_config = config;
_libraryManager = libraryManager;
_httpClient = httpClient;
_fileSystem = fileSystem;
_xmlSettings = xmlSettings;
}
public string Name
@ -89,7 +95,7 @@ namespace MediaBrowser.Providers.People
{
return null;
}
catch (DirectoryNotFoundException)
catch (IOException)
{
return null;
}
@ -97,46 +103,47 @@ namespace MediaBrowser.Providers.People
private RemoteImageInfo GetImageInfo(string xmlFile, string personName, CancellationToken cancellationToken)
{
var settings = new XmlReaderSettings
{
CheckCharacters = false,
IgnoreProcessingInstructions = true,
IgnoreComments = true,
ValidationType = ValidationType.None
};
var settings = _xmlSettings.Create(false);
using (var streamReader = new StreamReader(xmlFile, Encoding.UTF8))
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var fileStream = _fileSystem.GetFileStream(xmlFile, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
{
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
reader.MoveToContent();
// Loop through each element
while (reader.Read())
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
cancellationToken.ThrowIfCancellationRequested();
reader.MoveToContent();
if (reader.NodeType == XmlNodeType.Element)
// Loop through each element
while (reader.Read())
{
switch (reader.Name)
{
case "Actor":
{
using (var subtree = reader.ReadSubtree())
{
var info = FetchImageInfoFromActorNode(personName, subtree);
cancellationToken.ThrowIfCancellationRequested();
if (info != null)
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Actor":
{
using (var subtree = reader.ReadSubtree())
{
return info;
var info = FetchImageInfoFromActorNode(personName, subtree);
if (info != null)
{
return info;
}
}
break;
}
default:
reader.Skip();
break;
}
default:
reader.Skip();
break;
}
}
}
}

View File

@ -35,12 +35,12 @@ namespace MediaBrowser.Providers.TV
private readonly IZipClient _zipClient;
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
private readonly IXmlReaderSettingsFactory _xmlSettings;
private readonly IServerConfigurationManager _config;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
private readonly IMemoryStreamProvider _memoryStreamProvider;
private readonly IXmlReaderSettingsFactory _xmlSettings;
private readonly ILocalizationManager _localizationManager;
public TvdbSeriesProvider(IZipClient zipClient, IHttpClient httpClient, IFileSystem fileSystem, IServerConfigurationManager config, ILogger logger, ILibraryManager libraryManager, IMemoryStreamProvider memoryStreamProvider, IXmlReaderSettingsFactory xmlSettings, ILocalizationManager localizationManager)
@ -512,9 +512,11 @@ namespace MediaBrowser.Providers.TV
private async Task<IEnumerable<RemoteSearchResult>> FindSeriesInternal(string name, string language, CancellationToken cancellationToken)
{
var url = string.Format(SeriesSearchUrl, WebUtility.UrlEncode(name), NormalizeLanguage(language));
var doc = new XmlDocument();
var searchResults = new List<RemoteSearchResult>();
using (var results = await _httpClient.Get(new HttpRequestOptions
var comparableName = GetComparableName(name);
using (var stream = await _httpClient.Get(new HttpRequestOptions
{
Url = url,
ResourcePool = TvDbResourcePool,
@ -522,90 +524,48 @@ namespace MediaBrowser.Providers.TV
}).ConfigureAwait(false))
{
doc.Load(results);
}
var settings = _xmlSettings.Create(false);
var searchResults = new List<RemoteSearchResult>();
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
if (doc.HasChildNodes)
{
var nodes = doc.SelectNodes("//Series");
var comparableName = GetComparableName(name);
if (nodes != null)
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
foreach (XmlNode node in nodes)
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
var searchResult = new RemoteSearchResult
{
SearchProviderName = Name
};
reader.MoveToContent();
var titles = new List<string>();
var nameNode = node.SelectSingleNode("./SeriesName");
if (nameNode != null)
// Loop through each element
while (reader.Read())
{
titles.Add(GetComparableName(nameNode.InnerText));
}
cancellationToken.ThrowIfCancellationRequested();
var aliasNode = node.SelectSingleNode("./AliasNames");
if (aliasNode != null)
{
var alias = aliasNode.InnerText.Split('|').Select(GetComparableName);
titles.AddRange(alias);
}
var imdbIdNode = node.SelectSingleNode("./IMDB_ID");
if (imdbIdNode != null)
{
var val = imdbIdNode.InnerText;
if (!string.IsNullOrWhiteSpace(val))
if (reader.NodeType == XmlNodeType.Element)
{
searchResult.SetProviderId(MetadataProviders.Imdb, val);
}
}
var bannerNode = node.SelectSingleNode("./banner");
if (bannerNode != null)
{
var val = bannerNode.InnerText;
if (!string.IsNullOrWhiteSpace(val))
{
searchResult.ImageUrl = TVUtils.BannerUrl + val;
}
}
var airDateNode = node.SelectSingleNode("./FirstAired");
if (airDateNode != null)
{
var val = airDateNode.InnerText;
if (!string.IsNullOrWhiteSpace(val))
{
DateTime date;
if (DateTime.TryParse(val, out date))
switch (reader.Name)
{
searchResult.ProductionYear = date.Year;
case "Series":
{
using (var subtree = reader.ReadSubtree())
{
var searchResult = GetSeriesSearchResultFromSubTree(subtree, comparableName);
if (searchResult != null)
{
searchResult.SearchProviderName = Name;
searchResults.Add(searchResult);
}
}
break;
}
default:
reader.Skip();
break;
}
}
}
foreach (var title in titles)
{
if (string.Equals(title, comparableName, StringComparison.OrdinalIgnoreCase))
{
var id = node.SelectSingleNode("./seriesid") ??
node.SelectSingleNode("./id");
if (id != null)
{
searchResult.Name = title;
searchResult.SetProviderId(MetadataProviders.Tvdb, id.InnerText);
searchResults.Add(searchResult);
}
break;
}
_logger.Info("TVDb Provider - " + title + " did not match " + comparableName);
}
}
}
}
@ -618,6 +578,118 @@ namespace MediaBrowser.Providers.TV
return searchResults;
}
private RemoteSearchResult GetSeriesSearchResultFromSubTree(XmlReader reader, string comparableName)
{
var searchResult = new RemoteSearchResult
{
SearchProviderName = Name
};
var titles = new List<string>();
string seriesId = null;
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "SeriesName":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
titles.Add(GetComparableName(val));
}
break;
}
case "AliasNames":
{
var val = reader.ReadElementContentAsString();
var alias = (val ?? string.Empty).Split(new [] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(GetComparableName);
titles.AddRange(alias);
break;
}
case "IMDB_ID":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
searchResult.SetProviderId(MetadataProviders.Imdb, val);
}
break;
}
case "banner":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
searchResult.ImageUrl = TVUtils.BannerUrl + val;
}
break;
}
case "FirstAired":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
DateTime date;
if (DateTime.TryParse(val, out date))
{
searchResult.ProductionYear = date.Year;
}
}
break;
}
case "id":
case "seriesid":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
seriesId = val;
}
break;
}
default:
reader.Skip();
break;
}
}
}
foreach (var title in titles)
{
if (string.Equals(title, comparableName, StringComparison.OrdinalIgnoreCase))
{
if (!string.IsNullOrWhiteSpace(seriesId))
{
searchResult.Name = title;
searchResult.SetProviderId(MetadataProviders.Tvdb, seriesId);
return searchResult;
}
break;
}
_logger.Info("TVDb Provider - " + title + " did not match " + comparableName);
}
return null;
}
/// <summary>
/// The remove
/// </summary>