jellyfin-server/MediaBrowser.Providers/Plugins/AudioDb/ArtistProvider.cs

283 lines
9.8 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.IO;
2019-09-10 20:37:53 +00:00
using System.Linq;
2020-01-08 16:52:50 +00:00
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
2014-05-27 14:35:29 +00:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Json;
2020-08-31 17:05:21 +00:00
using MediaBrowser.Common.Net;
2014-02-09 07:27:44 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
2020-03-02 17:07:31 +00:00
using MediaBrowser.Providers.Music;
2014-02-09 07:27:44 +00:00
2020-03-02 17:07:31 +00:00
namespace MediaBrowser.Providers.Plugins.AudioDb
2014-02-09 07:27:44 +00:00
{
public class AudioDbArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
{
2020-09-07 11:20:39 +00:00
private const string ApiKey = "195003";
public const string BaseUrl = "https://www.theaudiodb.com/api/v1/json/" + ApiKey;
2014-02-09 07:27:44 +00:00
private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem;
2020-08-17 19:10:02 +00:00
private readonly IHttpClientFactory _httpClientFactory;
2021-03-09 04:57:38 +00:00
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
2014-02-09 07:27:44 +00:00
public AudioDbArtistProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClientFactory httpClientFactory)
2014-02-09 07:27:44 +00:00
{
_config = config;
_fileSystem = fileSystem;
2020-08-17 19:10:02 +00:00
_httpClientFactory = httpClientFactory;
2014-02-09 07:27:44 +00:00
Current = this;
}
2020-09-07 11:20:39 +00:00
public static AudioDbArtistProvider Current { get; private set; }
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
public string Name => "TheAudioDB";
/// <inheritdoc />
// After musicbrainz
public int Order => 1;
/// <inheritdoc />
2017-05-26 06:48:54 +00:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
2019-09-10 20:37:53 +00:00
=> Task.FromResult(Enumerable.Empty<RemoteSearchResult>());
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
2014-02-09 07:27:44 +00:00
public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
{
var result = new MetadataResult<MusicArtist>();
var id = info.GetMusicBrainzArtistId();
if (!string.IsNullOrWhiteSpace(id))
{
await EnsureArtistInfo(id, cancellationToken).ConfigureAwait(false);
var path = GetArtistInfoPath(_config.ApplicationPaths, id);
2020-12-23 18:24:58 +00:00
await using FileStream jsonStream = File.OpenRead(path);
var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
2014-02-09 07:27:44 +00:00
if (obj != null && obj.artists != null && obj.artists.Count > 0)
{
result.Item = new MusicArtist();
result.HasMetadata = true;
2016-08-04 16:39:19 +00:00
ProcessResult(result.Item, obj.artists[0], info.MetadataLanguage);
2014-02-09 07:27:44 +00:00
}
}
return result;
}
2016-08-04 16:39:19 +00:00
private void ProcessResult(MusicArtist item, Artist result, string preferredLanguage)
2014-02-09 07:27:44 +00:00
{
2020-06-14 09:11:11 +00:00
// item.HomePageUrl = result.strWebsite;
2014-02-09 07:27:44 +00:00
2014-02-09 21:11:11 +00:00
if (!string.IsNullOrEmpty(result.strGenre))
{
2018-09-12 17:26:21 +00:00
item.Genres = new[] { result.strGenre };
2014-02-09 21:11:11 +00:00
}
2020-06-06 19:17:49 +00:00
item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist);
item.SetProviderId(MetadataProvider.MusicBrainzArtist, result.strMusicBrainzID);
2016-08-04 16:39:19 +00:00
string overview = null;
if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
{
overview = result.strBiographyDE;
}
else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
{
overview = result.strBiographyFR;
}
else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
{
overview = result.strBiographyNL;
}
else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
{
overview = result.strBiographyRU;
}
else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
{
overview = result.strBiographyIT;
}
else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
{
overview = result.strBiographyPT;
}
if (string.IsNullOrWhiteSpace(overview))
{
overview = result.strBiographyEN;
}
item.Overview = (overview ?? string.Empty).StripHtml();
2014-02-09 07:27:44 +00:00
}
internal Task EnsureArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
{
var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
2019-09-10 20:37:53 +00:00
if (fileInfo.Exists
&& (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
2014-02-09 07:27:44 +00:00
{
2019-09-10 20:37:53 +00:00
return Task.CompletedTask;
2014-02-09 07:27:44 +00:00
}
return DownloadArtistInfo(musicBrainzId, cancellationToken);
}
internal async Task DownloadArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var url = BaseUrl + "/artist-mb.php?i=" + musicBrainzId;
var path = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
2020-08-31 17:05:21 +00:00
using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken).ConfigureAwait(false);
2020-11-17 18:43:00 +00:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
2017-10-20 16:16:56 +00:00
2020-08-17 19:10:02 +00:00
Directory.CreateDirectory(Path.GetDirectoryName(path));
2021-03-07 13:43:28 +00:00
// use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
await using var xmlFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, true);
2020-08-17 19:10:02 +00:00
await stream.CopyToAsync(xmlFileStream, cancellationToken).ConfigureAwait(false);
2014-02-09 07:27:44 +00:00
}
/// <summary>
/// Gets the artist data path.
/// </summary>
/// <param name="appPaths">The application paths.</param>
/// <param name="musicBrainzArtistId">The music brainz artist identifier.</param>
/// <returns>System.String.</returns>
private static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
2019-09-10 20:37:53 +00:00
=> Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
2014-02-09 07:27:44 +00:00
/// <summary>
/// Gets the artist data path.
/// </summary>
/// <param name="appPaths">The application paths.</param>
/// <returns>System.String.</returns>
2014-02-09 21:11:11 +00:00
private static string GetArtistDataPath(IApplicationPaths appPaths)
2019-09-10 20:37:53 +00:00
=> Path.Combine(appPaths.CachePath, "audiodb-artist");
2014-02-09 07:27:44 +00:00
internal static string GetArtistInfoPath(IApplicationPaths appPaths, string musicBrainzArtistId)
{
var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
return Path.Combine(dataPath, "artist.json");
}
public class Artist
{
public string idArtist { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strArtist { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strArtistAlternate { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public object idLabel { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string intFormedYear { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string intBornYear { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public object intDiedYear { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public object strDisbanded { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strGenre { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strSubGenre { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strWebsite { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strFacebook { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strTwitter { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strBiographyEN { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strBiographyDE { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strBiographyFR { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyCN { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strBiographyIT { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyJP { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyRU { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyES { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyPT { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographySE { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyNL { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyHU { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyNO { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyIL { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strBiographyPL { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strGender { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string intMembers { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strCountry { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strCountryCode { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strArtistThumb { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strArtistLogo { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strArtistFanart { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strArtistFanart2 { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strArtistFanart3 { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strArtistBanner { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strMusicBrainzID { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public object strLastFMChart { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 07:27:44 +00:00
public string strLocked { get; set; }
}
public class RootObject
{
public List<Artist> artists { get; set; }
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
2020-08-17 19:10:02 +00:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
2014-02-09 07:27:44 +00:00
}
}