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

298 lines
10 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CA1002, CS1591, SA1300
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
2019-09-10 22:37:53 +02:00
using System.Linq;
2020-01-08 17:52:50 +01:00
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
2021-07-23 02:33:19 +02:00
using Jellyfin.Extensions.Json;
using MediaBrowser.Common.Configuration;
2014-07-13 23:03:57 +02:00
using MediaBrowser.Common.Extensions;
2020-08-31 19:05:21 +02:00
using MediaBrowser.Common.Net;
2014-02-09 22:11:11 +01: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 18:07:31 +01:00
using MediaBrowser.Providers.Music;
2014-02-09 22:11:11 +01:00
2020-03-02 18:07:31 +01:00
namespace MediaBrowser.Providers.Plugins.AudioDb
2014-02-09 22:11:11 +01:00
{
public class AudioDbAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
2014-02-09 22:11:11 +01:00
{
private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem;
2020-08-17 21:10:02 +02:00
private readonly IHttpClientFactory _httpClientFactory;
2021-03-09 05:57:38 +01:00
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
2014-02-09 22:11:11 +01:00
#pragma warning disable SA1401, CA2211
2014-02-09 22:11:11 +01:00
public static AudioDbAlbumProvider Current;
#pragma warning restore SA1401, CA2211
2014-02-09 22:11:11 +01:00
public AudioDbAlbumProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClientFactory httpClientFactory)
2014-02-09 22:11:11 +01:00
{
_config = config;
_fileSystem = fileSystem;
2020-08-17 21:10:02 +02:00
_httpClientFactory = httpClientFactory;
2014-02-09 22:11:11 +01:00
Current = this;
}
2019-09-10 22:37:53 +02:00
/// <inheritdoc />
public string Name => "TheAudioDB";
/// <inheritdoc />
// After music brainz
public int Order => 1;
/// <inheritdoc />
2017-05-26 08:48:54 +02:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
2019-09-10 22:37:53 +02:00
=> Task.FromResult(Enumerable.Empty<RemoteSearchResult>());
2019-09-10 22:37:53 +02:00
/// <inheritdoc />
2014-02-09 22:11:11 +01: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);
await using FileStream jsonStream = AsyncFile.OpenRead(path);
var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
2014-02-09 22:11:11 +01:00
if (obj != null && obj.album != null && obj.album.Count > 0)
{
result.Item = new MusicAlbum();
result.HasMetadata = true;
2016-08-04 18:39:19 +02:00
ProcessResult(result.Item, obj.album[0], info.MetadataLanguage);
2014-02-09 22:11:11 +01:00
}
}
return result;
}
2016-08-04 18:39:19 +02:00
private void ProcessResult(MusicAlbum item, Album result, string preferredLanguage)
2014-02-09 22:11:11 +01:00
{
if (Plugin.Instance.Configuration.ReplaceAlbumName && !string.IsNullOrWhiteSpace(result.strAlbum))
{
item.Album = result.strAlbum;
}
if (!string.IsNullOrWhiteSpace(result.strArtist))
{
2017-08-10 20:01:31 +02:00
item.AlbumArtists = new string[] { result.strArtist };
}
2014-02-09 22:11:11 +01:00
if (!string.IsNullOrEmpty(result.intYearReleased))
{
2019-09-10 22:37:53 +02:00
item.ProductionYear = int.Parse(result.intYearReleased, CultureInfo.InvariantCulture);
2014-02-09 22:11:11 +01:00
}
if (!string.IsNullOrEmpty(result.strGenre))
{
item.Genres = new[] { result.strGenre };
2014-02-09 22:11:11 +01:00
}
2020-06-06 21:17:49 +02:00
item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist);
item.SetProviderId(MetadataProvider.AudioDbAlbum, result.idAlbum);
2014-02-09 22:11:11 +01:00
2020-06-06 21:17:49 +02:00
item.SetProviderId(MetadataProvider.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);
item.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, result.strMusicBrainzID);
2014-07-13 23:03:57 +02:00
2016-08-04 18:39:19 +02: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 22:11:11 +01:00
}
2014-02-09 22:11:11 +01: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 19:26:21 +02:00
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
2014-02-09 22:11:11 +01:00
{
2018-09-12 19:26:21 +02:00
return Task.CompletedTask;
2014-02-09 22:11:11 +01: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 22:11:11 +01:00
2020-08-31 19:05:21 +02:00
using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken).ConfigureAwait(false);
2020-11-17 19:43:00 +01:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
var fileStreamOptions = AsyncFile.WriteOptions;
fileStreamOptions.Mode = FileMode.Create;
fileStreamOptions.PreallocationSize = stream.Length;
await using var xmlFileStream = new FileStream(path, fileStreamOptions);
2020-08-17 21:10:02 +02:00
await stream.CopyToAsync(xmlFileStream, cancellationToken).ConfigureAwait(false);
2014-02-09 22:11:11 +01: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 02:33:19 +02:00
/// <inheritdoc />
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
#pragma warning disable CA1034, CA2227
2014-02-09 22:11:11 +01:00
public class Album
{
public string idAlbum { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string idArtist { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strAlbum { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strArtist { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string intYearReleased { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strGenre { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strSubGenre { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strReleaseFormat { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string intSales { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strAlbumThumb { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strAlbumCDart { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strDescriptionEN { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionDE { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionFR { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionCN { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionIT { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionJP { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionRU { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionES { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionPT { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionSE { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionNL { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionHU { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionNO { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionIL { get; set; }
2020-06-15 23:43:52 +02:00
2016-08-04 18:39:19 +02:00
public string strDescriptionPL { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public object intLoved { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public object intScore { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strReview { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public object strMood { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public object strTheme { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public object strSpeed { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public object strLocation { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strMusicBrainzID { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strMusicBrainzArtistID { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public object strItunesID { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public object strAmazonID { get; set; }
2020-06-15 23:43:52 +02:00
2014-02-09 22:11:11 +01:00
public string strLocked { get; set; }
}
public class RootObject
{
public List<Album> album { get; set; }
}
}
}