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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Json;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Providers.Music;
namespace MediaBrowser.Providers.Plugins.AudioDb
{
public class AudioDbAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
{
private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem;
private readonly IHttpClientFactory _httpClientFactory;
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.GetOptions();
public static AudioDbAlbumProvider Current;
public AudioDbAlbumProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClientFactory httpClientFactory)
{
_config = config;
_fileSystem = fileSystem;
_httpClientFactory = httpClientFactory;
Current = this;
}
/// <inheritdoc />
public string Name => "TheAudioDB";
/// <inheritdoc />
// After music brainz
public int Order => 1;
/// <inheritdoc />
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
=> Task.FromResult(Enumerable.Empty<RemoteSearchResult>());
/// <inheritdoc />
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);
await using FileStream jsonStream = File.OpenRead(path);
var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
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 (Plugin.Instance.Configuration.ReplaceAlbumName && !string.IsNullOrWhiteSpace(result.strAlbum))
{
item.Album = result.strAlbum;
}
if (!string.IsNullOrWhiteSpace(result.strArtist))
{
item.AlbumArtists = new string[] { result.strArtist };
}
if (!string.IsNullOrEmpty(result.intYearReleased))
{
item.ProductionYear = int.Parse(result.intYearReleased, CultureInfo.InvariantCulture);
}
if (!string.IsNullOrEmpty(result.strGenre))
{
item.Genres = new[] { result.strGenre };
}
item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist);
item.SetProviderId(MetadataProvider.AudioDbAlbum, result.idAlbum);
item.SetProviderId(MetadataProvider.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);
item.SetProviderId(MetadataProvider.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();
}
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);
Directory.CreateDirectory(Path.GetDirectoryName(path));
using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken).ConfigureAwait(false);
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
// use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
await using var xmlFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, true);
await stream.CopyToAsync(xmlFileStream, cancellationToken).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 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; }
}
/// <inheritdoc />
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
}
|