Example #1
0
        protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken)
        {
            // Get artist info with provided id
            var url = RootUrl + string.Format("method=artist.getInfo&mbid={0}&api_key={1}&format=json", UrlEncode(id), ApiKey);

            LastfmGetArtistResult result = null;

            try
            {
                using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
                {
                    result = JsonSerializer.DeserializeFromStream <LastfmGetArtistResult>(json);
                }
            }
            catch (HttpException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new LastfmProviderException(string.Format("Unable to retrieve artist info for {0} with id {1}", item.Name, id));
                }
                throw;
            }

            if (result != null && result.artist != null)
            {
                LastfmHelper.ProcessArtistData(item, result.artist);
                //And save locally if indicated
                if (ConfigurationManager.Configuration.SaveLocalMeta)
                {
                    var ms = new MemoryStream();
                    JsonSerializer.SerializeToStream(result.artist, ms);

                    cancellationToken.ThrowIfCancellationRequested();

                    await _providerManager.SaveToLibraryFilesystem(item, Path.Combine(item.MetaLocation, LocalMetaFileName), ms, cancellationToken).ConfigureAwait(false);
                }
            }
        }
Example #2
0
        public override Task <bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                var entry = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, LastfmHelper.LocalArtistMetaFileName));
                if (entry.HasValue)
                {
                    // read in our saved meta and pass to processing function
                    var data = JsonSerializer.DeserializeFromFile <LastfmArtist>(entry.Value.Path);

                    cancellationToken.ThrowIfCancellationRequested();

                    LastfmHelper.ProcessArtistData(item, data);

                    item.SetProviderId(MetadataProviders.Musicbrainz, data.mbid);

                    SetLastRefreshed(item, DateTime.UtcNow);
                    return true;
                }
                return false;
            }));
        }