/// <summary> /// Fetches the lastfm data. /// </summary> /// <param name="item">The item.</param> /// <param name="musicBrainzId">The music brainz id.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> protected override async Task FetchLastfmData(BaseItem item, string musicBrainzId, bool force, CancellationToken cancellationToken) { // Get artist info with provided id var url = RootUrl + string.Format("method=artist.getInfo&mbid={0}&api_key={1}&format=json", UrlEncode(musicBrainzId), ApiKey); LastfmGetArtistResult result; using (var json = await HttpClient.Get(new HttpRequestOptions { Url = url, ResourcePool = LastfmResourcePool, CancellationToken = cancellationToken, EnableHttpCompression = false }).ConfigureAwait(false)) { using (var reader = new StreamReader(json)) { var jsonText = await reader.ReadToEndAsync().ConfigureAwait(false); // Fix their bad json jsonText = jsonText.Replace("\"#text\"", "\"url\""); result = JsonSerializer.DeserializeFromString <LastfmGetArtistResult>(jsonText); } } if (result != null && result.artist != null) { LastfmHelper.ProcessArtistData(item, result.artist); } }
private void ProcessArtistData(MusicArtist artist, LastfmArtist data, string musicBrainzId) { var yearFormed = 0; if (data.bio != null) { Int32.TryParse(data.bio.yearformed, out yearFormed); if (!artist.LockedFields.Contains(MetadataFields.Overview)) { artist.Overview = (data.bio.content ?? string.Empty).StripHtml(); } if (!string.IsNullOrEmpty(data.bio.placeformed) && !artist.LockedFields.Contains(MetadataFields.ProductionLocations)) { artist.AddProductionLocation(data.bio.placeformed); } } if (yearFormed > 0) { artist.PremiereDate = new DateTime(yearFormed, 1, 1, 0, 0, 0, DateTimeKind.Utc); artist.ProductionYear = yearFormed; } string imageSize; var url = LastfmHelper.GetImageUrl(data, out imageSize); if (!string.IsNullOrEmpty(musicBrainzId) && !string.IsNullOrEmpty(url)) { LastfmHelper.SaveImageInfo(_config.ApplicationPaths, _logger, musicBrainzId, url, imageSize); } }
/// <summary> /// Fetches the lastfm data. /// </summary> /// <param name="item">The item.</param> /// <param name="musicBrainzId">The music brainz id.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> protected override async Task FetchLastfmData(BaseItem item, string musicBrainzId, bool force, CancellationToken cancellationToken) { var artist = (Artist)item; // See if we can avoid an http request by finding the matching MusicArtist entity var musicArtist = Artist.FindMusicArtist(artist, LibraryManager); if (musicArtist != null && !force) { LastfmHelper.ProcessArtistData(musicArtist, artist); } else { await base.FetchLastfmData(item, musicBrainzId, force, cancellationToken).ConfigureAwait(false); } }
protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken) { var result = await GetAlbumResult(item, cancellationToken).ConfigureAwait(false); if (result != null && result.album != null) { LastfmHelper.ProcessAlbumData(item, result.album); } BaseProviderInfo data; if (!item.ProviderData.TryGetValue(Id, out data)) { data = new BaseProviderInfo(); item.ProviderData[Id] = data; } data.FileStamp = GetComparisonData(item as MusicAlbum); }
private void ProcessAlbumData(MusicAlbum 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 DateTime release; if (DateTime.TryParse(data.releasedate, out release)) { // Lastfm sends back null as sometimes 1901, other times 0 if (release.Year > 1901) { if (!item.PremiereDate.HasValue) { item.PremiereDate = release; } if (!item.ProductionYear.HasValue) { item.ProductionYear = release.Year; } } } string imageSize; var url = LastfmHelper.GetImageUrl(data, out imageSize); var musicBrainzId = item.GetProviderId(MetadataProviders.MusicBrainzAlbum) ?? item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup); if (!string.IsNullOrEmpty(musicBrainzId) && !string.IsNullOrEmpty(url)) { LastfmHelper.SaveImageInfo(_config.ApplicationPaths, _logger, musicBrainzId, url, imageSize); } }
/// <summary> /// Fetches metadata and returns true or false indicating if any work that requires persistence was done /// </summary> /// <param name="item">The item.</param> /// <param name="force">if set to <c>true</c> [force].</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>Task{System.Boolean}.</returns> public override async Task <bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var album = (MusicAlbum)item; var result = await GetAlbumResult(album, cancellationToken).ConfigureAwait(false); if (result != null && result.album != null) { LastfmHelper.ProcessAlbumData(item, result.album); } var releaseEntryId = item.GetProviderId(MetadataProviders.Musicbrainz); if (!string.IsNullOrEmpty(releaseEntryId)) { var musicBrainzReleaseGroupId = album.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup); if (string.IsNullOrEmpty(musicBrainzReleaseGroupId)) { musicBrainzReleaseGroupId = await GetReleaseGroupId(releaseEntryId, cancellationToken).ConfigureAwait(false); album.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, musicBrainzReleaseGroupId); } } BaseProviderInfo data; if (!item.ProviderData.TryGetValue(Id, out data)) { data = new BaseProviderInfo(); item.ProviderData[Id] = data; } data.FileStamp = GetComparisonData(album); SetLastRefreshed(item, DateTime.UtcNow); return(true); }