Skip to content

Commit 57febd6

Browse files
committed
feat: implement asynchronous fetching of multiple pages
1 parent 7af7797 commit 57febd6

File tree

1 file changed

+64
-29
lines changed

1 file changed

+64
-29
lines changed

AdnmbBackup-gui/Form1.cs

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
7171
System.Diagnostics.Process.Start("https://www.coldthunder11.com/index.php/2020/03/19/%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96a%e5%b2%9b%e7%9a%84%e9%a5%bc%e5%b9%b2/");
7272
}
7373

74-
private void button1_Click(object sender, EventArgs e)
74+
private async void button1_Click(object sender, EventArgs e)
7575
{
7676
string id = textBox1.Text;
7777
if (id == string.Empty) return;
@@ -155,21 +155,12 @@ private void button1_Click(object sender, EventArgs e)
155155
var replyCount = int.Parse(fpjson["ReplyCount"].ToString());
156156
int pageCount = replyCount / 19;
157157
if (replyCount % 19 != 0) pageCount++;
158-
for (int page = pageCountInCache; page <= pageCount; page++)
158+
if (pageCount >= pageCountInCache)
159159
{
160-
label4.Text = "第" + page + "页";
161-
t = http.GetAsync(url + "?id=" + id + "&page=" + page);
162-
t.Wait();
163-
result = t.Result;
164-
t2 = result.Content.ReadAsByteArrayAsync();
165-
t2.Wait();
166-
bytes = t2.Result;
167-
str = ReadGzip(bytes);
168-
var jo = JsonConvert.DeserializeObject<JObject>(str);
169-
JArray ja = jo["Replies"].ToObject<JArray>();
170-
foreach (var item in ja)
160+
label4.Text = "正在获取多个页面...";
161+
JArray additionalReplies = await FetchPagesInParallelAsync(http, url, id, pageCountInCache, pageCount);
162+
foreach (var item in additionalReplies)
171163
{
172-
if (item["user_hash"].ToString() == "Tips") continue;
173164
contentJA.Add(item);
174165
}
175166
}
@@ -217,23 +208,13 @@ private void button1_Click(object sender, EventArgs e)
217208
int pageCount = replyCount / 19;
218209
if (replyCount % 19 != 0) pageCount++;
219210
JArray contentJA = fpjson["Replies"].ToObject<JArray>();
220-
for (var page = 2; page <= pageCount; page++)
211+
if (pageCount > 1)
221212
{
222-
label4.Text = "正在获取第" + page + "页";
223-
t = http.GetAsync(url + "?id=" + id + "&page=" + page);
224-
t.Wait();
225-
result = t.Result;
226-
t2 = result.Content.ReadAsByteArrayAsync();
227-
t2.Wait();
228-
bytes = t2.Result;
229-
str = ReadGzip(bytes);
230-
var jo = JsonConvert.DeserializeObject<JObject>(str);
231-
JArray ja = jo["Replies"].ToObject<JArray>();
232-
var rpcount = ja.Count;
233-
for (int j = 0; j < rpcount; j++)
213+
label4.Text = "正在获取多个页面...";
214+
JArray additionalReplies = await FetchPagesInParallelAsync(http, url, id, 2, pageCount);
215+
foreach (var item in additionalReplies)
234216
{
235-
if (ja[j]["user_hash"].ToString() == "Tips") continue;
236-
contentJA.Add(ja[j]);
217+
contentJA.Add(item);
237218
}
238219
}
239220
label4.Text = "完成";
@@ -259,6 +240,60 @@ private void button1_Click(object sender, EventArgs e)
259240
ConvertToMarkdown(id);
260241
ConvertToMarkdownPoOnly(id);
261242
}
243+
244+
private async Task<JArray> FetchPagesInParallelAsync(HttpClient http, string url, string id, int startPage, int endPage)
245+
{
246+
JArray resultArray = new JArray();
247+
var tasks = new List<Task<Tuple<int, JArray>>>();
248+
using (SemaphoreSlim semaphore = new SemaphoreSlim(16)) // limit to 16 concurrent tasks
249+
{
250+
for (int page = startPage; page <= endPage; page++)
251+
{
252+
int pageNumber = page; // capture page number locally
253+
tasks.Add(Task.Run(async () =>
254+
{
255+
await semaphore.WaitAsync();
256+
try
257+
{
258+
return await FetchPageAsync(http, url, id, pageNumber);
259+
}
260+
finally
261+
{
262+
semaphore.Release();
263+
}
264+
}));
265+
}
266+
var results = await Task.WhenAll(tasks);
267+
foreach (var result in results.OrderBy(r => r.Item1))
268+
{
269+
foreach (var item in result.Item2)
270+
{
271+
if (item["user_hash"].ToString() == "Tips") continue;
272+
resultArray.Add(item);
273+
}
274+
}
275+
}
276+
return resultArray;
277+
}
278+
279+
private async Task<Tuple<int, JArray>> FetchPageAsync(HttpClient http, string url, string id, int page)
280+
{
281+
try
282+
{
283+
var response = await http.GetAsync(url + "?id=" + id + "&page=" + page);
284+
var bytes = await response.Content.ReadAsByteArrayAsync();
285+
string str = ReadGzip(bytes);
286+
var jo = JsonConvert.DeserializeObject<JObject>(str);
287+
JArray ja = jo["Replies"].ToObject<JArray>();
288+
return Tuple.Create(page, ja);
289+
}
290+
catch (Exception ex)
291+
{
292+
Console.WriteLine($"Error fetching page {page}: {ex.Message}");
293+
return Tuple.Create(page, new JArray());
294+
}
295+
}
296+
262297
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
263298
{
264299
System.Diagnostics.Process.Start("https://github.com/Ovler-Young/AdnmbBackup-gui");

0 commit comments

Comments
 (0)