From 89c7b1021f8be3ecdb5ef57dc20d014ed56b6577 Mon Sep 17 00:00:00 2001 From: Elwador <75888166+Elwador@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:29:13 +0200 Subject: [PATCH] Add - Added "Remove Finished" to download queue Fix - Fixed a crash when muxing title wasn't set --- CRD/Downloader/Crunchyroll.cs | 15 +++-- CRD/Utils/Structs/CrDownloadOptions.cs | 9 +++ CRD/ViewModels/DownloadsPageViewModel.cs | 79 ++++++++++++------------ CRD/Views/DownloadsPageView.axaml | 3 +- 4 files changed, 61 insertions(+), 45 deletions(-) diff --git a/CRD/Downloader/Crunchyroll.cs b/CRD/Downloader/Crunchyroll.cs index cfb150b..19944d9 100644 --- a/CRD/Downloader/Crunchyroll.cs +++ b/CRD/Downloader/Crunchyroll.cs @@ -44,7 +44,6 @@ public class Crunchyroll{ public RefreshableObservableCollection Queue = new RefreshableObservableCollection(); public ObservableCollection DownloadItemModels = new ObservableCollection(); public int ActiveDownloads; - public bool AutoDownload = false; #endregion @@ -103,6 +102,10 @@ public class Crunchyroll{ #endregion + public Crunchyroll(){ + CrunOptions = new CrDownloadOptions(); + } + public async Task Init(){ _widevine = Widevine.Instance; @@ -129,8 +132,8 @@ public class Crunchyroll{ Console.WriteLine($"Can Decrypt: {_widevine.canDecrypt}"); - CrunOptions = new CrDownloadOptions(); - + CrunOptions.AutoDownload = false; + CrunOptions.RemoveFinishedDownload = false; CrunOptions.Chapters = true; CrunOptions.Hslang = "none"; CrunOptions.Force = "Y"; @@ -441,6 +444,10 @@ public class Crunchyroll{ Doing = "Done" }; + if (CrunOptions.RemoveFinishedDownload){ + Queue.Remove(data); + } + Queue.Refresh(); } else{ Console.WriteLine("Skipping mux"); @@ -1384,7 +1391,7 @@ public class Crunchyroll{ Error = dlFailed, FileName = fileName.Length > 0 ? (Path.IsPathRooted(fileName) ? fileName : Path.Combine(fileDir, fileName)) : "./unknown", ErrorText = "", - VideoTitle = FileNameManager.ParseFileName(options.VideoTitle, variables, options.Numbers, options.Override).Last() + VideoTitle = FileNameManager.ParseFileName(options.VideoTitle ?? "", variables, options.Numbers, options.Override).Last() }; } diff --git a/CRD/Utils/Structs/CrDownloadOptions.cs b/CRD/Utils/Structs/CrDownloadOptions.cs index 9e1be50..6fd4e29 100644 --- a/CRD/Utils/Structs/CrDownloadOptions.cs +++ b/CRD/Utils/Structs/CrDownloadOptions.cs @@ -5,6 +5,15 @@ using YamlDotNet.Serialization; namespace CRD.Utils.Structs; public class CrDownloadOptions{ + + [YamlIgnore] + public bool AutoDownload{ get; set; } + + + [YamlIgnore] + public bool RemoveFinishedDownload{ get; set; } + + [YamlMember(Alias = "hard_sub_lang", ApplyNamingConventions = false)] public string Hslang{ get; set; } diff --git a/CRD/ViewModels/DownloadsPageViewModel.cs b/CRD/ViewModels/DownloadsPageViewModel.cs index 87d9803..71d058f 100644 --- a/CRD/ViewModels/DownloadsPageViewModel.cs +++ b/CRD/ViewModels/DownloadsPageViewModel.cs @@ -18,18 +18,19 @@ using CRD.Utils.Structs; namespace CRD.ViewModels; public partial class DownloadsPageViewModel : ViewModelBase{ - - public ObservableCollection Items{ get; } - [ObservableProperty] public bool _autoDownload; - - private SemaphoreSlim semaphore = new SemaphoreSlim(1, 1); + [ObservableProperty] + public bool _autoDownload; + [ObservableProperty] + public bool _removeFinished; + public DownloadsPageViewModel(){ UpdateListItems(); Items = Crunchyroll.Instance.DownloadItemModels; - AutoDownload = Crunchyroll.Instance.AutoDownload; + AutoDownload = Crunchyroll.Instance.CrunOptions.AutoDownload; + RemoveFinished = Crunchyroll.Instance.CrunOptions.RemoveFinishedDownload; Crunchyroll.Instance.Queue.CollectionChanged += UpdateItemListOnRemove; // Items.Add(new DownloadItemModel{Title = "Test - S1E1"}); } @@ -51,32 +52,36 @@ public partial class DownloadsPageViewModel : ViewModelBase{ } - public void UpdateListItems(){ - var list = Crunchyroll.Instance.Queue; + public void UpdateListItems(){ + var list = Crunchyroll.Instance.Queue; - foreach (CrunchyEpMeta crunchyEpMeta in list){ - var downloadItem = Crunchyroll.Instance.DownloadItemModels.FirstOrDefault(e => e.epMeta.Equals(crunchyEpMeta)); - if (downloadItem != null){ - downloadItem.Refresh(); - } else{ - downloadItem = new DownloadItemModel(crunchyEpMeta); - downloadItem.LoadImage(); - Crunchyroll.Instance.DownloadItemModels.Add(downloadItem); - } - - if (downloadItem is{ isDownloading: false, Error: false } && Crunchyroll.Instance.AutoDownload && Crunchyroll.Instance.ActiveDownloads < Crunchyroll.Instance.CrunOptions.SimultaneousDownloads){ - downloadItem.StartDownload(); - } + foreach (CrunchyEpMeta crunchyEpMeta in list){ + var downloadItem = Crunchyroll.Instance.DownloadItemModels.FirstOrDefault(e => e.epMeta.Equals(crunchyEpMeta)); + if (downloadItem != null){ + downloadItem.Refresh(); + } else{ + downloadItem = new DownloadItemModel(crunchyEpMeta); + downloadItem.LoadImage(); + Crunchyroll.Instance.DownloadItemModels.Add(downloadItem); } + + if (downloadItem is{ isDownloading: false, Error: false } && Crunchyroll.Instance.CrunOptions.AutoDownload && Crunchyroll.Instance.ActiveDownloads < Crunchyroll.Instance.CrunOptions.SimultaneousDownloads){ + downloadItem.StartDownload(); + } + } } partial void OnAutoDownloadChanged(bool value){ - Crunchyroll.Instance.AutoDownload = value; + Crunchyroll.Instance.CrunOptions.AutoDownload = value; if (value){ UpdateListItems(); } } + partial void OnRemoveFinishedChanged(bool value){ + Crunchyroll.Instance.CrunOptions.RemoveFinishedDownload = value; + } + public void Cleanup(){ Crunchyroll.Instance.Queue.CollectionChanged -= UpdateItemListOnRemove; } @@ -99,7 +104,7 @@ public partial class DownloadItemModel : INotifyPropertyChanged{ public CrunchyEpMeta epMeta{ get; set; } - + public bool Error{ get; set; } public DownloadItemModel(CrunchyEpMeta epMetaF){ @@ -107,20 +112,20 @@ public partial class DownloadItemModel : INotifyPropertyChanged{ ImageUrl = epMeta.Image; Title = epMeta.SeriesTitle + " - S" + epMeta.Season + "E" + (epMeta.EpisodeNumber != string.Empty ? epMeta.EpisodeNumber : epMeta.AbsolutEpisodeNumberE) + " - " + epMeta.EpisodeTitle; - isDownloading = epMeta.DownloadProgress.IsDownloading || Done; - + isDownloading = epMeta.DownloadProgress.IsDownloading || Done; + Done = epMeta.DownloadProgress.Done; Percent = epMeta.DownloadProgress.Percent; Time = "Estimated Time: " + TimeSpan.FromSeconds(epMeta.DownloadProgress.Time / 1000).ToString(@"hh\:mm\:ss"); DownloadSpeed = $"{epMeta.DownloadProgress.DownloadSpeed / 1000000.0:F2}Mb/s"; Paused = epMeta.Paused || !isDownloading && !epMeta.Paused; - DoingWhat = epMeta.Paused ? "Paused" : Done ? "Done" : epMeta.DownloadProgress.Doing != string.Empty ? epMeta.DownloadProgress.Doing : "Waiting"; + DoingWhat = epMeta.Paused ? "Paused" : Done ? "Done" : epMeta.DownloadProgress.Doing != string.Empty ? epMeta.DownloadProgress.Doing : "Waiting"; if (epMeta.Data != null) InfoText = GetDubString() + " - " + GetSubtitleString(); Error = epMeta.DownloadProgress.Error; } - + private string GetDubString(){ var hardSubs = Crunchyroll.Instance.CrunOptions.Hslang != "none" ? "Hardsub: " + Crunchyroll.Instance.CrunOptions.Hslang : ""; if (hardSubs != string.Empty){ @@ -153,7 +158,7 @@ public partial class DownloadItemModel : INotifyPropertyChanged{ return softSubs; } - } + } foreach (var crunOptionsDlSub in Crunchyroll.Instance.CrunOptions.DlSubs){ if (epMeta.AvailableSubs != null && epMeta.AvailableSubs.Contains(crunOptionsDlSub)){ @@ -165,19 +170,19 @@ public partial class DownloadItemModel : INotifyPropertyChanged{ } public void Refresh(){ - isDownloading = epMeta.DownloadProgress.IsDownloading || Done; + isDownloading = epMeta.DownloadProgress.IsDownloading || Done; Done = epMeta.DownloadProgress.Done; Percent = epMeta.DownloadProgress.Percent; Time = "Estimated Time: " + TimeSpan.FromSeconds(epMeta.DownloadProgress.Time / 1000).ToString(@"hh\:mm\:ss"); DownloadSpeed = $"{epMeta.DownloadProgress.DownloadSpeed / 1000000.0:F2}Mb/s"; Paused = epMeta.Paused || !isDownloading && !epMeta.Paused; - DoingWhat = epMeta.Paused ? "Paused" : Done ? "Done" : epMeta.DownloadProgress.Doing != string.Empty ? epMeta.DownloadProgress.Doing : "Waiting"; + DoingWhat = epMeta.Paused ? "Paused" : Done ? "Done" : epMeta.DownloadProgress.Doing != string.Empty ? epMeta.DownloadProgress.Doing : "Waiting"; if (epMeta.Data != null) InfoText = GetDubString() + " - " + GetSubtitleString(); - + Error = epMeta.DownloadProgress.Error; - + if (PropertyChanged != null){ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(isDownloading))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Percent))); @@ -188,22 +193,18 @@ public partial class DownloadItemModel : INotifyPropertyChanged{ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InfoText))); } } - - public event PropertyChangedEventHandler? PropertyChanged; [RelayCommand] public void ToggleIsDownloading(){ - if (isDownloading){ //StopDownload(); epMeta.Paused = !epMeta.Paused; Paused = epMeta.Paused || !isDownloading && !epMeta.Paused; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Paused))); - } else{ if (epMeta.Paused){ epMeta.Paused = false; @@ -213,12 +214,11 @@ public partial class DownloadItemModel : INotifyPropertyChanged{ StartDownload(); } } - - + + if (PropertyChanged != null){ PropertyChanged.Invoke(this, new PropertyChangedEventArgs("isDownloading")); } - } public async void StartDownload(){ @@ -229,7 +229,6 @@ public partial class DownloadItemModel : INotifyPropertyChanged{ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Paused))); await Crunchyroll.Instance.DownloadEpisode(epMeta, Crunchyroll.Instance.CrunOptions); } - } [RelayCommand] diff --git a/CRD/Views/DownloadsPageView.axaml b/CRD/Views/DownloadsPageView.axaml index 9b699c7..f9d47b4 100644 --- a/CRD/Views/DownloadsPageView.axaml +++ b/CRD/Views/DownloadsPageView.axaml @@ -21,8 +21,9 @@ - + +