From 65fcff0b87f0236a7498a69fbb78d8766eb0ca38 Mon Sep 17 00:00:00 2001 From: Intel Date: Sun, 20 Dec 2020 12:13:11 +0200 Subject: [PATCH] Collections bugfixes & General Improvements - Proper page limiter as wallhaven tends to return different amount of images per page in different circumstances - Fixed user collections selector not working (fixes #2) - Fixed progress bar not updating while fetching target collection info - Fixed "429: Too many requests" while fetching target collection info (fixes #2) - Collection info fetcher now uses general search_params.page instead of his own one. --- Wallhaven Downloader V2/Main.Designer.cs | 3 ++- Wallhaven Downloader V2/Main.cs | 30 ++++++++++++++------- Wallhaven Downloader V2/SearchParameters.cs | 2 ++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Wallhaven Downloader V2/Main.Designer.cs b/Wallhaven Downloader V2/Main.Designer.cs index 249bbf5..9cacef6 100644 --- a/Wallhaven Downloader V2/Main.Designer.cs +++ b/Wallhaven Downloader V2/Main.Designer.cs @@ -734,7 +734,8 @@ private void InitializeComponent() this.ImageSourceCollectionsListBox.Name = "ImageSourceCollectionsListBox"; this.ImageSourceCollectionsListBox.Size = new System.Drawing.Size(186, 69); this.ImageSourceCollectionsListBox.TabIndex = 11; - this.ImageSourceCollectionsListBox.TabIndexChanged += new System.EventHandler(this.ImageSourceCollectionsListBox_IndexChanged); + this.ImageSourceCollectionsListBox.SelectedIndexChanged += new System.EventHandler(this.ImageSourceCollectionsListBox_SelectedIndexChanged); + this.ImageSourceCollectionsListBox.TabIndexChanged += new System.EventHandler(this.ImageSourceCollectionsListBox_SelectedIndexChanged); // // AmountToDownloadLabel // diff --git a/Wallhaven Downloader V2/Main.cs b/Wallhaven Downloader V2/Main.cs index 9b4265a..8b42144 100644 --- a/Wallhaven Downloader V2/Main.cs +++ b/Wallhaven Downloader V2/Main.cs @@ -664,8 +664,8 @@ private void DownloadButton_Click(object sender, EventArgs e) { int range_end = Int32.Parse(PagesRangeEnd.Text); int range_delta = range_end - range_begin; if (range_delta > 0) { - target_amount = range_delta * 64; search_params.page = range_begin; + search_params.end_page = range_end; Logpush($"New params: target_amount = {target_amount}, page = {search_params.page}"); } else { @@ -693,13 +693,23 @@ private void DownloadButton_Click(object sender, EventArgs e) { target_amount = Int32.Parse(probe.SelectToken("meta.total").ToString()); Logpush($"Amount set to {target_amount} as it was 0"); } + else { + Logpush("Amount will be used to determine when to stop."); + } + if (search_params.end_page == 0) { + search_params.end_page = Int32.Parse(probe.SelectToken("meta.last_page").ToString()); + Logpush($"End page set to {search_params.end_page} as it was 0"); + } + else { + Logpush("Eng page will be used to determine where to stop."); + } if (probe.SelectToken("meta.seed").ToString() != "") { search_params.seed = probe.SelectToken("meta.seed").ToString(); Logpush($"New seed: {search_params.seed}"); } ProgressBarSetValue(0); SetMaxProgressBar(target_amount); - while (Images.Count < target_amount & started) { + while ((Images.Count < target_amount & search_params.page <= search_params.end_page) & started) { Thread.Sleep(1340); foreach (var image in probe.SelectToken("data")) { Images.Add(new Image(image["id"].ToString(), image["path"].ToString())); @@ -720,25 +730,25 @@ private void DownloadButton_Click(object sender, EventArgs e) { Collection target_collection = Collections[selected_collection]; Logpush($"Selected collection is {target_collection.name}, ID: {target_collection.id}, User: {target_collection.owner}"); Logpush("Warn: Only Purity filter can be aplied to collections!"); - int page = 1; - int max_pages = 0; string base_url = $"https://wallhaven.cc/api/v1/collections/{target_collection.owner}/{target_collection.id}"; - JObject response = GetJsonFromURL(base_url + $"?purity={search_params.purity}&page={page}&apikey={APIKey}"); - max_pages = Int32.Parse(response.SelectToken("meta.last_page").ToString()); + JObject response = GetJsonFromURL(base_url + $"?purity={search_params.purity}&page={search_params.page}&apikey={APIKey}"); if (target_amount == 0) { target_amount = Int32.Parse(response.SelectToken("meta.total").ToString()); Logpush($"Amount set to {target_amount} as it was 0"); } - while (Images.Count < target_amount) { + ProgressBarSetValue(0); + SetMaxProgressBar(target_amount); + while ((Images.Count < target_amount & search_params.page <= search_params.end_page) & started) { Thread.Sleep(1340); foreach (var image in response.SelectToken("data")) { Images.Add(new Image(image["id"].ToString(), image["path"].ToString())); + ProgressBarSetValue(Images.Count); if (Images.Count >= target_amount) { break; } - page++; - response = GetJsonFromURL(base_url + $"?purity={search_params.purity}&page={page}&apikey={APIKey}"); } + search_params.page++; + response = GetJsonFromURL(base_url + $"?purity={search_params.purity}&page={search_params.page}&apikey={APIKey}"); } } else { @@ -800,7 +810,7 @@ private void CancelButton_Click(object sender, EventArgs e) { Logpush("[Cancel] Signal send, please wait for main and workers threads to respond..."); } - private void ImageSourceCollectionsListBox_IndexChanged(object sender, EventArgs e) { + private void ImageSourceCollectionsListBox_SelectedIndexChanged(object sender, EventArgs e) { selected_collection = ImageSourceCollectionsListBox.SelectedIndex; } diff --git a/Wallhaven Downloader V2/SearchParameters.cs b/Wallhaven Downloader V2/SearchParameters.cs index d153a35..45329ce 100644 --- a/Wallhaven Downloader V2/SearchParameters.cs +++ b/Wallhaven Downloader V2/SearchParameters.cs @@ -18,6 +18,7 @@ public class SearchParameters { public List ratios; public List colors; public int page; + public int end_page; public string seed; public SearchParameters() { @@ -32,6 +33,7 @@ public SearchParameters() { colors = new List() { }; ratios = new List() { }; page = 1; + end_page = 0; seed = ""; }