1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
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;
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 async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
{
return 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]);
}
}
return result;
}
private void ProcessResult(MusicAlbum item, Album result)
{
if (!string.IsNullOrWhiteSpace(result.strArtist))
{
item.AlbumArtists = new List<string> { result.strArtist };
}
if (!string.IsNullOrEmpty(result.intYearReleased))
{
item.ProductionYear = int.Parse(result.intYearReleased, _usCulture);
}
if (!string.IsNullOrEmpty(result.strGenre))
{
item.Genres = new List<string> { 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);
item.Overview = (result.strDescriptionEN ?? string.Empty).StripHtml();
}
public string Name
{
get { return "TheAudioDB"; }
}
private readonly Task _cachedTask = Task.FromResult(true);
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 <= 7)
{
return _cachedTask;
}
}
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));
using (var response = await _httpClient.Get(new HttpRequestOptions
{
Url = url,
ResourcePool = AudioDbArtistProvider.Current.AudioDbResourcePool,
CancellationToken = cancellationToken
}).ConfigureAwait(false))
{
using (var xmlFileStream = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.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 object strDescriptionDE { get; set; }
public object strDescriptionFR { get; set; }
public object strDescriptionCN { get; set; }
public object strDescriptionIT { get; set; }
public object strDescriptionJP { get; set; }
public object strDescriptionRU { get; set; }
public object strDescriptionES { get; set; }
public object strDescriptionPT { get; set; }
public object strDescriptionSE { get; set; }
public object strDescriptionNL { get; set; }
public object strDescriptionHU { get; set; }
public object strDescriptionNO { get; set; }
public object strDescriptionIL { get; set; }
public object 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();
}
}
}
|