From c7a2ef5af71077c0ff222ba8dabc97a9cc2ecdbb Mon Sep 17 00:00:00 2001 From: Elwador <75888166+Elwador@users.noreply.github.com> Date: Fri, 16 Aug 2024 19:46:57 +0200 Subject: [PATCH] Fix - Chapters failed for series with only one dub https://github.com/Crunchy-DL/Crunchy-Downloader/issues/83 --- .../Crunchyroll/CrunchyrollManager.cs | 21 ++++++++++----- CRD/Utils/JsonConv/UtcToLocalTimeConverter.cs | 26 ++++++++++++++----- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs index 589f915..01854e2 100644 --- a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs +++ b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs @@ -166,7 +166,7 @@ public class CrunchyrollManager{ if (File.Exists(CfgManager.PathCrHistory)){ var decompressedJson = CfgManager.DecompressJsonFile(CfgManager.PathCrHistory); if (!string.IsNullOrEmpty(decompressedJson)){ - HistoryList = Helpers.Deserialize>(decompressedJson,CrunchyrollManager.Instance.SettingsJsonSerializerSettings) ?? new ObservableCollection(); + HistoryList = Helpers.Deserialize>(decompressedJson, CrunchyrollManager.Instance.SettingsJsonSerializerSettings) ?? new ObservableCollection(); foreach (var historySeries in HistoryList){ historySeries.Init(); @@ -565,7 +565,7 @@ public class CrunchyrollManager{ List compiledChapters = new List(); if (options.Chapters){ - await ParseChapters(primaryVersion.Guid, compiledChapters); + await ParseChapters(primaryVersion.Guid ?? mediaGuid, compiledChapters); } #endregion @@ -1423,7 +1423,7 @@ public class CrunchyrollManager{ string dialogue = string.Join("\\N", lines.Skip(2)); dialogue = Helpers.ConvertVTTStylesToASS(dialogue); - + // Append dialogue to ASS assBuilder.AppendLine($"Dialogue: 0,{startTime},{endTime},Default,,0000,0000,0000,,{dialogue}"); } @@ -1664,7 +1664,7 @@ public class CrunchyrollManager{ #endregion - private async Task ParseChapters(string currentMediaId, List compiledChapters){ + private async Task ParseChapters(string currentMediaId, List compiledChapters){ var showRequest = HttpClientReq.CreateRequestMessage($"https://static.crunchyroll.com/skip-events/production/{currentMediaId}.json", HttpMethod.Get, true, true, null); var showRequestResponse = await HttpClientReq.Instance.SendHttpRequest(showRequest); @@ -1698,6 +1698,7 @@ public class CrunchyrollManager{ } } catch (Exception ex){ Console.Error.WriteLine($"Error parsing JSON response: {ex.Message}"); + return false; } if (chapterData.Chapters.Count > 0){ @@ -1749,6 +1750,8 @@ public class CrunchyrollManager{ compiledChapters.Add($"CHAPTER{chapterNumber}NAME={formattedChapterType} End"); } } + + return true; } } else{ Console.WriteLine("Chapter request failed, attempting old API "); @@ -1758,7 +1761,7 @@ public class CrunchyrollManager{ showRequestResponse = await HttpClientReq.Instance.SendHttpRequest(showRequest); if (showRequestResponse.IsOk){ - CrunchyOldChapter chapterData = Helpers.Deserialize(showRequestResponse.ResponseContent,SettingsJsonSerializerSettings); + CrunchyOldChapter chapterData = Helpers.Deserialize(showRequestResponse.ResponseContent, SettingsJsonSerializerSettings); DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); @@ -1786,9 +1789,13 @@ public class CrunchyrollManager{ chapterNumber = (compiledChapters.Count / 2) + 1; compiledChapters.Add($"CHAPTER{chapterNumber}={endFormatted}"); compiledChapters.Add($"CHAPTER{chapterNumber}NAME=Episode"); - } else{ - Console.WriteLine("Old Chapter API request failed"); + return true; } + + Console.WriteLine("Old Chapter API request failed"); + return false; } + + return true; } } \ No newline at end of file diff --git a/CRD/Utils/JsonConv/UtcToLocalTimeConverter.cs b/CRD/Utils/JsonConv/UtcToLocalTimeConverter.cs index cdf9174..7387bd8 100644 --- a/CRD/Utils/JsonConv/UtcToLocalTimeConverter.cs +++ b/CRD/Utils/JsonConv/UtcToLocalTimeConverter.cs @@ -5,12 +5,26 @@ namespace CRD.Utils.JsonConv; public class UtcToLocalTimeConverter : JsonConverter{ public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer){ - return reader.Value switch{ - null => DateTime.MinValue, - DateTime dateTime when dateTime.Kind == DateTimeKind.Utc => dateTime.ToLocalTime(), - DateTime dateTime => dateTime, - _ => throw new JsonSerializationException($"Unexpected token parsing date. Expected DateTime, got {reader.Value.GetType()}.") - }; + try{ + return reader.Value switch{ + null => DateTime.MinValue, + DateTime dateTime when dateTime.Kind == DateTimeKind.Utc => dateTime.ToLocalTime(), + DateTime dateTime => dateTime, + string dateString => TryParseDateTime(dateString), + _ => throw new JsonSerializationException($"Unexpected token parsing date. Expected DateTime or string, got {reader.Value?.GetType()}.") + }; + } catch (Exception ex){ + Console.Error.WriteLine("Error deserializing DateTime", ex); + } + return DateTime.UnixEpoch; + } + + private DateTime TryParseDateTime(string dateString){ + if (DateTime.TryParse(dateString, out DateTime parsedDate)){ + return parsedDate.Kind == DateTimeKind.Utc ? parsedDate.ToLocalTime() : parsedDate; + } + + throw new JsonSerializationException($"Invalid date string: {dateString}"); } public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer){