2014-01-31 19:55:21 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-03-12 22:49:45 +00:00
|
|
|
|
using System;
|
2014-01-31 19:55:21 +00:00
|
|
|
|
using System.IO;
|
2013-05-17 15:22:08 +00:00
|
|
|
|
using System.Linq;
|
2013-03-04 20:03:36 +00:00
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.Music
|
2013-03-04 20:03:36 +00:00
|
|
|
|
{
|
|
|
|
|
public static class LastfmHelper
|
|
|
|
|
{
|
2014-01-31 04:50:09 +00:00
|
|
|
|
public static string GetImageUrl(IHasLastFmImages data, out string size)
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2013-11-06 16:29:20 +00:00
|
|
|
|
size = null;
|
|
|
|
|
|
2013-09-06 15:23:20 +00:00
|
|
|
|
if (data.image == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-06 17:26:56 +00:00
|
|
|
|
var validImages = data.image
|
2013-09-06 15:38:22 +00:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i.url))
|
2013-09-06 17:26:56 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var img = validImages
|
2013-09-06 15:38:22 +00:00
|
|
|
|
.FirstOrDefault(i => string.Equals(i.size, "mega", StringComparison.OrdinalIgnoreCase)) ??
|
|
|
|
|
data.image.FirstOrDefault(i => string.Equals(i.size, "extralarge", StringComparison.OrdinalIgnoreCase)) ??
|
2013-11-06 16:29:20 +00:00
|
|
|
|
data.image.FirstOrDefault(i => string.Equals(i.size, "large", StringComparison.OrdinalIgnoreCase)) ??
|
|
|
|
|
data.image.FirstOrDefault(i => string.Equals(i.size, "medium", StringComparison.OrdinalIgnoreCase)) ??
|
2013-09-06 15:23:20 +00:00
|
|
|
|
data.image.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (img != null)
|
|
|
|
|
{
|
2013-11-06 16:29:20 +00:00
|
|
|
|
size = img.size;
|
2013-09-06 15:23:20 +00:00
|
|
|
|
return img.url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2013-03-05 16:48:17 +00:00
|
|
|
|
}
|
2013-03-04 20:03:36 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public static void SaveImageInfo(IApplicationPaths appPaths, ILogger logger, string musicBrainzId, string url, string size)
|
2013-03-05 16:48:17 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
var cachePath = Path.Combine(appPaths.CachePath, "lastfm", musicBrainzId, "image.txt");
|
2013-03-12 04:09:31 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
try
|
2013-06-15 22:19:47 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
if (string.IsNullOrEmpty(url))
|
2013-11-08 21:22:02 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
File.Delete(cachePath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(cachePath));
|
|
|
|
|
File.WriteAllText(cachePath, url + "|" + size);
|
2013-11-08 21:22:02 +00:00
|
|
|
|
}
|
2013-09-02 01:37:44 +00:00
|
|
|
|
}
|
2014-01-31 19:55:21 +00:00
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
// Don't fail if this is unable to write
|
|
|
|
|
logger.ErrorException("Error saving to {0}", ex, cachePath);
|
|
|
|
|
}
|
2013-03-04 20:03:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|