Fix - Chapters failed for series with only one dub https://github.com/Crunchy-DL/Crunchy-Downloader/issues/83

This commit is contained in:
Elwador 2024-08-16 19:46:57 +02:00
parent 335a2a8bc5
commit c7a2ef5af7
2 changed files with 34 additions and 13 deletions

View File

@ -166,7 +166,7 @@ public class CrunchyrollManager{
if (File.Exists(CfgManager.PathCrHistory)){ if (File.Exists(CfgManager.PathCrHistory)){
var decompressedJson = CfgManager.DecompressJsonFile(CfgManager.PathCrHistory); var decompressedJson = CfgManager.DecompressJsonFile(CfgManager.PathCrHistory);
if (!string.IsNullOrEmpty(decompressedJson)){ if (!string.IsNullOrEmpty(decompressedJson)){
HistoryList = Helpers.Deserialize<ObservableCollection<HistorySeries>>(decompressedJson,CrunchyrollManager.Instance.SettingsJsonSerializerSettings) ?? new ObservableCollection<HistorySeries>(); HistoryList = Helpers.Deserialize<ObservableCollection<HistorySeries>>(decompressedJson, CrunchyrollManager.Instance.SettingsJsonSerializerSettings) ?? new ObservableCollection<HistorySeries>();
foreach (var historySeries in HistoryList){ foreach (var historySeries in HistoryList){
historySeries.Init(); historySeries.Init();
@ -565,7 +565,7 @@ public class CrunchyrollManager{
List<string> compiledChapters = new List<string>(); List<string> compiledChapters = new List<string>();
if (options.Chapters){ if (options.Chapters){
await ParseChapters(primaryVersion.Guid, compiledChapters); await ParseChapters(primaryVersion.Guid ?? mediaGuid, compiledChapters);
} }
#endregion #endregion
@ -1664,7 +1664,7 @@ public class CrunchyrollManager{
#endregion #endregion
private async Task ParseChapters(string currentMediaId, List<string> compiledChapters){ private async Task<bool> ParseChapters(string currentMediaId, List<string> compiledChapters){
var showRequest = HttpClientReq.CreateRequestMessage($"https://static.crunchyroll.com/skip-events/production/{currentMediaId}.json", HttpMethod.Get, true, true, null); 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); var showRequestResponse = await HttpClientReq.Instance.SendHttpRequest(showRequest);
@ -1698,6 +1698,7 @@ public class CrunchyrollManager{
} }
} catch (Exception ex){ } catch (Exception ex){
Console.Error.WriteLine($"Error parsing JSON response: {ex.Message}"); Console.Error.WriteLine($"Error parsing JSON response: {ex.Message}");
return false;
} }
if (chapterData.Chapters.Count > 0){ if (chapterData.Chapters.Count > 0){
@ -1749,6 +1750,8 @@ public class CrunchyrollManager{
compiledChapters.Add($"CHAPTER{chapterNumber}NAME={formattedChapterType} End"); compiledChapters.Add($"CHAPTER{chapterNumber}NAME={formattedChapterType} End");
} }
} }
return true;
} }
} else{ } else{
Console.WriteLine("Chapter request failed, attempting old API "); Console.WriteLine("Chapter request failed, attempting old API ");
@ -1758,7 +1761,7 @@ public class CrunchyrollManager{
showRequestResponse = await HttpClientReq.Instance.SendHttpRequest(showRequest); showRequestResponse = await HttpClientReq.Instance.SendHttpRequest(showRequest);
if (showRequestResponse.IsOk){ if (showRequestResponse.IsOk){
CrunchyOldChapter chapterData = Helpers.Deserialize<CrunchyOldChapter>(showRequestResponse.ResponseContent,SettingsJsonSerializerSettings); CrunchyOldChapter chapterData = Helpers.Deserialize<CrunchyOldChapter>(showRequestResponse.ResponseContent, SettingsJsonSerializerSettings);
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
@ -1786,9 +1789,13 @@ public class CrunchyrollManager{
chapterNumber = (compiledChapters.Count / 2) + 1; chapterNumber = (compiledChapters.Count / 2) + 1;
compiledChapters.Add($"CHAPTER{chapterNumber}={endFormatted}"); compiledChapters.Add($"CHAPTER{chapterNumber}={endFormatted}");
compiledChapters.Add($"CHAPTER{chapterNumber}NAME=Episode"); compiledChapters.Add($"CHAPTER{chapterNumber}NAME=Episode");
} else{ return true;
Console.WriteLine("Old Chapter API request failed");
} }
Console.WriteLine("Old Chapter API request failed");
return false;
} }
return true;
} }
} }

View File

@ -5,12 +5,26 @@ namespace CRD.Utils.JsonConv;
public class UtcToLocalTimeConverter : JsonConverter<DateTime>{ public class UtcToLocalTimeConverter : JsonConverter<DateTime>{
public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer){ public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer){
return reader.Value switch{ try{
null => DateTime.MinValue, return reader.Value switch{
DateTime dateTime when dateTime.Kind == DateTimeKind.Utc => dateTime.ToLocalTime(), null => DateTime.MinValue,
DateTime dateTime => dateTime, DateTime dateTime when dateTime.Kind == DateTimeKind.Utc => dateTime.ToLocalTime(),
_ => throw new JsonSerializationException($"Unexpected token parsing date. Expected DateTime, got {reader.Value.GetType()}.") 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){ public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer){