diff --git a/AutoDownloader/AutoDownloader/DownloadManager/Managers.cs b/AutoDownloader/AutoDownloader/DownloadManager/Managers.cs index c5187ff..8b1f40a 100644 --- a/AutoDownloader/AutoDownloader/DownloadManager/Managers.cs +++ b/AutoDownloader/AutoDownloader/DownloadManager/Managers.cs @@ -499,129 +499,177 @@ private string[] VerifyMetaFile(string filePath, Type type) //TODO:: change this } public Task active; + public CancellationTokenSource enqueueCancelToken = null; public void CheckQueue() { - if (active == null || active.IsCompleted) active = Enqueue(); + if (active == null || active.IsCompleted) + { + if (enqueueCancelToken == null) enqueueCancelToken = new CancellationTokenSource(); + else + { + enqueueCancelToken.Dispose(); + enqueueCancelToken = new CancellationTokenSource(); + } + active = Enqueue(enqueueCancelToken.Token); + } } public Link? current = null; - private async Task Enqueue() + private void AbortQueue() + { + form.SetQueue(null); + form.RestoreEpisodeToQueue(current.Value, true); + queue.AddLast(current.Value); + } + private async Task Enqueue(CancellationToken ct) { - if (downloads.files.Count == 0) + try { - if (queue.Count != 0) - { - Link l = queue.First(); - current = l; - - queue.RemoveFirst(); + ct.ThrowIfCancellationRequested(); + form.DownloadControl(true); - form.Downloads.Items.Clear(); - foreach (Link li in queue) + if (downloads.files.Count == 0) + { + if (queue.Count != 0) { - form.Downloads.Items.Add(li); - } + Link l = queue.First(); + current = l; + form.SetQueue(current); - form.Log("[Enqueue] Loading URL..."); - LoadUrlAsyncResponse resp = await downloader.LoadUrlAsync(l.episodeUrl); - if (!resp.Success) - { - form.Log("[Enqueue] Browser timed out, aborting..."); - return; - } + queue.RemoveFirst(); - form.Log("[Enqueue] Attempting to load Mp4Upload video...\n[Enqueue] Attempt 1..."); - string script = string.Empty; - switch (l.type) - { - case Type.subbed: - script = Scripts.LoadSubbedMp4UploadVideo; - break; - case Type.dubbed: - script = Scripts.LoadDubbedMp4UploadVideo; - break; - default: - Remove(l); + form.Downloads.Items.Clear(); + foreach (Link li in queue) + { + form.Downloads.Items.Add(li); + } + + form.Log("[Enqueue] Loading URL... (If your program gets stuck here, close and reopen)"); + LoadUrlAsyncResponse resp = await downloader.LoadUrlAsync(l.episodeUrl); + if (!resp.Success) + { + form.Log("[Enqueue] Browser timed out, aborting..."); + AbortQueue(); return; - } - downloader.ExecuteScriptAsync(script); + } + ct.ThrowIfCancellationRequested(); - string html = await downloader.GetSourceAsync(); + form.Log("[Enqueue] Attempting to load Mp4Upload video...\n[Enqueue] Attempt 1..."); + string script = string.Empty; + switch (l.type) + { + case Type.subbed: + script = Scripts.LoadSubbedMp4UploadVideo; + break; + case Type.dubbed: + script = Scripts.LoadDubbedMp4UploadVideo; + break; + default: + Remove(l); + AbortQueue(); + return; + } + downloader.ExecuteScriptAsync(script); - string linkPrefix = "https://www.mp4upload.com/embed-"; - int mp4Upload = html.IndexOf(linkPrefix); - for (int i = 0; i < 9 && mp4Upload < 0; i++) - { - form.Log("[Enqueue] Attempt " + (i + 2) + "..."); - html = await downloader.GetSourceAsync(); + string html = await downloader.GetSourceAsync(); - mp4Upload = html.IndexOf(linkPrefix); - await Task.Delay(1000); - } - if (mp4Upload < 0) - { - form.Log("[Enqueue] Failed to load Mp4Upload video."); - Remove(l); - return; - } - mp4Upload += linkPrefix.Length; + string linkPrefix = "https://www.mp4upload.com/embed-"; + int mp4Upload = html.IndexOf(linkPrefix); + for (int i = 0; i < 9 && mp4Upload < 0; i++) + { + ct.ThrowIfCancellationRequested(); - StringBuilder downloadLink = new StringBuilder("https://www.mp4upload.com/"); - for (char c = html[mp4Upload]; c != '.'; c = html[++mp4Upload]) - { - downloadLink.Append(c); - } + form.Log("[Enqueue] Attempt " + (i + 2) + "..."); + html = await downloader.GetSourceAsync(); - form.Log("[Enqueue] Found Mp4 video, " + downloadLink.ToString() + "\n[Enqueue] Attempting to load Mp4Upload embed...\n[Enqueue] Attempt 1..."); - resp = await downloader.LoadUrlAsync(downloadLink.ToString()); - if (!resp.Success) - { - form.Log("[Enqueue] Browser timed out, aborting..."); - return; - } + mp4Upload = html.IndexOf(linkPrefix); + await Task.Delay(1000); + } + if (mp4Upload < 0) + { + form.Log("[Enqueue] Failed to load Mp4Upload video."); + Remove(l); + AbortQueue(); + return; + } + mp4Upload += linkPrefix.Length; - downloader.ExecuteScriptAsync(Scripts.RedirectMp4UploadLink); - html = await downloader.GetSourceAsync(); + StringBuilder downloadLink = new StringBuilder("https://www.mp4upload.com/"); + for (char c = html[mp4Upload]; c != '.'; c = html[++mp4Upload]) + { + downloadLink.Append(c); + } + ct.ThrowIfCancellationRequested(); + + form.Log("[Enqueue] Found Mp4 video, " + downloadLink.ToString() + "\n[Enqueue] Attempting to load Mp4Upload embed...\n[Enqueue] Attempt 1..."); + resp = await downloader.LoadUrlAsync(downloadLink.ToString()); + if (!resp.Success) + { + form.Log("[Enqueue] Browser timed out, aborting..."); + AbortQueue(); + return; + } + ct.ThrowIfCancellationRequested(); - string loadedTest = "Embed code"; - mp4Upload = html.IndexOf(loadedTest); - for (int i = 0; i < 9 && mp4Upload < 0; i++) - { - form.Log("[Enqueue] Attempt " + (i + 2) + "..."); downloader.ExecuteScriptAsync(Scripts.RedirectMp4UploadLink); html = await downloader.GetSourceAsync(); + string loadedTest = "Embed code"; mp4Upload = html.IndexOf(loadedTest); - await Task.Delay(1000); - } - if (mp4Upload < 0) - { - form.Log("[Enqueue] Failed to load Mp4Upload embed."); - Remove(l); - return; - } + for (int i = 0; i < 9 && mp4Upload < 0; i++) + { + ct.ThrowIfCancellationRequested(); - form.Log("[Enqueue] Attempting to start download..."); - for (int i = 0; i < 10 && downloads.files.Count == 0; i++) - { - form.Log("[Enqueue] Attempt " + (i + 1) + "..."); - downloader.ExecuteScriptAsync(Scripts.StartMp4UploadDownload); - await Task.Delay(1000); - } - if (downloads.files.Count != 0) - { - string key = downloads.files.Keys.First(); - DownloadProgress progress = downloads.files[key]; - progress.acknowledged = true; - downloads.files[key] = progress; - form.Log("[Enqueue] Download started successfully!"); - } - else - { - form.Log("[Enqueue] Failed to start download."); + form.Log("[Enqueue] Attempt " + (i + 2) + "..."); + downloader.ExecuteScriptAsync(Scripts.RedirectMp4UploadLink); + html = await downloader.GetSourceAsync(); + + mp4Upload = html.IndexOf(loadedTest); + await Task.Delay(1000); + } + if (mp4Upload < 0) + { + form.Log("[Enqueue] Failed to load Mp4Upload embed."); + Remove(l); + AbortQueue(); + return; + } + ct.ThrowIfCancellationRequested(); + + form.Log("[Enqueue] Attempting to start download..."); + for (int i = 0; i < 10 && downloads.files.Count == 0; i++) + { + ct.ThrowIfCancellationRequested(); + + form.Log("[Enqueue] Attempt " + (i + 1) + "..."); + downloader.ExecuteScriptAsync(Scripts.StartMp4UploadDownload); + await Task.Delay(1000); + } + if (downloads.files.Count != 0) + { + string key = downloads.files.Keys.First(); + DownloadProgress progress = downloads.files[key]; + progress.acknowledged = true; + downloads.files[key] = progress; + form.Log("[Enqueue] Download started successfully!"); + } + else + { + form.Log("[Enqueue] Failed to start download."); + } } } } + catch (OperationCanceledException) + { + if (!form.enqueueing) + { + form.RestoreEpisodeToQueue(current.Value); + queue.AddFirst(current.Value); + } + } + + form.SetQueue(null); } private void Remove(Link l) @@ -660,6 +708,13 @@ public void Cancel() Remove(current.Value); } + else + { + form.Log("[Enqueue] Cancelling..."); + enqueueCancelToken.Cancel(); + } + + form.DownloadControl(false); } public void AddEpisodes(Link[] links, Type type) @@ -704,7 +759,7 @@ public async Task GetEpisodes(CancellationToken ct, string url, Type typ // Were we already canceled? ct.ThrowIfCancellationRequested(); - form.Log("[Get] Loading site..."); + form.Log("[Get] Loading site... (If your program gets stuck here, close and reopen)"); LoadUrlAsyncResponse resp = await fetcher.LoadUrlAsync(url); if (!resp.Success) { @@ -875,7 +930,7 @@ public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string form.SetProgress(progress); - return true; + return !manager.enqueueCancelToken.IsCancellationRequested; } else { @@ -932,12 +987,16 @@ public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, if (progress.cancelled) { form.Log("[Web] Cancelling download..."); - if (progress.savePath == manager.savePath) form.RestoreEpisode(manager.current.Value); + if (!form.enqueueing) + { + form.RestoreEpisodeToQueue(manager.current.Value); + manager.queue.AddFirst(manager.current.Value); + } + else if (progress.savePath == manager.savePath) form.RestoreEpisode(manager.current.Value); files.Remove(id); manager.current = null; form.ClearProgress(); callback.Cancel(); - form.DownloadControl(false); return; } @@ -960,8 +1019,6 @@ public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, FileInfo fileInfo = new FileInfo(progress.filePath); fileInfo.MoveTo(Path.Combine(fileInfo.Directory.FullName, l.ToString() + ".mp4")); - form.DownloadControl(false); - progress.completed = true; progress.speed = 0; progress.percentage = 0; @@ -972,7 +1029,6 @@ public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, else files[id] = progress; form.SetProgress(progress); - form.DownloadControl(true); } } } diff --git a/AutoDownloader/AutoDownloader/Form.Designer.cs b/AutoDownloader/AutoDownloader/Form.Designer.cs index c8b5ff7..af33b37 100644 --- a/AutoDownloader/AutoDownloader/Form.Designer.cs +++ b/AutoDownloader/AutoDownloader/Form.Designer.cs @@ -51,6 +51,8 @@ private void InitializeComponent() this.SubbedButton = new System.Windows.Forms.Button(); this.currentAnimeLabel = new System.Windows.Forms.TextBox(); this.CurrentSelection = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.CurrentEnqueue = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // browser @@ -254,9 +256,28 @@ private void InitializeComponent() this.CurrentSelection.Size = new System.Drawing.Size(228, 20); this.CurrentSelection.TabIndex = 23; // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(1107, 59); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(67, 13); + this.label2.TabIndex = 24; + this.label2.Text = "Enqueueing:"; + // + // Current + // + this.CurrentEnqueue.Location = new System.Drawing.Point(1180, 54); + this.CurrentEnqueue.Name = "Current"; + this.CurrentEnqueue.ReadOnly = true; + this.CurrentEnqueue.Size = new System.Drawing.Size(290, 20); + this.CurrentEnqueue.TabIndex = 25; + // // Form // this.ClientSize = new System.Drawing.Size(1482, 681); + this.Controls.Add(this.CurrentEnqueue); + this.Controls.Add(this.label2); this.Controls.Add(this.CurrentSelection); this.Controls.Add(this.currentAnimeLabel); this.Controls.Add(this.SubbedButton); @@ -311,6 +332,8 @@ private void InitializeComponent() public System.Windows.Forms.Button SubbedButton; private System.Windows.Forms.TextBox currentAnimeLabel; private System.Windows.Forms.TextBox CurrentSelection; + private System.Windows.Forms.Label label2; + public System.Windows.Forms.TextBox CurrentEnqueue; } } diff --git a/AutoDownloader/AutoDownloader/Form.cs b/AutoDownloader/AutoDownloader/Form.cs index 246c31e..07a5683 100644 --- a/AutoDownloader/AutoDownloader/Form.cs +++ b/AutoDownloader/AutoDownloader/Form.cs @@ -34,7 +34,7 @@ namespace AutoDownloader public partial class Form : System.Windows.Forms.Form { AutoDownloader_9Animeid manager; - private Version version = new Version("1.2.7"); + private Version version = new Version("1.2.8"); public class ScrollingText { @@ -74,8 +74,10 @@ public ScrollingText(TextBox container) ScrollingText currentAnimeScroll; ScrollingText selectionScroll; + ScrollingText currentEnqueueScroll; - System.Windows.Forms.Timer loop = new System.Windows.Forms.Timer(); + public bool enqueueing = false; + private System.Windows.Forms.Timer loop = new System.Windows.Forms.Timer(); StreamWriter logStream; @@ -99,6 +101,7 @@ public Form() currentAnimeScroll = new ScrollingText(currentAnimeLabel); selectionScroll = new ScrollingText(CurrentSelection); + currentEnqueueScroll = new ScrollingText(CurrentEnqueue); loop.Interval = 100; loop.Tick += (object sender, EventArgs e) => { @@ -414,6 +417,32 @@ private void AddEpisodes_Click(object sender, EventArgs e) manager.AddEpisodes(links, (AutoDownloader_9Animeid.Type)subbedDubbed); } + public void SetQueue(AutoDownloader_9Animeid.Link? link) + { + if (InvokeRequired) + { + this.Invoke(new Action(SetQueue), new object[] { link }); + return; + } + + if (link != null) + currentEnqueueScroll.Text = link.Value.ToString(); + else + currentEnqueueScroll.Text = string.Empty; + } + + public void RestoreEpisodeToQueue(AutoDownloader_9Animeid.Link link, bool first = true) + { + if (InvokeRequired) + { + this.Invoke(new Action(RestoreEpisodeToQueue), new object[] { link, first }); + return; + } + + if (Downloads.Items.Count == 0 || !first) Downloads.Items.Add(manager.current.Value); + else Downloads.Items.Insert(0, manager.current.Value); + } + public void RestoreEpisode(AutoDownloader_9Animeid.Link link) { if (InvokeRequired) @@ -468,8 +497,14 @@ private void Type_SelectedIndexChanged(object sender, EventArgs e) } } - private void Download_Click(object sender, EventArgs e) + public void Download_Click(object sender, EventArgs e) { + if (InvokeRequired) + { + this.Invoke(new Action(Download_Click), new object[] { sender, e }); + return; + } + if (Download.Text == "Start Queue") { if (manager.savePath == string.Empty) @@ -483,6 +518,7 @@ private void Download_Click(object sender, EventArgs e) Download.Text = "Pause Queue"; loop.Start(); + enqueueing = true; Downloads.Enabled = false; Downloads.SelectedIndices.Clear(); @@ -491,6 +527,7 @@ private void Download_Click(object sender, EventArgs e) { Download.Text = "Start Queue"; loop.Stop(); + enqueueing = false; Downloads.Enabled = true; } diff --git a/updateInfo.update b/updateInfo.update index ab62809..8aed6f8 100644 --- a/updateInfo.update +++ b/updateInfo.update @@ -1,5 +1,5 @@ -Auto Downloader 1.2.7 +Auto Downloader 1.2.8 -Bug Fixes: -- Fixed a bug where .ini files where formatted wrong -- Fixed a bug where episodes with blank name would not be deserialized \ No newline at end of file +New Features: +- Ability to cancel enqueueing process +- Shows episode current being enqueued \ No newline at end of file