2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2013-05-25 04:26:22 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-03-04 15:13:03 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-03-04 14:34:00 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-05-02 14:30:38 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-01-31 04:50:09 +00:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
2013-03-04 14:34:00 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-11-05 22:29:07 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-03-04 14:34:00 +00:00
|
|
|
|
using System;
|
2013-11-05 22:29:07 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-05-12 22:57:51 +00:00
|
|
|
|
using System.Globalization;
|
2013-05-25 15:16:50 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2014-01-31 04:50:09 +00:00
|
|
|
|
using System.Text;
|
2013-03-04 14:34:00 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2014-01-31 04:50:09 +00:00
|
|
|
|
using System.Xml;
|
2013-03-04 14:34:00 +00:00
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.Music
|
2013-03-04 14:34:00 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public class FanartArtistProvider : IRemoteImageProvider, IHasChangeMonitor, IHasOrder
|
2013-03-04 14:34:00 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
internal static readonly SemaphoreSlim FanArtResourcePool = new SemaphoreSlim(3, 3);
|
|
|
|
|
internal const string ApiKey = "5c6b04c68e904cfed1e6cbc9a9e683d4";
|
|
|
|
|
private const string FanArtBaseUrl = "http://api.fanart.tv/webservice/artist/{0}/{1}/xml/all/1/1";
|
2013-03-08 05:08:27 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-05-25 15:16:50 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
internal static FanartArtistProvider Current;
|
|
|
|
|
|
|
|
|
|
public FanartArtistProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
|
2013-03-04 14:34:00 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
_config = config;
|
|
|
|
|
_httpClient = httpClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-05-25 15:16:50 +00:00
|
|
|
|
|
|
|
|
|
Current = this;
|
2013-03-04 14:34:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return ProviderName; }
|
|
|
|
|
}
|
2013-03-04 14:34:00 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
public static string ProviderName
|
2013-06-25 01:22:21 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
get { return "FanArt"; }
|
2013-06-25 01:22:21 +00:00
|
|
|
|
}
|
2014-01-31 04:50:09 +00:00
|
|
|
|
|
|
|
|
|
public bool Supports(IHasImages item)
|
2013-03-04 14:34:00 +00:00
|
|
|
|
{
|
2013-05-02 14:30:38 +00:00
|
|
|
|
return item is MusicArtist;
|
2013-03-04 14:34:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
2013-05-02 22:32:15 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
return new List<ImageType>
|
2013-05-02 22:32:15 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
ImageType.Primary,
|
|
|
|
|
ImageType.Logo,
|
|
|
|
|
ImageType.Art,
|
|
|
|
|
ImageType.Banner,
|
|
|
|
|
ImageType.Backdrop
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
|
2014-01-31 04:50:09 +00:00
|
|
|
|
{
|
|
|
|
|
var artist = (MusicArtist)item;
|
|
|
|
|
|
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
|
2014-02-07 22:40:03 +00:00
|
|
|
|
var artistMusicBrainzId = artist.GetProviderId(MetadataProviders.MusicBrainzArtist);
|
2014-01-31 04:50:09 +00:00
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(artistMusicBrainzId))
|
|
|
|
|
{
|
|
|
|
|
await EnsureMovieXml(artistMusicBrainzId, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var artistXmlPath = GetArtistXmlPath(_config.CommonApplicationPaths, artistMusicBrainzId);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
AddImages(list, artistXmlPath, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2013-05-02 22:32:15 +00:00
|
|
|
|
}
|
2014-01-31 04:50:09 +00:00
|
|
|
|
|
|
|
|
|
var language = item.GetPreferredMetadataLanguage();
|
|
|
|
|
|
|
|
|
|
var isLanguageEn = String.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
// Sort first by width to prioritize HD versions
|
|
|
|
|
return list.OrderByDescending(i => i.Width ?? 0)
|
|
|
|
|
.ThenByDescending(i =>
|
|
|
|
|
{
|
|
|
|
|
if (String.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
if (!isLanguageEn)
|
|
|
|
|
{
|
|
|
|
|
if (String.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (String.IsNullOrEmpty(i.Language))
|
|
|
|
|
{
|
|
|
|
|
return isLanguageEn ? 3 : 2;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
})
|
|
|
|
|
.ThenByDescending(i => i.CommunityRating ?? 0)
|
|
|
|
|
.ThenByDescending(i => i.VoteCount ?? 0);
|
2013-05-02 22:32:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 15:16:50 +00:00
|
|
|
|
/// <summary>
|
2014-01-31 04:50:09 +00:00
|
|
|
|
/// Adds the images.
|
2013-05-25 15:16:50 +00:00
|
|
|
|
/// </summary>
|
2014-01-31 04:50:09 +00:00
|
|
|
|
/// <param name="list">The list.</param>
|
|
|
|
|
/// <param name="xmlPath">The XML path.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
private void AddImages(List<RemoteImageInfo> list, string xmlPath, CancellationToken cancellationToken)
|
2013-05-02 22:32:15 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
using (var streamReader = new StreamReader(xmlPath, Encoding.UTF8))
|
2013-05-02 22:32:15 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "music":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImagesFromMusicNode(list, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-02 22:32:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the images from music node.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="list">The list.</param>
|
|
|
|
|
/// <param name="reader">The reader.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
private void AddImagesFromMusicNode(List<RemoteImageInfo> list, XmlReader reader, CancellationToken cancellationToken)
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "hdmusiclogos":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImagesFromImageTypeNode(list, ImageType.Logo, 800, 310, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "musiclogos":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImagesFromImageTypeNode(list, ImageType.Logo, 400, 155, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "artistbackgrounds":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImagesFromImageTypeNode(list, ImageType.Backdrop, 1920, 1080, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "hdmusicarts":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImagesFromImageTypeNode(list, ImageType.Art, 1000, 562, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "musicarts":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImagesFromImageTypeNode(list, ImageType.Art, 500, 281, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "hdmusicbanners":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImagesFromImageTypeNode(list, ImageType.Banner, 1000, 185, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "musicbanners":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImagesFromImageTypeNode(list, ImageType.Banner, 1000, 185, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "artistthumbs":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImagesFromImageTypeNode(list, ImageType.Primary, 1000, 1000, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
using (reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-06 15:23:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-04 14:34:00 +00:00
|
|
|
|
/// <summary>
|
2014-01-31 04:50:09 +00:00
|
|
|
|
/// Adds the images from albums node.
|
2013-03-04 14:34:00 +00:00
|
|
|
|
/// </summary>
|
2014-01-31 04:50:09 +00:00
|
|
|
|
/// <param name="list">The list.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="width">The width.</param>
|
|
|
|
|
/// <param name="height">The height.</param>
|
|
|
|
|
/// <param name="reader">The reader.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
private void AddImagesFromImageTypeNode(List<RemoteImageInfo> list, ImageType type, int width, int height, XmlReader reader, CancellationToken cancellationToken)
|
2013-03-04 14:34:00 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
2013-04-28 23:39:17 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "hdmusiclogo":
|
|
|
|
|
case "musiclogo":
|
|
|
|
|
case "artistbackground":
|
|
|
|
|
case "hdmusicart":
|
|
|
|
|
case "musicart":
|
|
|
|
|
case "hdmusicbanner":
|
|
|
|
|
case "musicbanner":
|
|
|
|
|
case "artistthumb":
|
|
|
|
|
{
|
|
|
|
|
AddImage(list, reader, type, width, height);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
using (reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-28 23:39:17 +00:00
|
|
|
|
}
|
2013-03-04 14:34:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="list">The list.</param>
|
|
|
|
|
/// <param name="reader">The reader.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="width">The width.</param>
|
|
|
|
|
/// <param name="height">The height.</param>
|
|
|
|
|
private void AddImage(List<RemoteImageInfo> list, XmlReader reader, ImageType type, int width, int height)
|
2013-05-25 15:16:50 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
var url = reader.GetAttribute("url");
|
2013-05-25 15:16:50 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
var size = reader.GetAttribute("size");
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(size))
|
2013-05-25 15:16:50 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
int sizeNum;
|
|
|
|
|
if (Int32.TryParse(size, NumberStyles.Any, _usCulture, out sizeNum))
|
|
|
|
|
{
|
|
|
|
|
width = sizeNum;
|
|
|
|
|
height = sizeNum;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-25 15:16:50 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
var likesString = reader.GetAttribute("likes");
|
|
|
|
|
int likes;
|
|
|
|
|
|
|
|
|
|
var info = new RemoteImageInfo
|
|
|
|
|
{
|
|
|
|
|
RatingType = RatingType.Likes,
|
|
|
|
|
Type = type,
|
|
|
|
|
Width = width,
|
|
|
|
|
Height = height,
|
|
|
|
|
ProviderName = Name,
|
|
|
|
|
Url = url,
|
|
|
|
|
Language = reader.GetAttribute("lang")
|
|
|
|
|
};
|
2013-11-05 22:29:07 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
if (!String.IsNullOrEmpty(likesString) && Int32.TryParse(likesString, NumberStyles.Any, _usCulture, out likes))
|
|
|
|
|
{
|
|
|
|
|
info.CommunityRating = likes;
|
2013-05-25 15:16:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
list.Add(info);
|
2013-05-25 15:16:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
public int Order
|
2013-05-25 04:26:22 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
get { return 0; }
|
2013-05-25 04:26:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
2013-05-25 04:26:22 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = FanArtResourcePool
|
|
|
|
|
});
|
2013-05-25 04:26:22 +00:00
|
|
|
|
}
|
2013-05-25 15:16:50 +00:00
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
2013-03-04 14:34:00 +00:00
|
|
|
|
{
|
2014-02-07 00:43:45 +00:00
|
|
|
|
if (!_config.Configuration.EnableFanArtUpdates)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 22:40:03 +00:00
|
|
|
|
var id = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
|
2013-03-04 14:34:00 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
if (!String.IsNullOrEmpty(id))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
|
|
|
|
var artistXmlPath = GetArtistXmlPath(_config.CommonApplicationPaths, id);
|
2013-04-13 18:02:30 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
var fileInfo = new FileInfo(artistXmlPath);
|
2013-05-25 15:16:50 +00:00
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
|
2013-05-25 15:16:50 +00:00
|
|
|
|
}
|
2013-05-25 04:26:22 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 17:44:13 +00:00
|
|
|
|
private readonly Task _cachedTask = Task.FromResult(true);
|
2014-01-31 04:50:09 +00:00
|
|
|
|
internal Task EnsureMovieXml(string musicBrainzId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var xmlPath = GetArtistXmlPath(_config.ApplicationPaths, musicBrainzId);
|
|
|
|
|
|
|
|
|
|
var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
|
|
|
|
|
|
|
|
|
|
if (fileInfo.Exists)
|
2013-05-25 15:16:50 +00:00
|
|
|
|
{
|
2014-02-19 18:50:37 +00:00
|
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 7)
|
2014-01-31 04:50:09 +00:00
|
|
|
|
{
|
2014-02-03 17:44:13 +00:00
|
|
|
|
return _cachedTask;
|
2014-01-31 04:50:09 +00:00
|
|
|
|
}
|
2013-05-25 15:16:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
return DownloadArtistXml(musicBrainzId, cancellationToken);
|
2013-05-25 15:16:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Downloads the artist XML.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="musicBrainzId">The music brainz id.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
2014-01-31 04:50:09 +00:00
|
|
|
|
internal async Task DownloadArtistXml(string musicBrainzId, CancellationToken cancellationToken)
|
2013-05-25 15:16:50 +00:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var url = string.Format(FanArtBaseUrl, ApiKey, musicBrainzId);
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
var xmlPath = GetArtistXmlPath(_config.ApplicationPaths, musicBrainzId);
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(xmlPath));
|
2013-05-25 15:16:50 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
using (var response = await _httpClient.Get(new HttpRequestOptions
|
2013-04-13 18:02:30 +00:00
|
|
|
|
{
|
2013-05-12 22:57:51 +00:00
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = FanArtResourcePool,
|
2013-05-24 16:51:42 +00:00
|
|
|
|
CancellationToken = cancellationToken
|
2013-05-06 19:31:57 +00:00
|
|
|
|
|
2013-05-12 22:57:51 +00:00
|
|
|
|
}).ConfigureAwait(false))
|
2013-04-28 23:39:17 +00:00
|
|
|
|
{
|
2013-10-31 14:03:23 +00:00
|
|
|
|
using (var xmlFileStream = _fileSystem.GetFileStream(xmlPath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
|
2013-05-25 04:26:22 +00:00
|
|
|
|
{
|
|
|
|
|
await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-04-28 23:39:17 +00:00
|
|
|
|
}
|
2013-05-25 15:16:50 +00:00
|
|
|
|
}
|
2013-06-19 13:54:45 +00:00
|
|
|
|
|
2013-05-25 15:16:50 +00:00
|
|
|
|
/// <summary>
|
2014-01-31 04:50:09 +00:00
|
|
|
|
/// Gets the artist data path.
|
2013-05-25 15:16:50 +00:00
|
|
|
|
/// </summary>
|
2014-01-31 04:50:09 +00:00
|
|
|
|
/// <param name="appPaths">The application paths.</param>
|
|
|
|
|
/// <param name="musicBrainzArtistId">The music brainz artist identifier.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
|
2013-05-25 15:16:50 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
var dataPath = Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
|
2013-03-04 14:34:00 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
return dataPath;
|
|
|
|
|
}
|
2013-12-15 18:33:07 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the artist data path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The application paths.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
internal static string GetArtistDataPath(IApplicationPaths appPaths)
|
|
|
|
|
{
|
|
|
|
|
var dataPath = Path.Combine(appPaths.DataPath, "fanart-music");
|
2013-11-05 22:29:07 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
return dataPath;
|
2013-03-04 14:34:00 +00:00
|
|
|
|
}
|
2013-11-30 06:08:14 +00:00
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
internal static string GetArtistXmlPath(IApplicationPaths appPaths, string musicBrainzArtistId)
|
2013-11-30 06:08:14 +00:00
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
|
|
|
|
|
|
|
|
|
|
return Path.Combine(dataPath, "fanart.xml");
|
2013-11-30 06:08:14 +00:00
|
|
|
|
}
|
2013-03-04 14:34:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|