jellyfin/MediaBrowser.Providers/Music/AudioDbArtistProvider.cs

266 lines
9.8 KiB
C#
Raw Normal View History

2014-02-09 00:27:44 -07:00
using MediaBrowser.Common.Configuration;
2014-05-27 07:35:29 -07:00
using MediaBrowser.Common.Extensions;
2014-02-09 00:27:44 -07:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
2014-02-09 00:27:44 -07:00
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2017-05-25 23:48:54 -07:00
2016-10-25 12:02:04 -07:00
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2014-02-09 00:27:44 -07:00
namespace MediaBrowser.Providers.Music
{
public class AudioDbArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
{
private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem;
private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _json;
public static AudioDbArtistProvider Current;
private const string ApiKey = "49jhsf8248yfahka89724011";
2018-09-12 10:26:21 -07:00
public const string BaseUrl = "https://www.theaudiodb.com/api/v1/json/" + ApiKey;
2014-02-09 00:27:44 -07:00
public AudioDbArtistProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer json)
{
_config = config;
_fileSystem = fileSystem;
_httpClient = httpClient;
_json = json;
Current = this;
}
2017-05-25 23:48:54 -07:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
{
2017-05-25 23:48:54 -07:00
return Task.FromResult((IEnumerable<RemoteSearchResult>)new List<RemoteSearchResult>());
}
2014-02-09 00:27:44 -07: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);
var obj = _json.DeserializeFromFile<RootObject>(path);
if (obj != null && obj.artists != null && obj.artists.Count > 0)
{
result.Item = new MusicArtist();
result.HasMetadata = true;
2016-08-04 09:39:19 -07:00
ProcessResult(result.Item, obj.artists[0], info.MetadataLanguage);
2014-02-09 00:27:44 -07:00
}
}
return result;
}
2016-08-04 09:39:19 -07:00
private void ProcessResult(MusicArtist item, Artist result, string preferredLanguage)
2014-02-09 00:27:44 -07:00
{
2018-09-12 10:26:21 -07:00
//item.HomePageUrl = result.strWebsite;
2014-02-09 00:27:44 -07:00
2014-02-09 14:11:11 -07:00
if (!string.IsNullOrEmpty(result.strGenre))
{
2018-09-12 10:26:21 -07:00
item.Genres = new[] { result.strGenre };
2014-02-09 14:11:11 -07:00
}
2014-02-09 00:27:44 -07:00
item.SetProviderId(MetadataProviders.AudioDbArtist, result.idArtist);
item.SetProviderId(MetadataProviders.MusicBrainzArtist, result.strMusicBrainzID);
2016-08-04 09:39:19 -07: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 00:27:44 -07:00
}
public string Name
{
get { return "TheAudioDB"; }
}
internal Task EnsureArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
{
var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
if (fileInfo.Exists)
{
2018-09-12 10:26:21 -07:00
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
2014-02-09 00:27:44 -07:00
{
2018-09-12 10:26:21 -07:00
return Task.CompletedTask;
2014-02-09 00:27:44 -07: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);
2017-10-20 09:16:56 -07:00
using (var httpResponse = await _httpClient.SendAsync(new HttpRequestOptions
2014-02-09 00:27:44 -07:00
{
Url = url,
2016-10-31 11:39:41 -07:00
CancellationToken = cancellationToken,
BufferContent = true
2014-02-09 00:27:44 -07:00
2017-10-20 09:16:56 -07:00
}, "GET").ConfigureAwait(false))
2014-02-09 00:27:44 -07:00
{
2017-10-20 09:16:56 -07:00
using (var response = httpResponse.Content)
2014-02-09 00:27:44 -07:00
{
2017-10-20 09:16:56 -07:00
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
using (var xmlFileStream = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true))
{
await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
}
2014-02-09 00:27:44 -07: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)
{
var dataPath = Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
return dataPath;
}
/// <summary>
/// Gets the artist data path.
/// </summary>
/// <param name="appPaths">The application paths.</param>
/// <returns>System.String.</returns>
2014-02-09 14:11:11 -07:00
private static string GetArtistDataPath(IApplicationPaths appPaths)
2014-02-09 00:27:44 -07:00
{
2014-02-09 14:11:11 -07:00
var dataPath = Path.Combine(appPaths.CachePath, "audiodb-artist");
2014-02-09 00:27:44 -07:00
return dataPath;
}
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; }
public string strArtist { get; set; }
public string strArtistAlternate { get; set; }
public object idLabel { get; set; }
public string intFormedYear { get; set; }
public string intBornYear { get; set; }
public object intDiedYear { get; set; }
public object strDisbanded { get; set; }
public string strGenre { get; set; }
public string strSubGenre { get; set; }
public string strWebsite { get; set; }
public string strFacebook { get; set; }
public string strTwitter { get; set; }
public string strBiographyEN { get; set; }
public string strBiographyDE { get; set; }
public string strBiographyFR { get; set; }
2016-08-04 09:39:19 -07:00
public string strBiographyCN { get; set; }
2014-02-09 00:27:44 -07:00
public string strBiographyIT { get; set; }
2016-08-04 09:39:19 -07:00
public string strBiographyJP { get; set; }
public string strBiographyRU { get; set; }
public string strBiographyES { get; set; }
public string strBiographyPT { get; set; }
public string strBiographySE { get; set; }
public string strBiographyNL { get; set; }
public string strBiographyHU { get; set; }
public string strBiographyNO { get; set; }
public string strBiographyIL { get; set; }
public string strBiographyPL { get; set; }
2014-02-09 00:27:44 -07:00
public string strGender { get; set; }
public string intMembers { get; set; }
public string strCountry { get; set; }
public string strCountryCode { get; set; }
public string strArtistThumb { get; set; }
public string strArtistLogo { get; set; }
public string strArtistFanart { get; set; }
public string strArtistFanart2 { get; set; }
public string strArtistFanart3 { get; set; }
public string strArtistBanner { get; set; }
public string strMusicBrainzID { get; set; }
public object strLastFMChart { get; set; }
public string strLocked { get; set; }
}
public class RootObject
{
public List<Artist> artists { get; set; }
}
public int Order
{
get
{
// After musicbrainz
return 1;
}
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
2014-02-09 00:27:44 -07:00
}
}