Skip to content

Commit

Permalink
Collections bugfixes & General Improvements
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
intel777 committed Dec 20, 2020
1 parent 5d1f1be commit 65fcff0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Wallhaven Downloader V2/Main.Designer.cs

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

30 changes: 20 additions & 10 deletions Wallhaven Downloader V2/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()));
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions Wallhaven Downloader V2/SearchParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class SearchParameters {
public List<string> ratios;
public List<string> colors;
public int page;
public int end_page;
public string seed;

public SearchParameters() {
Expand All @@ -32,6 +33,7 @@ public SearchParameters() {
colors = new List<string>() { };
ratios = new List<String>() { };
page = 1;
end_page = 0;
seed = "";
}

Expand Down

0 comments on commit 65fcff0

Please sign in to comment.