Refactor and enhance Download Manager application
- Added new services: IContentDialogService, ISnackbarService - Replaced DashboardPage and DashboardViewModel with DownloadingPage, DownloadingPageViewModel, WaitingPage, and WaitingPageViewModel - Introduced AddNewTaskPage and AddNewTaskViewModel - Replaced DownloadServiceManger with DownloadManagerService - Updated WPF-UI package to 3.0.4 and added HtmlAgilityPack 1.11.61 - Refactored DownloadWorker to use async methods and added retry logic - Converted DownloadItemData to partial class with ObservableProperty attributes - Changed initial navigation page to DownloadingPage - Updated MainWindow navigation items and title - Added new utility classes: DoubleUtilities, MD5Utilities, StringExtension - Added new XAML pages and ViewModels for task management and display - Removed obsolete DashboardPage and related files
This commit is contained in:
63
Models/UrlGetter/YandeUrlGetter.cs
Normal file
63
Models/UrlGetter/YandeUrlGetter.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using DownloadManager.Utilities;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DownloadManager.Models.UrlGetter;
|
||||
public class YandeUrlGetter : IUrlGetter
|
||||
{
|
||||
readonly HttpClient client = new ();
|
||||
const int maxRetries = 3;
|
||||
const int delayMilliseconds = 1000;
|
||||
|
||||
public async Task<FileUrl> GetFileUrl(string taskUrl)
|
||||
{
|
||||
// Get the ID of the post from the URL
|
||||
var id = taskUrl.Split("https://yande.re/post/show/").Last();
|
||||
|
||||
for (int attempt = 0; attempt < maxRetries; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await client.GetAsync($"https://yande.re/post.json?tags=id:{id}");
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var data = await response.Content.ReadFromJsonAsync<JsonElement[]>();
|
||||
if (data == null || data.Length == 0)
|
||||
{
|
||||
return new FileUrl(null, null, null);
|
||||
}
|
||||
|
||||
var firstElement = data.FirstOrDefault();
|
||||
if (firstElement.ValueKind != JsonValueKind.Object || !firstElement.TryGetProperty("file_url", out var fileUrlElement))
|
||||
{
|
||||
return new FileUrl(null, null, null);
|
||||
}
|
||||
|
||||
if (!firstElement.TryGetProperty("md5", out var md5Element))
|
||||
{
|
||||
return new FileUrl(null, null, null);
|
||||
}
|
||||
|
||||
var fileUrl = fileUrlElement.GetString();
|
||||
var fileName = Path.GetFileName(fileUrl)?.ToUnescapedString();
|
||||
var md5 = md5Element.GetString();
|
||||
|
||||
return new FileUrl(fileUrl, fileName, md5);
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
// Log the exception if needed
|
||||
}
|
||||
|
||||
// Wait before retrying
|
||||
await Task.Delay(delayMilliseconds);
|
||||
}
|
||||
|
||||
// If all retries fail, return a FileUrl with null values
|
||||
return new FileUrl(null, null, null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user