| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 | using MediaBrowser.Common.Configuration;using MediaBrowser.Common.Extensions;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;using MediaBrowser.Model.Serialization;using System;using System.Collections.Generic;using System.Globalization;using System.IO;using System.Threading;using System.Threading.Tasks;using MediaBrowser.Controller.IO;using MediaBrowser.Model.IO;namespace MediaBrowser.Providers.Music{    public class AudioDbAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder    {        private readonly IServerConfigurationManager _config;        private readonly IFileSystem _fileSystem;        private readonly IHttpClient _httpClient;        private readonly IJsonSerializer _json;        public static AudioDbAlbumProvider Current;        private readonly CultureInfo _usCulture = new CultureInfo("en-US");        public AudioDbAlbumProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer json)        {            _config = config;            _fileSystem = fileSystem;            _httpClient = httpClient;            _json = json;            Current = this;        }        public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)        {            return Task.FromResult((IEnumerable<RemoteSearchResult>)new List<RemoteSearchResult>());        }        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;                    ProcessResult(result.Item, obj.album[0], info.MetadataLanguage);                }            }            return result;        }        private void ProcessResult(MusicAlbum item, Album result, string preferredLanguage)        {            if (!string.IsNullOrWhiteSpace(result.strArtist))            {                item.AlbumArtists = new string[] { result.strArtist };            }            if (!string.IsNullOrEmpty(result.intYearReleased))            {                item.ProductionYear = int.Parse(result.intYearReleased, _usCulture);            }            if (!string.IsNullOrEmpty(result.strGenre))            {                item.Genres = new [] { result.strGenre };            }            item.SetProviderId(MetadataProviders.AudioDbArtist, result.idArtist);            item.SetProviderId(MetadataProviders.AudioDbAlbum, result.idAlbum);            item.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);            item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, result.strMusicBrainzID);            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();        }        public string Name        {            get { return "TheAudioDB"; }        }        internal Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)        {            var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);            var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);            if (fileInfo.Exists)            {                if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)                {                    return Task.CompletedTask;                }            }            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);			_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));            using (var httpResponse = await _httpClient.SendAsync(new HttpRequestOptions            {                Url = url,                CancellationToken = cancellationToken            }, "GET").ConfigureAwait(false))            {                using (var response = httpResponse.Content)                {                    using (var xmlFileStream = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true))                    {                        await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);                    }                }            }        }        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 int Order        {            get            {                // After music brainz                return 1;            }        }        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; }            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; }            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; }        }        public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)        {            throw new NotImplementedException();        }    }}
 |