Skip to content

Commit 54c4f0f

Browse files
committed
Add statistics page download helpers
1 parent 52d1586 commit 54c4f0f

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

SabreTools.RedumpLib/Web/RedumpClient.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,76 @@ public async Task<bool> DownloadSingleWIPID(int id, string? outDir, bool rename)
13651365
}
13661366
}
13671367

1368+
/// <summary>
1369+
/// Download the statistics page
1370+
/// </summary>
1371+
/// <returns>String containing the page contents if successful, null on error</returns>
1372+
public async Task<string?> DownloadStatisticsPage()
1373+
{
1374+
Console.WriteLine("Processing statistics page");
1375+
try
1376+
{
1377+
// Try to retrieve the data
1378+
string statisticsUrl = UrlBuilder.BuildStatisticsUrl();
1379+
string? listPage = await DownloadString(statisticsUrl);
1380+
1381+
if (listPage is null)
1382+
{
1383+
Console.Error.WriteLine("An error occurred retrieving statistics page!");
1384+
return null;
1385+
}
1386+
1387+
Console.WriteLine("Statistics page has been successfully downloaded");
1388+
return listPage;
1389+
}
1390+
catch (Exception ex)
1391+
{
1392+
Console.Error.WriteLine($"An exception has occurred: {ex}");
1393+
return null;
1394+
}
1395+
}
1396+
1397+
/// <summary>
1398+
/// Download the statistics page
1399+
/// </summary>
1400+
/// <param name="outDir">Output directory to save data to</param>
1401+
/// <returns>True if all data was downloaded, false otherwise</returns>
1402+
public async Task<bool> DownloadStatisticsPage(string? outDir)
1403+
{
1404+
// If no output directory is defined, use the current directory instead
1405+
if (string.IsNullOrEmpty(outDir))
1406+
outDir = Environment.CurrentDirectory;
1407+
1408+
Console.WriteLine("Processing statistics page");
1409+
try
1410+
{
1411+
// Try to retrieve the data
1412+
string? statisticsPage = await DownloadStatisticsPage();
1413+
1414+
if (statisticsPage is null)
1415+
{
1416+
Console.Error.WriteLine("An error occurred retrieving statistics page!");
1417+
return false;
1418+
}
1419+
1420+
// Write the list to the output directory
1421+
Directory.CreateDirectory(outDir);
1422+
using (var listStreamWriter = File.CreateText(Path.Combine(outDir, $"statistics.html")))
1423+
{
1424+
listStreamWriter.Write(statisticsPage);
1425+
listStreamWriter.Flush();
1426+
}
1427+
1428+
Console.WriteLine("Statistics page has been successfully downloaded");
1429+
return true;
1430+
}
1431+
catch (Exception ex)
1432+
{
1433+
Console.Error.WriteLine($"An exception has occurred: {ex}");
1434+
return false;
1435+
}
1436+
}
1437+
13681438
#endregion
13691439

13701440
#region Helpers

0 commit comments

Comments
 (0)