internal async Task <Match> RetrieveMatch(Disc d) { if (d is DiscSony ds) { if (ds.IsCD()) { var lengths = d.StandardizedCDTableOfContents(); if (lengths != null) { if (lengths2NameCD.TryGetValue(lengths, out string name)) { return(nameToMatchCD[name]); } if (authCD == null) { return(null); } int frameCount = lengths.Length; string graceNoteDiscID = (ds as DiscSonyBD)?.DiscIDData?.GraceNoteDiscID; string cdID; if (String.IsNullOrEmpty(graceNoteDiscID)) { graceNoteDiscID = null; int initial = 150; var cumulative = lengths.Aggregate(new List <int>(frameCount + 1) { initial }, (c, nxt) => { c.Add(c.Last() + nxt); return(c); }); cdID = String.Join(' ', cumulative); } else { cdID = graceNoteDiscID; } var task = SoapClientCD.MatchCDIDAsync(authCD, cdID); if (await Task.WhenAny(task, Task.Delay(30000)) != task) { throw new Exception("GD3 MatchCDIDAsync timeout: " + cdID); } var matchCDIDResponse = await task; var matchesCD = matchCDIDResponse?.MatchCDIDResult; GD3.AlbumCode[] matchesCDNonNull; if (matchesCD != null && (matchesCDNonNull = matchesCD.Where(m => m != null).ToArray()) != null && matchesCDNonNull.Length > 0) { var matchCD = new MatchCD(this) { SelectedMatch = 0, GraceNoteDiscID = graceNoteDiscID, Lengths = lengths, Matches = matchesCDNonNull }; var firstMatch = matchesCDNonNull.First(); string fileNameArtist = MetaDataProvider.RemoveBlacklistedCharacters(firstMatch.Artist ?? "ArtistUnk", 40); string fileNameAlbum = MetaDataProvider.RemoveBlacklistedCharacters(firstMatch.Album ?? "TitleUnk", 80); string fileName = MakeUniqueFileName(nameToMatchCD, $"Match_CD_{fileNameArtist}_{fileNameAlbum}_{firstMatch.MatchType}"); using (var f = File.Create(Path.Combine(GD3CDPath, Path.ChangeExtension(fileName, "json")))) { var w = new Utf8JsonWriter(f, new JsonWriterOptions { Indented = true }); JsonSerializer.Serialize(w, matchCD); f.Close(); } nameToMatchCD[fileName] = matchCD; lengths2NameCD[lengths] = fileName; matchCD.AssociateMetaData(); return(matchCD); } } } else if (ds.IsDVD() || ds.IsBD()) { MatchDVDBase match = null; GD3DVD.DVDMatch[] matches = null; IDictionary nameToMatch = null; string path = null;; if (ds.IsDVD()) { string gtoc = (ds as DiscSonyBD)?.DiscIDData?.GraceNoteDiscID; if (gtoc == null) { gtoc = TableOfContents2GTOC(ds.TableOfContents); } if (String.IsNullOrEmpty(gtoc)) { return(null); } if (graceNoteID2NameDVD.TryGetValue(gtoc, out string name)) { return(nameToMatchDVD[name]); } if (authDVD == null) { return(null); } var task = SoapClientDVD.MatchDVDID_vToc2Async(authDVD, gtoc); if (await Task.WhenAny(task, Task.Delay(30000)) != task) { throw new Exception("GD3 MatchDVDID_vToc2Async timeout: " + gtoc); } var matchDVDID_vToc2Response = await task; matches = matchDVDID_vToc2Response?.MatchDVDID_vToc2Result?.DVDMatches; path = GD3DVDPath; match = new MatchDVD(this) { SelectedMatch = 0, GraceNoteDiscID = gtoc }; nameToMatch = nameToMatchDVD; } else if (ds.IsBD() && d is DiscSonyBD bdd) { var AACSDiscID = bdd.DiscIDData?.AACSDiscID; if (AACSDiscID == null || AACSDiscID.Length == 0) { return(null); } if (AACSDiscID2NameBD.TryGetValue(AACSDiscID, out string name)) { return(nameToMatchBD[name]); } if (authDVD == null) { return(null); } var task = SoapClientDVD.Match_AACSAsync(authDVD, bdd.DiscIDData.AACSDiscID); if (await Task.WhenAny(task, Task.Delay(30000)) != task) { throw new Exception("GD3 Match_AACSAsync timeout"); } var match_AACSResponse = await task; matches = match_AACSResponse?.Match_AACSResult?.DVDMatches; path = GD3BDPath; match = new MatchBD(this) { SelectedMatch = 0, AACSDiscID = AACSDiscID }; nameToMatch = nameToMatchBD; } GD3DVD.DVDMatch[] matchesNonNull; if (matches != null && (matchesNonNull = matches.Where(m => m != null).ToArray()).Length > 0) { string[] frontCoverImages = new string[matchesNonNull.Length]; int i = 0; foreach (var m in matchesNonNull) { string mediaType2 = MetaDataProvider.RemoveBlacklistedCharacters(m.MediaType ?? "NoMediaType", 11); string title2 = MetaDataProvider.RemoveBlacklistedCharacters(m.DVDTitle ?? "NoTitle", 80); var frontCoverImage = m.FrontCoverImage; if (frontCoverImage != null && frontCoverImage.Length > 0) { var frontCoverImageFileName = await WriteImage(frontCoverImage, path, $"MatchFrontCover_{mediaType2}_{title2}_{m.DVDCode}_{m.DiscID}"); frontCoverImages[i] = frontCoverImageFileName; match.FrontCoverImages = frontCoverImages; m.FrontCoverImage = null;//erase base64-encoded thumbnail to save space } var extraImages = m.ExtraImages; if (extraImages != null) { foreach (var ei in extraImages) { try { var task2 = SoapClientDVD.RetrieveExtraMovieImageAsync(authDVD, ei.MovieImageID, false); if (await Task.WhenAny(task2, Task.Delay(30000)) != task2) { throw new Exception("GD3 RetrieveExtraMovieImageAsync timeout: " + ei.MovieImageID); } var retrieveExtraMovieImageResponse = await task2; byte[] b = retrieveExtraMovieImageResponse?.RetrieveExtraMovieImageResult; if (b != null && b.Length > 0) { var movieImageFileName = await WriteImage(b, path, $"MovieImage_{ei.MovieImageID}"); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine($"Error retrieving Movie Image {ei.MovieImageID}: {e.Message}"); } } } i++; } match.Matches = matchesNonNull; var firstMatch = matchesNonNull.First(); string mediaType = MetaDataProvider.RemoveBlacklistedCharacters(firstMatch.MediaType ?? "NoMediaType", 11); string title = MetaDataProvider.RemoveBlacklistedCharacters(firstMatch.DVDTitle ?? "NoTitle", 80); string fileName = MakeUniqueFileName(nameToMatch, $"Match_{mediaType}_{title}_{firstMatch.MatchType}"); using (var f = File.Create(Path.Combine(path, Path.ChangeExtension(fileName, "json")))) { var w = new Utf8JsonWriter(f, new JsonWriterOptions { Indented = true }); JsonSerializer.Serialize(w, (object)match); f.Close(); } if (match is MatchDVD matchDVD) { nameToMatchDVD[fileName] = matchDVD; graceNoteID2NameDVD[matchDVD.GraceNoteDiscID] = fileName; } if (match is MatchBD matchBD) { nameToMatchBD[fileName] = matchBD; AACSDiscID2NameBD[matchBD.AACSDiscID] = fileName; } match.AssociateMetaData(); return(match); } } } return(null); }
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; } }