Skip to content

Commit

Permalink
Added ability to cancel enqueue process
Browse files Browse the repository at this point in the history
  • Loading branch information
randomuserhi committed Sep 4, 2022
1 parent 2dfcc9a commit 1e6b444
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 110 deletions.
262 changes: 159 additions & 103 deletions AutoDownloader/AutoDownloader/DownloadManager/Managers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -704,7 +759,7 @@ public async Task<Link[]> 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)
{
Expand Down Expand Up @@ -875,7 +930,7 @@ public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string

form.SetProgress(progress);

return true;
return !manager.enqueueCancelToken.IsCancellationRequested;
}
else
{
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -972,7 +1029,6 @@ public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser,
else files[id] = progress;

form.SetProgress(progress);
form.DownloadControl(true);
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions AutoDownloader/AutoDownloader/Form.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1e6b444

Please sign in to comment.