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

292 lines
10 KiB
C#
Raw Normal View History

2021-07-23 00:33:19 +00:00
#pragma warning disable CS1591, SA1300
using System;
using System.Collections.Generic;
using System.Globalization;
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;
2021-07-23 00:33:19 +00:00
using Jellyfin.Extensions.Json;
using MediaBrowser.Common.Configuration;
2014-07-13 21:03:57 +00:00
using MediaBrowser.Common.Extensions;
2020-08-31 17:05:21 +00:00
using MediaBrowser.Common.Net;
2014-02-09 21:11:11 +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 21:11:11 +00:00
2020-03-02 17:07:31 +00:00
namespace MediaBrowser.Providers.Plugins.AudioDb
2014-02-09 21:11:11 +00:00
{
public class AudioDbAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
2014-02-09 21:11:11 +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 21:11:11 +00:00
2021-07-23 00:33:19 +00:00
#pragma warning disable SA1401
2014-02-09 21:11:11 +00:00
public static AudioDbAlbumProvider Current;
2021-07-23 00:33:19 +00:00
#pragma warning restore SA1401
2014-02-09 21:11:11 +00:00
public AudioDbAlbumProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClientFactory httpClientFactory)
2014-02-09 21:11:11 +00:00
{
_config = config;
_fileSystem = fileSystem;
2020-08-17 19:10:02 +00:00
_httpClientFactory = httpClientFactory;
2014-02-09 21:11:11 +00:00
Current = this;
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
public string Name => "TheAudioDB";
/// <inheritdoc />
// After music brainz
public int Order => 1;
/// <inheritdoc />
2017-05-26 06:48:54 +00:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo 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 21:11:11 +00:00
public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, CancellationToken cancellationToken)
{
var result = new MetadataResult<MusicAlbum>();
var id = info.GetReleaseGroupId();
if (!string.IsNullOrWhiteSpace(id))
{
await EnsureInfo(id, cancellationToken).ConfigureAwait(false);
var path = GetAlbumInfoPath(_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 21:11:11 +00:00
if (obj != null && obj.album != null && obj.album.Count > 0)
{
result.Item = new MusicAlbum();
result.HasMetadata = true;
2016-08-04 16:39:19 +00:00
ProcessResult(result.Item, obj.album[0], info.MetadataLanguage);
2014-02-09 21:11:11 +00:00
}
}
return result;
}
2016-08-04 16:39:19 +00:00
private void ProcessResult(MusicAlbum item, Album result, string preferredLanguage)
2014-02-09 21:11:11 +00:00
{
if (Plugin.Instance.Configuration.ReplaceAlbumName && !string.IsNullOrWhiteSpace(result.strAlbum))
{
item.Album = result.strAlbum;
}
if (!string.IsNullOrWhiteSpace(result.strArtist))
{
2017-08-10 18:01:31 +00:00
item.AlbumArtists = new string[] { result.strArtist };
}
2014-02-09 21:11:11 +00:00
if (!string.IsNullOrEmpty(result.intYearReleased))
{
2019-09-10 20:37:53 +00:00
item.ProductionYear = int.Parse(result.intYearReleased, CultureInfo.InvariantCulture);
2014-02-09 21:11:11 +00:00
}
if (!string.IsNullOrEmpty(result.strGenre))
{
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.AudioDbAlbum, result.idAlbum);
2014-02-09 21:11:11 +00:00
2020-06-06 19:17:49 +00:00
item.SetProviderId(MetadataProvider.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);
item.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, result.strMusicBrainzID);
2014-07-13 21:03:57 +00:00
2016-08-04 16:39:19 +00:00
string overview = null;
if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
{
overview = result.strDescriptionDE;
}
else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
{
overview = result.strDescriptionFR;
}
else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
{
overview = result.strDescriptionNL;
}
else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
{
overview = result.strDescriptionRU;
}
else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
{
overview = result.strDescriptionIT;
}
else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
{
overview = result.strDescriptionPT;
}
if (string.IsNullOrWhiteSpace(overview))
{
overview = result.strDescriptionEN;
}
item.Overview = (overview ?? string.Empty).StripHtml();
2014-02-09 21:11:11 +00:00
}
2014-02-09 21:11:11 +00:00
internal Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
{
var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
if (fileInfo.Exists)
{
2018-09-12 17:26:21 +00:00
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
2014-02-09 21:11:11 +00:00
{
2018-09-12 17:26:21 +00:00
return Task.CompletedTask;
2014-02-09 21:11:11 +00:00
}
}
return DownloadInfo(musicBrainzReleaseGroupId, cancellationToken);
}
internal async Task DownloadInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var url = AudioDbArtistProvider.BaseUrl + "/album-mb.php?i=" + musicBrainzReleaseGroupId;
var path = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
Directory.CreateDirectory(Path.GetDirectoryName(path));
2014-02-09 21:11:11 +00:00
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);
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 21:11:11 +00:00
}
private static string GetAlbumDataPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
{
var dataPath = Path.Combine(GetAlbumDataPath(appPaths), musicBrainzReleaseGroupId);
return dataPath;
}
private static string GetAlbumDataPath(IApplicationPaths appPaths)
{
var dataPath = Path.Combine(appPaths.CachePath, "audiodb-album");
return dataPath;
}
internal static string GetAlbumInfoPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
{
var dataPath = GetAlbumDataPath(appPaths, musicBrainzReleaseGroupId);
return Path.Combine(dataPath, "album.json");
}
2021-07-23 00:33:19 +00:00
/// <inheritdoc />
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
2014-02-09 21:11:11 +00:00
public class Album
{
public string idAlbum { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string idArtist { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strAlbum { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strArtist { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string intYearReleased { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strGenre { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strSubGenre { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strReleaseFormat { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string intSales { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strAlbumThumb { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strAlbumCDart { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strDescriptionEN { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionDE { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionFR { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionCN { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionIT { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionJP { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionRU { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionES { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionPT { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionSE { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionNL { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionHU { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionNO { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionIL { get; set; }
2020-06-15 21:43:52 +00:00
2016-08-04 16:39:19 +00:00
public string strDescriptionPL { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public object intLoved { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public object intScore { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strReview { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public object strMood { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public object strTheme { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public object strSpeed { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public object strLocation { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strMusicBrainzID { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strMusicBrainzArtistID { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public object strItunesID { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public object strAmazonID { get; set; }
2020-06-15 21:43:52 +00:00
2014-02-09 21:11:11 +00:00
public string strLocked { get; set; }
}
public class RootObject
{
public List<Album> album { get; set; }
}
}
}