You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
9.5 KiB
253 lines
9.5 KiB
6 years ago
|
using System;
|
||
6 years ago
|
using System.Collections.Generic;
|
||
|
using System.Globalization;
|
||
|
using System.IO;
|
||
5 years ago
|
using System.Linq;
|
||
5 years ago
|
using System.Net.Http;
|
||
6 years ago
|
using System.Threading;
|
||
|
using System.Threading.Tasks;
|
||
|
using MediaBrowser.Common.Configuration;
|
||
10 years ago
|
using MediaBrowser.Common.Extensions;
|
||
11 years ago
|
using MediaBrowser.Common.Net;
|
||
|
using MediaBrowser.Controller.Configuration;
|
||
|
using MediaBrowser.Controller.Entities.Audio;
|
||
|
using MediaBrowser.Controller.Providers;
|
||
|
using MediaBrowser.Model.Entities;
|
||
6 years ago
|
using MediaBrowser.Model.IO;
|
||
11 years ago
|
using MediaBrowser.Model.Providers;
|
||
11 years ago
|
using MediaBrowser.Model.Serialization;
|
||
5 years ago
|
using MediaBrowser.Providers.Music;
|
||
11 years ago
|
|
||
5 years ago
|
namespace MediaBrowser.Providers.Plugins.AudioDb
|
||
11 years ago
|
{
|
||
11 years ago
|
public class AudioDbAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
|
||
11 years ago
|
{
|
||
|
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;
|
||
|
}
|
||
|
|
||
5 years ago
|
/// <inheritdoc />
|
||
|
public string Name => "TheAudioDB";
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
// After music brainz
|
||
|
public int Order => 1;
|
||
|
|
||
|
/// <inheritdoc />
|
||
8 years ago
|
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
|
||
5 years ago
|
=> Task.FromResult(Enumerable.Empty<RemoteSearchResult>());
|
||
11 years ago
|
|
||
5 years ago
|
/// <inheritdoc />
|
||
11 years ago
|
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);
|
||
|
|
||
|
var obj = _json.DeserializeFromFile<RootObject>(path);
|
||
|
|
||
|
if (obj != null && obj.album != null && obj.album.Count > 0)
|
||
|
{
|
||
|
result.Item = new MusicAlbum();
|
||
|
result.HasMetadata = true;
|
||
8 years ago
|
ProcessResult(result.Item, obj.album[0], info.MetadataLanguage);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
8 years ago
|
private void ProcessResult(MusicAlbum item, Album result, string preferredLanguage)
|
||
11 years ago
|
{
|
||
10 years ago
|
if (!string.IsNullOrWhiteSpace(result.strArtist))
|
||
|
{
|
||
7 years ago
|
item.AlbumArtists = new string[] { result.strArtist };
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
|
if (!string.IsNullOrEmpty(result.intYearReleased))
|
||
|
{
|
||
5 years ago
|
item.ProductionYear = int.Parse(result.intYearReleased, CultureInfo.InvariantCulture);
|
||
11 years ago
|
}
|
||
|
|
||
|
if (!string.IsNullOrEmpty(result.strGenre))
|
||
|
{
|
||
6 years ago
|
item.Genres = new[] { result.strGenre };
|
||
11 years ago
|
}
|
||
|
|
||
|
item.SetProviderId(MetadataProviders.AudioDbArtist, result.idArtist);
|
||
|
item.SetProviderId(MetadataProviders.AudioDbAlbum, result.idAlbum);
|
||
|
|
||
|
item.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);
|
||
|
item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, result.strMusicBrainzID);
|
||
10 years ago
|
|
||
8 years ago
|
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();
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
internal Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
|
||
|
{
|
||
|
var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
|
||
|
|
||
|
var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
|
||
|
|
||
|
if (fileInfo.Exists)
|
||
|
{
|
||
6 years ago
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
|
||
11 years ago
|
{
|
||
6 years ago
|
return Task.CompletedTask;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
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);
|
||
11 years ago
|
|
||
6 years ago
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||
11 years ago
|
|
||
5 years ago
|
using (var httpResponse = await _httpClient.SendAsync(
|
||
|
new HttpRequestOptions
|
||
|
{
|
||
|
Url = url,
|
||
|
CancellationToken = cancellationToken
|
||
|
},
|
||
5 years ago
|
HttpMethod.Get).ConfigureAwait(false))
|
||
5 years ago
|
using (var response = httpResponse.Content)
|
||
5 years ago
|
using (var xmlFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true))
|
||
11 years ago
|
{
|
||
5 years ago
|
await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
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; }
|
||
|
public string idArtist { get; set; }
|
||
|
public string strAlbum { get; set; }
|
||
|
public string strArtist { get; set; }
|
||
|
public string intYearReleased { get; set; }
|
||
|
public string strGenre { get; set; }
|
||
|
public string strSubGenre { get; set; }
|
||
|
public string strReleaseFormat { get; set; }
|
||
|
public string intSales { get; set; }
|
||
|
public string strAlbumThumb { get; set; }
|
||
|
public string strAlbumCDart { get; set; }
|
||
|
public string strDescriptionEN { get; set; }
|
||
8 years ago
|
public string strDescriptionDE { get; set; }
|
||
|
public string strDescriptionFR { get; set; }
|
||
|
public string strDescriptionCN { get; set; }
|
||
|
public string strDescriptionIT { get; set; }
|
||
|
public string strDescriptionJP { get; set; }
|
||
|
public string strDescriptionRU { get; set; }
|
||
|
public string strDescriptionES { get; set; }
|
||
|
public string strDescriptionPT { get; set; }
|
||
|
public string strDescriptionSE { get; set; }
|
||
|
public string strDescriptionNL { get; set; }
|
||
|
public string strDescriptionHU { get; set; }
|
||
|
public string strDescriptionNO { get; set; }
|
||
|
public string strDescriptionIL { get; set; }
|
||
|
public string strDescriptionPL { get; set; }
|
||
11 years ago
|
public object intLoved { get; set; }
|
||
|
public object intScore { get; set; }
|
||
|
public string strReview { get; set; }
|
||
|
public object strMood { get; set; }
|
||
|
public object strTheme { get; set; }
|
||
|
public object strSpeed { get; set; }
|
||
|
public object strLocation { get; set; }
|
||
|
public string strMusicBrainzID { get; set; }
|
||
|
public string strMusicBrainzArtistID { get; set; }
|
||
|
public object strItunesID { get; set; }
|
||
|
public object strAmazonID { get; set; }
|
||
|
public string strLocked { get; set; }
|
||
|
}
|
||
|
|
||
|
public class RootObject
|
||
|
{
|
||
|
public List<Album> album { get; set; }
|
||
|
}
|
||
11 years ago
|
|
||
5 years ago
|
/// <inheritdoc />
|
||
11 years ago
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|