2020-06-19 20:24:13 +02:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 21:03:10 +01:00
|
|
|
using System;
|
2019-01-13 20:26:31 +01:00
|
|
|
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;
|
2019-01-13 20:26:31 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-07-13 23:03:57 +02:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-02-09 22:11:11 +01:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2019-01-13 20:26:31 +01:00
|
|
|
using MediaBrowser.Model.IO;
|
2014-02-20 05:53:15 +01:00
|
|
|
using MediaBrowser.Model.Providers;
|
2014-02-09 22:11:11 +01:00
|
|
|
using MediaBrowser.Model.Serialization;
|
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
|
|
|
{
|
2014-02-20 05:53:15 +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;
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
private readonly IJsonSerializer _json;
|
|
|
|
|
|
|
|
public static AudioDbAlbumProvider Current;
|
|
|
|
|
|
|
|
public AudioDbAlbumProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer json)
|
|
|
|
{
|
|
|
|
_config = config;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_httpClient = httpClient;
|
|
|
|
_json = json;
|
|
|
|
|
|
|
|
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>());
|
2014-02-20 05:53:15 +01:00
|
|
|
|
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>();
|
|
|
|
|
2020-03-08 04:10:25 +01:00
|
|
|
// TODO maybe remove when artist metadata can be disabled
|
|
|
|
if (!Plugin.Instance.Configuration.Enable)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:11:11 +01:00
|
|
|
var id = info.GetReleaseGroupId();
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(id))
|
|
|
|
{
|
|
|
|
await EnsureInfo(id, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var path = GetAlbumInfoPath(_config.ApplicationPaths, id);
|
|
|
|
|
|
|
|
var obj = _json.DeserializeFromFile<RootObject>(path);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-03-08 04:10:25 +01:00
|
|
|
if (Plugin.Instance.Configuration.ReplaceAlbumName && !string.IsNullOrWhiteSpace(result.strAlbum))
|
|
|
|
{
|
|
|
|
item.Album = result.strAlbum;
|
|
|
|
}
|
|
|
|
|
2014-08-29 02:49:25 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(result.strArtist))
|
|
|
|
{
|
2017-08-10 20:01:31 +02:00
|
|
|
item.AlbumArtists = new string[] { result.strArtist };
|
2014-08-29 02:49:25 +02:00
|
|
|
}
|
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))
|
|
|
|
{
|
2019-01-13 20:26:31 +01:00
|
|
|
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-20 05:53:15 +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);
|
2014-02-20 05:53:15 +01:00
|
|
|
|
2019-01-26 22:08:04 +01:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
2014-02-09 22:11:11 +01:00
|
|
|
|
2019-09-10 22:37:53 +02:00
|
|
|
using (var httpResponse = await _httpClient.SendAsync(
|
|
|
|
new HttpRequestOptions
|
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken
|
|
|
|
},
|
2020-01-08 17:52:50 +01:00
|
|
|
HttpMethod.Get).ConfigureAwait(false))
|
2019-09-10 22:37:53 +02:00
|
|
|
using (var response = httpResponse.Content)
|
2020-01-08 17:52:50 +01:00
|
|
|
using (var xmlFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true))
|
2014-02-09 22:11:11 +01:00
|
|
|
{
|
2019-09-10 22:37:53 +02:00
|
|
|
await response.CopyToAsync(xmlFileStream).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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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; }
|
|
|
|
}
|
2014-02-20 05:53:15 +01:00
|
|
|
|
2019-09-10 22:37:53 +02:00
|
|
|
/// <inheritdoc />
|
2014-02-20 05:53:15 +01:00
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2014-02-09 22:11:11 +01:00
|
|
|
}
|
|
|
|
}
|