consolidated duplicate code

This commit is contained in:
Luke Pulverenti 2013-04-28 19:39:17 -04:00
parent 46a546732c
commit 2a5ba9e707
9 changed files with 367 additions and 445 deletions

View File

@ -203,6 +203,8 @@ namespace MediaBrowser.Controller.Providers
throw new ArgumentNullException("providerInfo"); throw new ArgumentNullException("providerInfo");
} }
if (item.DontFetchMeta && RequiresInternet) return false;
if (CompareDate(item) > providerInfo.LastRefreshed) if (CompareDate(item) > providerInfo.LastRefreshed)
{ {
return true; return true;
@ -218,6 +220,16 @@ namespace MediaBrowser.Controller.Providers
return true; return true;
} }
if (RequiresInternet && DateTime.UtcNow > (providerInfo.LastRefreshed.AddDays(ConfigurationManager.Configuration.MetadataRefreshDays)))
{
return true;
}
if (providerInfo.LastRefreshStatus != ProviderRefreshStatus.Success)
{
return true;
}
return false; return false;
} }

View File

@ -67,20 +67,6 @@ namespace MediaBrowser.Controller.Providers
{ {
} }
/// <summary>
/// Needses the refresh internal.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (item.DontFetchMeta) return false;
return DateTime.UtcNow > (providerInfo.LastRefreshed.AddDays(ConfigurationManager.Configuration.MetadataRefreshDays))
&& ShouldFetch(item, providerInfo);
}
/// <summary> /// <summary>
/// Gets a value indicating whether [requires internet]. /// Gets a value indicating whether [requires internet].
/// </summary> /// </summary>
@ -99,16 +85,6 @@ namespace MediaBrowser.Controller.Providers
get { return MetadataProviderPriority.Third; } get { return MetadataProviderPriority.Third; }
} }
/// <summary>
/// Shoulds the fetch.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected virtual bool ShouldFetch(BaseItem item, BaseProviderInfo providerInfo)
{
return false;
}
#region Result Objects #region Result Objects
protected class FanArtImageInfo protected class FanArtImageInfo

View File

@ -1,5 +1,4 @@
using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Entities.Movies;
@ -74,22 +73,26 @@ namespace MediaBrowser.Controller.Providers.Movies
} }
/// <summary> /// <summary>
/// Shoulds the fetch. /// Needses the refresh internal.
/// </summary> /// </summary>
/// <param name="item">The item.</param> /// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param> /// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected override bool ShouldFetch(BaseItem item, BaseProviderInfo providerInfo) protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{ {
var baseItem = item; if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tmdb)))
if (item.Path == null || item.DontFetchMeta || string.IsNullOrEmpty(baseItem.GetProviderId(MetadataProviders.Tmdb))) return false; //nothing to do {
var artExists = item.ResolveArgs.ContainsMetaFileByName(ART_FILE); return false;
var logoExists = item.ResolveArgs.ContainsMetaFileByName(LOGO_FILE); }
var discExists = item.ResolveArgs.ContainsMetaFileByName(DISC_FILE);
return (!artExists && ConfigurationManager.Configuration.DownloadMovieImages.Art) if (!ConfigurationManager.Configuration.DownloadMovieImages.Art &&
|| (!logoExists && ConfigurationManager.Configuration.DownloadMovieImages.Logo) !ConfigurationManager.Configuration.DownloadMovieImages.Logo &&
|| (!discExists && ConfigurationManager.Configuration.DownloadMovieImages.Disc); !ConfigurationManager.Configuration.DownloadMovieImages.Disc)
{
return false;
}
return base.NeedsRefreshInternal(item, providerInfo);
} }
/// <summary> /// <summary>
@ -105,14 +108,6 @@ namespace MediaBrowser.Controller.Providers.Movies
var movie = item; var movie = item;
BaseProviderInfo providerData;
if (!item.ProviderData.TryGetValue(Id, out providerData))
{
providerData = new BaseProviderInfo();
}
if (ShouldFetch(movie, providerData))
{
var language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower(); var language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower();
var url = string.Format(FanArtBaseUrl, APIKey, movie.GetProviderId(MetadataProviders.Tmdb)); var url = string.Format(FanArtBaseUrl, APIKey, movie.GetProviderId(MetadataProviders.Tmdb));
var doc = new XmlDocument(); var doc = new XmlDocument();
@ -259,7 +254,6 @@ namespace MediaBrowser.Controller.Providers.Movies
} }
} }
} }
}
SetLastRefreshed(movie, DateTime.UtcNow); SetLastRefreshed(movie, DateTime.UtcNow);
return true; return true;
} }

View File

@ -31,19 +31,6 @@ namespace MediaBrowser.Controller.Providers.Music
return item is MusicAlbum; return item is MusicAlbum;
} }
/// <summary>
/// Needses the refresh internal.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if we need refreshing, <c>false</c> otherwise</returns>
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
//we fetch if image needed and haven't already tried recently
return (string.IsNullOrEmpty(item.PrimaryImagePath) || !item.HasImage(ImageType.Disc)) &&
DateTime.Today.Subtract(providerInfo.LastRefreshed).TotalDays > ConfigurationManager.Configuration.MetadataRefreshDays;
}
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken) public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{ {
var mbid = item.GetProviderId(MetadataProviders.Musicbrainz); var mbid = item.GetProviderId(MetadataProviders.Musicbrainz);

View File

@ -1,8 +1,6 @@
using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging; using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net; using MediaBrowser.Model.Net;
@ -60,18 +58,28 @@ namespace MediaBrowser.Controller.Providers.Music
} }
/// <summary> /// <summary>
/// Shoulds the fetch. /// Needses the refresh internal.
/// </summary> /// </summary>
/// <param name="item">The item.</param> /// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param> /// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected override bool ShouldFetch(BaseItem item, BaseProviderInfo providerInfo) protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{ {
if (item.Path == null || item.DontFetchMeta || string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz))) return false; //nothing to do if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz)))
{
return false;
}
return (!item.ResolveArgs.ContainsMetaFileByName(ART_FILE) && ConfigurationManager.Configuration.DownloadMusicArtistImages.Art) if (!ConfigurationManager.Configuration.DownloadMusicArtistImages.Art &&
|| (!item.ResolveArgs.ContainsMetaFileByName(LOGO_FILE) && ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo) !ConfigurationManager.Configuration.DownloadMusicArtistImages.Backdrops &&
|| (!item.ResolveArgs.ContainsMetaFileByName(DISC_FILE) && ConfigurationManager.Configuration.DownloadMusicArtistImages.Disc); !ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner &&
!ConfigurationManager.Configuration.DownloadMusicArtistImages.Logo &&
!ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary)
{
return false;
}
return base.NeedsRefreshInternal(item, providerInfo);
} }
/// <summary> /// <summary>
@ -87,15 +95,6 @@ namespace MediaBrowser.Controller.Providers.Music
//var artist = item; //var artist = item;
BaseProviderInfo providerData;
if (!item.ProviderData.TryGetValue(Id, out providerData))
{
providerData = new BaseProviderInfo();
}
if (ShouldFetch(item, providerData))
{
var url = string.Format(FanArtBaseUrl, APIKey, item.GetProviderId(MetadataProviders.Musicbrainz)); var url = string.Format(FanArtBaseUrl, APIKey, item.GetProviderId(MetadataProviders.Musicbrainz));
var doc = new XmlDocument(); var doc = new XmlDocument();
@ -201,7 +200,7 @@ namespace MediaBrowser.Controller.Providers.Music
if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner && !item.ResolveArgs.ContainsMetaFileByName(BANNER_FILE)) if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner && !item.ResolveArgs.ContainsMetaFileByName(BANNER_FILE))
{ {
var node = doc.SelectSingleNode("//fanart/music/musicbanners/"+hd+"musicbanner/@url") ?? var node = doc.SelectSingleNode("//fanart/music/musicbanners/" + hd + "musicbanner/@url") ??
doc.SelectSingleNode("//fanart/music/musicbanners/musicbanner/@url"); doc.SelectSingleNode("//fanart/music/musicbanners/musicbanner/@url");
path = node != null ? node.Value : null; path = node != null ? node.Value : null;
if (!string.IsNullOrEmpty(path)) if (!string.IsNullOrEmpty(path))
@ -245,7 +244,7 @@ namespace MediaBrowser.Controller.Providers.Music
} }
} }
} }
}
SetLastRefreshed(item, DateTime.UtcNow); SetLastRefreshed(item, DateTime.UtcNow);
return true; return true;
} }

View File

@ -168,36 +168,6 @@ namespace MediaBrowser.Controller.Providers.Music
return WebUtility.UrlEncode(name); return WebUtility.UrlEncode(name);
} }
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (item.DontFetchMeta) return false;
if (RefreshOnFileSystemStampChange && HasFileSystemStampChanged(item, providerInfo))
{
//If they deleted something from file system, chances are, this item was mis-identified the first time
item.SetProviderId(MetadataProviders.Musicbrainz, null);
Logger.Debug("LastfmProvider reports file system stamp change...");
return true;
}
if (providerInfo.LastRefreshStatus != ProviderRefreshStatus.Success)
{
Logger.Debug("LastfmProvider for {0} - last attempt had errors. Will try again.", item.Path);
return true;
}
if (RefreshOnVersionChange && ProviderVersion != providerInfo.ProviderVersion)
{
Logger.Debug("LastfmProvider version change re-running for {0}", item.Path);
return true;
}
if (DateTime.UtcNow.Subtract(providerInfo.LastRefreshed).TotalDays > ConfigurationManager.Configuration.MetadataRefreshDays) // only refresh every n days
return true;
return false;
}
/// <summary> /// <summary>
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
/// </summary> /// </summary>

View File

@ -1,5 +1,4 @@
using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Entities.TV;
@ -42,17 +41,27 @@ namespace MediaBrowser.Controller.Providers.TV
return item is Series; return item is Series;
} }
protected override bool ShouldFetch(BaseItem item, BaseProviderInfo providerInfo) /// <summary>
/// Needses the refresh internal.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{ {
if (item.DontFetchMeta || string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tvdb))) return false; //nothing to do if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tvdb)))
var artExists = item.ResolveArgs.ContainsMetaFileByName(ART_FILE); {
var logoExists = item.ResolveArgs.ContainsMetaFileByName(LOGO_FILE); return false;
var thumbExists = item.ResolveArgs.ContainsMetaFileByName(THUMB_FILE); }
if (!ConfigurationManager.Configuration.DownloadSeriesImages.Art &&
!ConfigurationManager.Configuration.DownloadSeriesImages.Logo &&
!ConfigurationManager.Configuration.DownloadSeriesImages.Thumb)
{
return false;
}
return (!artExists && ConfigurationManager.Configuration.DownloadSeriesImages.Art) return base.NeedsRefreshInternal(item, providerInfo);
|| (!logoExists && ConfigurationManager.Configuration.DownloadSeriesImages.Logo)
|| (!thumbExists && ConfigurationManager.Configuration.DownloadSeriesImages.Thumb);
} }
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken) public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
@ -61,15 +70,6 @@ namespace MediaBrowser.Controller.Providers.TV
var series = (Series)item; var series = (Series)item;
BaseProviderInfo providerData;
if (!item.ProviderData.TryGetValue(Id, out providerData))
{
providerData = new BaseProviderInfo();
}
if (ShouldFetch(series, providerData))
{
string language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower(); string language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower();
string url = string.Format(FanArtBaseUrl, APIKey, series.GetProviderId(MetadataProviders.Tvdb)); string url = string.Format(FanArtBaseUrl, APIKey, series.GetProviderId(MetadataProviders.Tvdb));
var doc = new XmlDocument(); var doc = new XmlDocument();
@ -93,9 +93,9 @@ namespace MediaBrowser.Controller.Providers.TV
var hd = ConfigurationManager.Configuration.DownloadHDFanArt ? "hdtv" : "clear"; var hd = ConfigurationManager.Configuration.DownloadHDFanArt ? "hdtv" : "clear";
if (ConfigurationManager.Configuration.DownloadSeriesImages.Logo && !series.ResolveArgs.ContainsMetaFileByName(LOGO_FILE)) if (ConfigurationManager.Configuration.DownloadSeriesImages.Logo && !series.ResolveArgs.ContainsMetaFileByName(LOGO_FILE))
{ {
var node = doc.SelectSingleNode("//fanart/series/"+hd+"logos/"+hd+"logo[@lang = \"" + language + "\"]/@url") ?? var node = doc.SelectSingleNode("//fanart/series/" + hd + "logos/" + hd + "logo[@lang = \"" + language + "\"]/@url") ??
doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo[@lang = \"" + language + "\"]/@url") ?? doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo[@lang = \"" + language + "\"]/@url") ??
doc.SelectSingleNode("//fanart/series/"+hd+"logos/"+hd+"logo/@url") ?? doc.SelectSingleNode("//fanart/series/" + hd + "logos/" + hd + "logo/@url") ??
doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo/@url"); doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo/@url");
path = node != null ? node.Value : null; path = node != null ? node.Value : null;
if (!string.IsNullOrEmpty(path)) if (!string.IsNullOrEmpty(path))
@ -120,9 +120,9 @@ namespace MediaBrowser.Controller.Providers.TV
hd = ConfigurationManager.Configuration.DownloadHDFanArt ? "hd" : ""; hd = ConfigurationManager.Configuration.DownloadHDFanArt ? "hd" : "";
if (ConfigurationManager.Configuration.DownloadSeriesImages.Art && !series.ResolveArgs.ContainsMetaFileByName(ART_FILE)) if (ConfigurationManager.Configuration.DownloadSeriesImages.Art && !series.ResolveArgs.ContainsMetaFileByName(ART_FILE))
{ {
var node = doc.SelectSingleNode("//fanart/series/"+hd+"cleararts/"+hd+"clearart[@lang = \"" + language + "\"]/@url") ?? var node = doc.SelectSingleNode("//fanart/series/" + hd + "cleararts/" + hd + "clearart[@lang = \"" + language + "\"]/@url") ??
doc.SelectSingleNode("//fanart/series/cleararts/clearart[@lang = \"" + language + "\"]/@url") ?? doc.SelectSingleNode("//fanart/series/cleararts/clearart[@lang = \"" + language + "\"]/@url") ??
doc.SelectSingleNode("//fanart/series/"+hd+"cleararts/"+hd+"clearart/@url") ?? doc.SelectSingleNode("//fanart/series/" + hd + "cleararts/" + hd + "clearart/@url") ??
doc.SelectSingleNode("//fanart/series/cleararts/clearart/@url"); doc.SelectSingleNode("//fanart/series/cleararts/clearart/@url");
path = node != null ? node.Value : null; path = node != null ? node.Value : null;
if (!string.IsNullOrEmpty(path)) if (!string.IsNullOrEmpty(path))
@ -166,8 +166,9 @@ namespace MediaBrowser.Controller.Providers.TV
} }
} }
} }
}
SetLastRefreshed(series, DateTime.UtcNow); SetLastRefreshed(series, DateTime.UtcNow);
return true; return true;
} }
} }

View File

@ -96,22 +96,12 @@ namespace MediaBrowser.Controller.Providers.TV
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{ {
bool fetch = false; if (HasLocalMeta(item))
var episode = (Episode)item;
var downloadDate = providerInfo.LastRefreshed;
if (ConfigurationManager.Configuration.MetadataRefreshDays == -1 && downloadDate != DateTime.MinValue)
{ {
return false; return false;
} }
if (!item.DontFetchMeta && !HasLocalMeta(episode)) return base.NeedsRefreshInternal(item, providerInfo);
{
fetch = ConfigurationManager.Configuration.MetadataRefreshDays != -1 &&
DateTime.Today.Subtract(downloadDate).TotalDays > ConfigurationManager.Configuration.MetadataRefreshDays;
}
return fetch;
} }
/// <summary> /// <summary>
@ -313,7 +303,7 @@ namespace MediaBrowser.Controller.Providers.TV
/// </summary> /// </summary>
/// <param name="episode">The episode.</param> /// <param name="episode">The episode.</param>
/// <returns><c>true</c> if [has local meta] [the specified episode]; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if [has local meta] [the specified episode]; otherwise, <c>false</c>.</returns>
private bool HasLocalMeta(Episode episode) private bool HasLocalMeta(BaseItem episode)
{ {
return (episode.Parent.ResolveArgs.ContainsMetaFileByName(Path.GetFileNameWithoutExtension(episode.Path) + ".xml")); return (episode.Parent.ResolveArgs.ContainsMetaFileByName(Path.GetFileNameWithoutExtension(episode.Path) + ".xml"));
} }

View File

@ -78,19 +78,12 @@ namespace MediaBrowser.Controller.Providers.TV
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{ {
bool fetch = false; if (HasLocalMeta(item))
var downloadDate = providerInfo.LastRefreshed;
if (ConfigurationManager.Configuration.MetadataRefreshDays == -1 && downloadDate != DateTime.MinValue)
return false;
if (!HasLocalMeta(item))
{ {
fetch = ConfigurationManager.Configuration.MetadataRefreshDays != -1 && return false;
DateTime.UtcNow.Subtract(downloadDate).TotalDays > ConfigurationManager.Configuration.MetadataRefreshDays;
} }
return fetch; return base.NeedsRefreshInternal(item, providerInfo);
} }
/// <summary> /// <summary>