jellyfin/MediaBrowser.Providers/Music/LastfmHelper.cs

124 lines
4.0 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Entities;
2013-04-24 14:05:47 +00:00
using MediaBrowser.Controller.Entities.Audio;
2013-03-05 16:48:17 +00:00
using MediaBrowser.Model.Entities;
using System;
using System.Linq;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.Music
{
public static class LastfmHelper
{
public static void ProcessArtistData(MusicArtist artist, LastfmArtist data)
{
2013-03-05 15:45:25 +00:00
var yearFormed = 0;
if (data.bio != null)
2013-03-05 15:45:25 +00:00
{
Int32.TryParse(data.bio.yearformed, out yearFormed);
if (!artist.LockedFields.Contains(MetadataFields.Overview))
{
artist.Overview = data.bio.content;
}
if (!string.IsNullOrEmpty(data.bio.placeformed) && !artist.LockedFields.Contains(MetadataFields.ProductionLocations))
2013-04-22 04:38:03 +00:00
{
artist.AddProductionLocation(data.bio.placeformed);
}
2013-03-05 15:45:25 +00:00
}
2013-11-08 21:22:02 +00:00
if (yearFormed > 0)
{
artist.PremiereDate = new DateTime(yearFormed, 1, 1, 0, 0, 0, DateTimeKind.Utc);
artist.ProductionYear = yearFormed;
}
if (data.tags != null && !artist.LockedFields.Contains(MetadataFields.Tags))
{
AddTags(artist, data.tags);
2013-03-05 16:48:17 +00:00
}
string imageSize;
artist.LastFmImageUrl = GetImageUrl(data, out imageSize);
artist.LastFmImageSize = imageSize;
}
private 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
}
2013-03-05 16:48:17 +00:00
public static void ProcessAlbumData(BaseItem item, LastfmAlbum data)
{
var overview = data.wiki != null ? data.wiki.content : null;
if (!item.LockedFields.Contains(MetadataFields.Overview))
{
item.Overview = overview;
}
// Only grab the date here if the album doesn't already have one, since id3 tags are preferred
if (!item.PremiereDate.HasValue)
{
DateTime release;
if (DateTime.TryParse(data.releasedate, out release))
2013-11-08 21:22:02 +00:00
{
// Lastfm sends back null as sometimes 1901, other times 0
if (release.Year > 1901)
{
item.PremiereDate = release;
item.ProductionYear = release.Year;
}
2013-11-08 21:22:02 +00:00
}
}
if (data.toptags != null && !item.LockedFields.Contains(MetadataFields.Tags))
2013-03-05 16:48:17 +00:00
{
AddTags(item, data.toptags);
}
var album = (MusicAlbum)item;
string imageSize;
album.LastFmImageUrl = GetImageUrl(data, out imageSize);
album.LastFmImageSize = imageSize;
}
private static void AddTags(BaseItem item, LastfmTags tags)
2013-03-05 16:48:17 +00:00
{
var itemTags = (from tag in tags.tag where !string.IsNullOrEmpty(tag.name) select tag.name).ToList();
var hasTags = item as IHasTags;
if (hasTags != null)
{
hasTags.Tags = itemTags;
}
2013-03-05 16:48:17 +00:00
}
}
}