jellyfin/MediaBrowser.Providers/Music/LastfmHelper.cs

77 lines
2.4 KiB
C#
Raw Normal View History

2014-01-31 19:55:21 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Model.Logging;
using System;
2014-01-31 19:55:21 +00:00
using System.IO;
using System.Linq;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.Music
{
public static class LastfmHelper
{
2014-01-31 04:50:09 +00:00
public static string GetImageUrl(IHasLastFmImages data, out string size)
{
size = null;
if (data.image == null)
{
return null;
}
2013-09-06 17:26:56 +00:00
var validImages = data.image
.Where(i => !string.IsNullOrWhiteSpace(i.url))
2013-09-06 17:26:56 +00:00
.ToList();
var img = validImages
.FirstOrDefault(i => string.Equals(i.size, "mega", StringComparison.OrdinalIgnoreCase)) ??
data.image.FirstOrDefault(i => string.Equals(i.size, "extralarge", StringComparison.OrdinalIgnoreCase)) ??
data.image.FirstOrDefault(i => string.Equals(i.size, "large", StringComparison.OrdinalIgnoreCase)) ??
data.image.FirstOrDefault(i => string.Equals(i.size, "medium", StringComparison.OrdinalIgnoreCase)) ??
data.image.FirstOrDefault();
if (img != null)
{
size = img.size;
return img.url;
}
return null;
2013-03-05 16:48:17 +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-02-09 17:44:25 +00:00
if (appPaths == null)
{
throw new ArgumentNullException("appPaths");
}
if (string.IsNullOrEmpty(musicBrainzId))
{
throw new ArgumentNullException("musicBrainzId");
}
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
2014-01-31 19:55:21 +00:00
var cachePath = Path.Combine(appPaths.CachePath, "lastfm", musicBrainzId, "image.txt");
2014-01-31 19:55:21 +00:00
try
{
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
}
}
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);
}
}
}
}