From b451120bde68b8d4253ea1c0b740a49378883126 Mon Sep 17 00:00:00 2001 From: Elwador <75888166+Elwador@users.noreply.github.com> Date: Sat, 3 Aug 2024 14:44:04 +0200 Subject: [PATCH] Fix - Sonarr error with missing status https://github.com/Crunchy-DL/Crunchy-Downloader/issues/53 Fix - FFMPEG default dub/sub Fix - Calendar/Search not working if the downloader wasn't used for a while --- CRD/Downloader/Crunchyroll/CRAuth.cs | 94 +++++++++---------- CRD/Downloader/Crunchyroll/CrEpisode.cs | 34 ++----- CRD/Downloader/Crunchyroll/CrSeries.cs | 9 +- .../Crunchyroll/CrunchyrollManager.cs | 42 ++++----- CRD/Utils/Enums/EnumCollection.cs | 3 +- CRD/Utils/Muxing/Merger.cs | 24 ++--- 6 files changed, 96 insertions(+), 110 deletions(-) diff --git a/CRD/Downloader/Crunchyroll/CRAuth.cs b/CRD/Downloader/Crunchyroll/CRAuth.cs index bea3979..014bc92 100644 --- a/CRD/Downloader/Crunchyroll/CRAuth.cs +++ b/CRD/Downloader/Crunchyroll/CRAuth.cs @@ -45,7 +45,7 @@ public class CrAuth{ PreferredContentSubtitleLanguage = "de-DE" }; - CrunchyrollManager.Instance.CmsToken = new CrCmsToken(); + // CrunchyrollManager.Instance.CmsToken = new CrCmsToken(); } @@ -171,7 +171,7 @@ public class CrAuth{ await GetProfile(); } - await GetCmsToken(); + // await GetCmsToken(); } public async Task RefreshToken(bool needsToken){ @@ -210,52 +210,52 @@ public class CrAuth{ Console.Error.WriteLine("Refresh Token Auth Failed"); } - await GetCmsToken(); + // await GetCmsToken(); } - public async Task GetCmsToken(){ - if (crunInstance.Token?.access_token == null){ - Console.Error.WriteLine($"Missing Access Token: {crunInstance.Token?.access_token}"); - return; - } - - var request = HttpClientReq.CreateRequestMessage(Api.BetaCmsToken, HttpMethod.Get, true, true, null); - - var response = await HttpClientReq.Instance.SendHttpRequest(request); - - if (response.IsOk){ - crunInstance.CmsToken = JsonConvert.DeserializeObject(response.ResponseContent, crunInstance.SettingsJsonSerializerSettings); - } else{ - Console.Error.WriteLine("CMS Token Auth Failed"); - } - } - - public async Task GetCmsData(){ - if (crunInstance.CmsToken?.Cms == null){ - Console.Error.WriteLine("Missing CMS Token"); - return; - } - - UriBuilder uriBuilder = new UriBuilder(Api.BetaCms + crunInstance.CmsToken.Cms.Bucket + "/index?"); - - NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query); - - query["preferred_audio_language"] = "ja-JP"; - query["Policy"] = crunInstance.CmsToken.Cms.Policy; - query["Signature"] = crunInstance.CmsToken.Cms.Signature; - query["Key-Pair-Id"] = crunInstance.CmsToken.Cms.KeyPairId; - - uriBuilder.Query = query.ToString(); - - var request = new HttpRequestMessage(HttpMethod.Get, uriBuilder.ToString()); - - var response = await HttpClientReq.Instance.SendHttpRequest(request); - - if (response.IsOk){ - Console.WriteLine(response.ResponseContent); - } else{ - Console.Error.WriteLine("Failed to Get CMS Index"); - } - } + // public async Task GetCmsToken(){ + // if (crunInstance.Token?.access_token == null){ + // Console.Error.WriteLine($"Missing Access Token: {crunInstance.Token?.access_token}"); + // return; + // } + // + // var request = HttpClientReq.CreateRequestMessage(Api.BetaCmsToken, HttpMethod.Get, true, true, null); + // + // var response = await HttpClientReq.Instance.SendHttpRequest(request); + // + // if (response.IsOk){ + // crunInstance.CmsToken = JsonConvert.DeserializeObject(response.ResponseContent, crunInstance.SettingsJsonSerializerSettings); + // } else{ + // Console.Error.WriteLine("CMS Token Auth Failed"); + // } + // } + // + // public async Task GetCmsData(){ + // if (crunInstance.CmsToken?.Cms == null){ + // Console.Error.WriteLine("Missing CMS Token"); + // return; + // } + // + // UriBuilder uriBuilder = new UriBuilder(Api.BetaCms + crunInstance.CmsToken.Cms.Bucket + "/index?"); + // + // NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query); + // + // query["preferred_audio_language"] = "ja-JP"; + // query["Policy"] = crunInstance.CmsToken.Cms.Policy; + // query["Signature"] = crunInstance.CmsToken.Cms.Signature; + // query["Key-Pair-Id"] = crunInstance.CmsToken.Cms.KeyPairId; + // + // uriBuilder.Query = query.ToString(); + // + // var request = new HttpRequestMessage(HttpMethod.Get, uriBuilder.ToString()); + // + // var response = await HttpClientReq.Instance.SendHttpRequest(request); + // + // if (response.IsOk){ + // Console.WriteLine(response.ResponseContent); + // } else{ + // Console.Error.WriteLine("Failed to Get CMS Index"); + // } + // } } \ No newline at end of file diff --git a/CRD/Downloader/Crunchyroll/CrEpisode.cs b/CRD/Downloader/Crunchyroll/CrEpisode.cs index 60ca5da..2f360f3 100644 --- a/CRD/Downloader/Crunchyroll/CrEpisode.cs +++ b/CRD/Downloader/Crunchyroll/CrEpisode.cs @@ -15,10 +15,10 @@ public class CrEpisode(){ private readonly CrunchyrollManager crunInstance = CrunchyrollManager.Instance; public async Task ParseEpisodeById(string id, string crLocale, bool forcedLang = false){ - if (crunInstance.CmsToken?.Cms == null){ - Console.Error.WriteLine("Missing CMS Access Token"); - return null; - } + // if (crunInstance.CmsToken?.Cms == null){ + // Console.Error.WriteLine("Missing CMS Access Token"); + // return null; + // } NameValueCollection query = HttpUtility.ParseQueryString(new UriBuilder().Query); @@ -130,30 +130,7 @@ public class CrEpisode(){ if (!serieshasversions){ Console.WriteLine("Couldn\'t find versions on episode, fell back to old method."); } - - - // crunchySeriesList.Data = sortedEpisodes; - // - // - // var images = (episode.EpisodeAndLanguages.Items[0].Images?.Thumbnail ?? new List>{ new List{ new Image{ Source = "/notFound.png" } } }); - // var seconds = (int)Math.Floor(episode.EpisodeAndLanguages.Items[0].DurationMs / 1000.0); - // - // var newEpisode = new Episode{ - // E = episode.Key.StartsWith("E") ? episode.Key.Substring(1) : episode.Key, - // Lang = episode.EpisodeAndLanguages.Langs.Select(a => a.Code).ToList(), - // Name = episode.EpisodeAndLanguages.Items[0].Title, - // Season = Helpers.ExtractNumberAfterS(episode.EpisodeAndLanguages.Items[0].Identifier) ?? episode.EpisodeAndLanguages.Items[0].SeasonNumber.ToString(), - // SeriesTitle = Regex.Replace(episode.EpisodeAndLanguages.Items[0].SeriesTitle, @"\(\w+ Dub\)", "").TrimEnd(), - // SeasonTitle = Regex.Replace(episode.EpisodeAndLanguages.Items[0].SeasonTitle, @"\(\w+ Dub\)", "").TrimEnd(), - // EpisodeNum = episode.EpisodeAndLanguages.Items[0].EpisodeNumber?.ToString() ?? episode.EpisodeAndLanguages.Items[0].Episode ?? "?", - // Id = episode.EpisodeAndLanguages.Items[0].SeasonId, - // Img = images[images.Count / 2].FirstOrDefault().Source, - // Description = episode.EpisodeAndLanguages.Items[0].Description, - // Time = $"{seconds / 60}:{seconds % 60:D2}" // Ensures two digits for seconds. - // }; - // - // CrunchySeriesList crunchySeriesList = new CrunchySeriesList(); - + return episode; } @@ -243,6 +220,7 @@ public class CrEpisode(){ } public async Task GetNewEpisodes(string? crLocale, int requestAmount,DateTime? firstWeekDay = null , bool forcedLang = false){ + await crunInstance.CrAuth.RefreshToken(true); CrBrowseEpisodeBase? complete = new CrBrowseEpisodeBase(); complete.Data =[]; diff --git a/CRD/Downloader/Crunchyroll/CrSeries.cs b/CRD/Downloader/Crunchyroll/CrSeries.cs index dfdb66c..74f8916 100644 --- a/CRD/Downloader/Crunchyroll/CrSeries.cs +++ b/CRD/Downloader/Crunchyroll/CrSeries.cs @@ -234,14 +234,16 @@ public class CrSeries(){ newKey = $"{(isSpecial ? "SP" : 'E')}{(isSpecial ? (specialIndex + " " + item.Items[0].Id) : epIndex + "")}"; } + + episodes.Remove(key); + int counter = 1; string originalKey = newKey; while (episodes.ContainsKey(newKey)){ newKey = originalKey + "_" + counter; counter++; } - - episodes.Remove(key); + episodes.Add(newKey, item); if (isSpecial){ @@ -389,6 +391,7 @@ public class CrSeries(){ } public async Task ParseSeriesById(string id, string? crLocale, bool forced = false){ + await crunInstance.CrAuth.RefreshToken(true); NameValueCollection query = HttpUtility.ParseQueryString(new UriBuilder().Query); query["preferred_audio_language"] = "ja-JP"; @@ -420,6 +423,7 @@ public class CrSeries(){ } public async Task SeriesById(string id, string? crLocale, bool forced = false){ + await crunInstance.CrAuth.RefreshToken(true); NameValueCollection query = HttpUtility.ParseQueryString(new UriBuilder().Query); query["preferred_audio_language"] = "ja-JP"; @@ -451,6 +455,7 @@ public class CrSeries(){ public async Task Search(string searchString, string? crLocale){ + await crunInstance.CrAuth.RefreshToken(true); NameValueCollection query = HttpUtility.ParseQueryString(new UriBuilder().Query); if (!string.IsNullOrEmpty(crLocale)){ diff --git a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs index 86724a0..560155e 100644 --- a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs +++ b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs @@ -28,7 +28,7 @@ namespace CRD.Downloader.Crunchyroll; public class CrunchyrollManager{ public CrToken? Token; - public CrCmsToken? CmsToken; + // public CrCmsToken? CmsToken; public CrProfile Profile = new(); private readonly Lazy _optionsLazy; @@ -382,16 +382,16 @@ public class CrunchyrollManager{ } private async Task DownloadMediaList(CrunchyEpMeta data, CrDownloadOptions options){ - if (CmsToken?.Cms == null){ - Console.WriteLine("Missing CMS Token"); - MainWindow.Instance.ShowError("Missing CMS Token - are you signed in?"); - return new DownloadResponse{ - Data = new List(), - Error = true, - FileName = "./unknown", - ErrorText = "Login problem" - }; - } + // if (CmsToken?.Cms == null){ + // Console.WriteLine("Missing CMS Token"); + // MainWindow.Instance.ShowError("Missing CMS Token - are you signed in?"); + // return new DownloadResponse{ + // Data = new List(), + // Error = true, + // FileName = "./unknown", + // ErrorText = "Login problem" + // }; + // } if (Profile.Username == "???"){ MainWindow.Instance.ShowError("User Account not recognized - are you signed in?"); @@ -453,16 +453,16 @@ public class CrunchyrollManager{ List files = new List(); - if (data.Data != null && data.Data.All(a => a.Playback == null)){ - Console.WriteLine("No Video Data found - Are you trying to download a premium episode without havíng a premium account?"); - MainWindow.Instance.ShowError("No Video Data found - Are you trying to download a premium episode without havíng a premium account?"); - return new DownloadResponse{ - Data = files, - Error = true, - FileName = "./unknown", - ErrorText = "Video Data not found" - }; - } + // if (data.Data != null && data.Data.All(a => a.Playback == null)){ + // Console.WriteLine("No Video Data found - Are you trying to download a premium episode without havíng a premium account?"); + // MainWindow.Instance.ShowError("No Video Data found - Are you trying to download a premium episode without havíng a premium account?"); + // return new DownloadResponse{ + // Data = files, + // Error = true, + // FileName = "./unknown", + // ErrorText = "Video Data not found" + // }; + // } bool dlFailed = false; diff --git a/CRD/Utils/Enums/EnumCollection.cs b/CRD/Utils/Enums/EnumCollection.cs index 7353475..b062cd2 100644 --- a/CRD/Utils/Enums/EnumCollection.cs +++ b/CRD/Utils/Enums/EnumCollection.cs @@ -207,5 +207,6 @@ public enum SonarrSeriesType{ public enum SonarrStatus{ Continuing, Upcoming, - Ended + Ended, + Deleted }; \ No newline at end of file diff --git a/CRD/Utils/Muxing/Merger.cs b/CRD/Utils/Muxing/Merger.cs index 04da6e2..212a656 100644 --- a/CRD/Utils/Muxing/Merger.cs +++ b/CRD/Utils/Muxing/Merger.cs @@ -56,6 +56,11 @@ public class Merger{ args.Add($"-i \"{aud.Path}\""); metaData.Add($"-map {index}:a"); metaData.Add($"-metadata:s:a:{audioIndex} language={aud.Language.Code}"); + if (options.Defaults.Audio.Code == aud.Language.Code){ + metaData.Add($"-disposition:a:{audioIndex} default"); + } else{ + metaData.Add($"-disposition:a:{audioIndex} 0"); + } index++; audioIndex++; } @@ -75,20 +80,17 @@ public class Merger{ } args.Add($"-i \"{sub.value.File}\""); - } - - if (options.Output.EndsWith(".mkv", StringComparison.OrdinalIgnoreCase)){ - if (options.Fonts != null){ - int fontIndex = 0; - foreach (var font in options.Fonts){ - args.Add($"-attach {font.Path} -metadata:s:t:{fontIndex} mimetype={font.Mime}"); - fontIndex++; - } + metaData.Add($"-map {index}:s"); + if (options.Defaults.Sub.Code == sub.value.Language.Code && CrunchyrollManager.Instance.CrunOptions.DefaultSubSigns == sub.value.Signs && sub.value.ClosedCaption == false){ + args.Add($"-disposition:s:{sub.i} default"); + } else{ + args.Add($"-disposition:s:{sub.i} 0"); } + index++; } - + args.AddRange(metaData); - args.AddRange(options.Subtitles.Select((sub, subIndex) => $"-map {subIndex + index}")); + // args.AddRange(options.Subtitles.Select((sub, subIndex) => $"-map {subIndex + index}")); args.Add("-c:v copy"); args.Add("-c:a copy"); args.Add(options.Output.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase) ? "-c:s mov_text" : "-c:s ass");