public MetaDataGD3(IDataProtectionProvider provider, string contentRootPath, string GD3Path, string GD3RelPath) { _protector = provider.CreateProtector("DiscChanger.NET.GD3"); settingsJsonFileName = Path.Combine(contentRootPath, "GD3settings.json"); try { settings = File.Exists(settingsJsonFileName) ? JsonSerializer.Deserialize <Settings>(File.ReadAllBytes(settingsJsonFileName)) : new Settings(); if (settings?.UserName != null && settings?.Password != null) { var u = _protector.Unprotect(settings.UserName); var p = _protector.Unprotect(settings.Password); authCD = new GD3.AuthHeader() { Username = u, Password = p }; authDVD = new GD3DVD.AuthHeader() { Username = u, Password = p }; UpdateLookupsRemaining(); } } catch (Exception e) { if (settings == null) { settings = new Settings(); } System.Diagnostics.Debug.WriteLine($"Error {e.Message} reading or processing GD3 settings from: {settingsJsonFileName}"); } this.GD3Path = GD3Path; this.GD3CDPath = Path.Combine(GD3Path, "CD"); var dirGD3CD = Directory.CreateDirectory(GD3CDPath); this.GD3DVDPath = Path.Combine(GD3Path, "DVD"); var dirGD3DVD = Directory.CreateDirectory(GD3DVDPath); this.GD3BDPath = Path.Combine(GD3Path, "BD"); var dirGD3BD = Directory.CreateDirectory(GD3BDPath); this.GD3RelPath = GD3RelPath; this.GD3CDRelPath = GD3RelPath + "/CD"; this.GD3DVDRelPath = GD3RelPath + "/DVD"; this.GD3BDRelPath = GD3RelPath + "/BD"; foreach (var fileInfo in dirGD3CD.GetFiles("Match_*.json")) { MatchCD m = JsonSerializer.Deserialize <MatchCD>(File.ReadAllText(fileInfo.FullName)); m.SetMetaDataGD3(this); string baseName = Path.GetFileNameWithoutExtension(fileInfo.Name); nameToMatchCD[baseName] = m; lengths2NameCD[m.Lengths] = baseName; } foreach (var fileInfo in dirGD3DVD.GetFiles("Match_*.json")) { MatchDVD m = JsonSerializer.Deserialize <MatchDVD>(File.ReadAllText(fileInfo.FullName)); m.SetMetaDataGD3(this); string baseName = Path.GetFileNameWithoutExtension(fileInfo.Name); nameToMatchDVD[baseName] = m; if (!String.IsNullOrEmpty(m.GraceNoteDiscID)) { graceNoteID2NameDVD[m.GraceNoteDiscID] = baseName; } } foreach (var fileInfo in dirGD3BD.GetFiles("Match_*.json")) { MatchBD m = JsonSerializer.Deserialize <MatchBD>(File.ReadAllText(fileInfo.FullName)); m.SetMetaDataGD3(this); string baseName = Path.GetFileNameWithoutExtension(fileInfo.Name); nameToMatchBD[baseName] = m; if (m.AACSDiscID != null && m.AACSDiscID.Length > 0) { AACSDiscID2NameBD[m.AACSDiscID] = baseName; } } foreach (var fileInfo in dirGD3CD.GetFiles("Meta_*.json")) { MetaDataCD m = JsonSerializer.Deserialize <MetaDataCD>(File.ReadAllText(fileInfo.FullName)); cdCodeToMetaData[m.AlbumCode] = m; } foreach (var fileInfo in dirGD3DVD.GetFiles("Meta_*.json").Concat(dirGD3BD.GetFiles("Meta_*.json"))) { MetaDataDVD m = JsonSerializer.Deserialize <MetaDataDVD>(File.ReadAllText(fileInfo.FullName)); dvdCodeDiscIDToMetaData[Tuple.Create(m.DVDCode, m.DiscID)] = m; } }
public override async Task <bool> RetrieveMetaData() { if (Matches == null || Matches.Length == 0 || metaData != null) { return(false); } GD3.AlbumCode match = Matches[SelectedMatch ?? 0]; var albumCode1 = match.AlbumCode1; if (AssociateMetaData() || metaDataGD3.authCD == null) { return(false); } var task = metaDataGD3.SoapClientCD.RetrieveAlbumAsync(metaDataGD3.authCD, albumCode1, 0); if (await Task.WhenAny(task, Task.Delay(30000)) != task) { throw new Exception("GD3 RetrieveAlbumAsync timeout: " + albumCode1); } var retrieveAlbumResponse = await task; var albumMeta = retrieveAlbumResponse?.RetrieveAlbumResult; if (albumMeta == null) { return(false); } var albumCode = albumMeta.AlbumID; if (albumCode == 0) { albumCode = albumMeta.AlbumCode; } if (albumCode == 0) { albumCode = Math.Abs(albumCode1); } string fileNameArtist = MetaDataProvider.RemoveBlacklistedCharacters(albumMeta.Artist ?? "ArtistUnk", 40); string fileNameAlbum = MetaDataProvider.RemoveBlacklistedCharacters(albumMeta.Album ?? "AlbumUnk", 80); string fileNameBase = $"Meta_CD_{fileNameArtist}_{fileNameAlbum}_{albumCode}"; string albumImageFileName = null; var ai = albumMeta.AlbumImage; if (ai != null) { if (ai.Length > 0) { albumImageFileName = await WriteImage(ai, metaDataGD3.GD3CDPath, fileNameBase); } albumMeta.AlbumImage = null; } var m = new MetaDataCD { AlbumCode = albumCode, AlbumMeta = albumMeta, ImageFileName = albumImageFileName }; using (var f = File.Create(Path.Combine(metaDataGD3.GD3CDPath, Path.ChangeExtension(fileNameBase, "json")))) { var w = new Utf8JsonWriter(f, new JsonWriterOptions { Indented = true }); JsonSerializer.Serialize(w, m); f.Close(); } metaDataGD3.cdCodeToMetaData[albumCode] = m; metaData = m; return(true); }