diff --git a/src/CyberdropDownloader.Avalonia/CyberdropDownloader.Avalonia.csproj b/src/CyberdropDownloader.Avalonia/CyberdropDownloader.Avalonia.csproj index 04945e7..962bfc1 100644 --- a/src/CyberdropDownloader.Avalonia/CyberdropDownloader.Avalonia.csproj +++ b/src/CyberdropDownloader.Avalonia/CyberdropDownloader.Avalonia.csproj @@ -4,17 +4,18 @@ net5.0 enable Cyberdrop Downloader - Copyright © 2021 + Copyright © 2022 Cyberdrop Downloader - 2.2.3.0 - 2.2.2.0 + 2.2.4.0 + 2.2.4.0 en Assets\duck.ico - 2.2.3 + 2.2.4 + https://github.com/izqalan/cy-client none diff --git a/src/CyberdropDownloader.Avalonia/ViewModels/MainWindowViewModel.cs b/src/CyberdropDownloader.Avalonia/ViewModels/MainWindowViewModel.cs index c60f225..c67449b 100644 --- a/src/CyberdropDownloader.Avalonia/ViewModels/MainWindowViewModel.cs +++ b/src/CyberdropDownloader.Avalonia/ViewModels/MainWindowViewModel.cs @@ -10,6 +10,7 @@ using System.Reflection; using System.Threading; using System.Threading.Tasks; +using CyberdropDownloader.Core.Exceptions; namespace CyberdropDownloader.Avalonia.ViewModels { @@ -115,8 +116,29 @@ await Task.Run(async () => _cancellationTokenSource = new CancellationTokenSource(); } - // Load the album - await _webScraper.LoadAlbumAsync(url); + try + { + await _webScraper.LoadAlbumAsync(url); + } + catch(Exception exception) + { + switch(exception) + { + case NullAlbumTitleException: + Log("Failed to fetch album title."); + break; + + case NullAlbumSizeException: + Log("Failed to fetch album size."); + break; + + case NullAlbumFilesException: + Log("Failed to fetch album files."); + break; + } + + continue; + } // If the album url is invalid, then log and skip over it if(!_webScraper.Successful) diff --git a/src/CyberdropDownloader.Core/Exceptions/NullAlbumFilesException.cs b/src/CyberdropDownloader.Core/Exceptions/NullAlbumFilesException.cs new file mode 100644 index 0000000..072da36 --- /dev/null +++ b/src/CyberdropDownloader.Core/Exceptions/NullAlbumFilesException.cs @@ -0,0 +1,24 @@ +using System; +using System.Runtime.Serialization; + +namespace CyberdropDownloader.Core.Exceptions +{ + public class NullAlbumFilesException : Exception + { + public NullAlbumFilesException() + { + } + + public NullAlbumFilesException(string message) : base(message) + { + } + + public NullAlbumFilesException(string message, Exception innerException) : base(message, innerException) + { + } + + protected NullAlbumFilesException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} diff --git a/src/CyberdropDownloader.Core/Exceptions/NullAlbumSizeException.cs b/src/CyberdropDownloader.Core/Exceptions/NullAlbumSizeException.cs new file mode 100644 index 0000000..c07ef06 --- /dev/null +++ b/src/CyberdropDownloader.Core/Exceptions/NullAlbumSizeException.cs @@ -0,0 +1,24 @@ +using System; +using System.Runtime.Serialization; + +namespace CyberdropDownloader.Core.Exceptions +{ + public class NullAlbumSizeException : Exception + { + public NullAlbumSizeException() + { + } + + public NullAlbumSizeException(string message) : base(message) + { + } + + public NullAlbumSizeException(string message, Exception innerException) : base(message, innerException) + { + } + + protected NullAlbumSizeException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} diff --git a/src/CyberdropDownloader.Core/Exceptions/NullAlbumTitleException.cs b/src/CyberdropDownloader.Core/Exceptions/NullAlbumTitleException.cs new file mode 100644 index 0000000..ace5286 --- /dev/null +++ b/src/CyberdropDownloader.Core/Exceptions/NullAlbumTitleException.cs @@ -0,0 +1,24 @@ +using System; +using System.Runtime.Serialization; + +namespace CyberdropDownloader.Core.Exceptions +{ + public class NullAlbumTitleException : Exception + { + public NullAlbumTitleException() + { + } + + public NullAlbumTitleException(string message) : base(message) + { + } + + public NullAlbumTitleException(string message, Exception innerException) : base(message, innerException) + { + } + + protected NullAlbumTitleException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + } +} diff --git a/src/CyberdropDownloader.Core/WebScraper.cs b/src/CyberdropDownloader.Core/WebScraper.cs index 2bbfbda..d79fab4 100644 --- a/src/CyberdropDownloader.Core/WebScraper.cs +++ b/src/CyberdropDownloader.Core/WebScraper.cs @@ -1,4 +1,5 @@ using CyberdropDownloader.Core.DataModels; +using CyberdropDownloader.Core.Exceptions; using HtmlAgilityPack; using System; using System.Collections.Generic; @@ -7,76 +8,80 @@ namespace CyberdropDownloader.Core { - public class WebScraper - { - private string _url; - private HtmlDocument _htmlDocument; + public class WebScraper + { + private Album _album; + private bool _successful; - private Album _album; - private bool _successful; + public Album Album => _album; + public bool Successful => _successful; - public Album Album => _album; - public bool Successful => _successful; + public async Task LoadAlbumAsync(string url) + { + await Task.Run(async () => + { + // Load webpage + HtmlDocument htmlDocument = await new HtmlWeb().LoadFromWebAsync(url); - public async Task LoadAlbumAsync(string url) - { - _url = url; - _successful = false; + if(htmlDocument != null) + { + try + { + (string title, string size, Queue files) albumData = FetchAlbumData(htmlDocument); - await Task.Run(async () => - { - await LoadHtmlDocumenteAsync(); + _album = new Album(albumData.title, albumData.size, albumData.files); + _successful = true; + } + catch(Exception) + { + _successful = false; + } + } + }); + } - if (_htmlDocument != null) - { - try - { - // Insatiate new album with title, size, and files. - _album = new Album(FetchAlbumTitle(), FetchAlbumSize(), FetchAlbumFiles()); - _successful = true; - } - catch - { - _successful = false; - } - } - }); - } + #region Load Album + private (string title, string size, Queue files) FetchAlbumData(HtmlDocument htmlDocument) + { + return (FetchAlbumTitle(htmlDocument), FetchAlbumSize(htmlDocument), FetchAlbumFiles(htmlDocument)); + } - #region Load Album - private string FetchAlbumTitle() => _htmlDocument.DocumentNode.SelectNodes("//div/h1[@id='title']").First().Attributes["title"].Value; + private string FetchAlbumTitle(HtmlDocument htmlDocument) + { + string title = htmlDocument.DocumentNode.SelectNodes("//div/h1[@id='title']").First().Attributes["title"].Value; - private string FetchAlbumSize() => _htmlDocument.DocumentNode.SelectNodes("//div/p[@class='title']")[1].InnerHtml; + return title ?? throw new NullAlbumTitleException(); + } - private Queue FetchAlbumFiles() - { - Queue urls = new Queue(); + private string FetchAlbumSize(HtmlDocument htmlDocument) + { + string size = htmlDocument.DocumentNode.SelectNodes("//div/p[@class='title']")[1].InnerHtml; - HtmlNodeCollection nodes = _htmlDocument.DocumentNode.SelectNodes("//a[@class='image'][@href]"); + return size ?? throw new NullAlbumFilesException(); + } - foreach (HtmlNode link in nodes) - { - urls.Enqueue(new AlbumFile() - { - Name = link.Attributes["title"].Value, - Url = link.Attributes["href"].Value - }); - } + private Queue FetchAlbumFiles(HtmlDocument htmlDocument) + { + Queue urls = new Queue(); - return urls; - } + HtmlNodeCollection files = htmlDocument.DocumentNode.SelectNodes("//a[@class='image'][@href]"); - private async Task LoadHtmlDocumenteAsync() - { - try - { - _htmlDocument = await new HtmlWeb().LoadFromWebAsync(_url); - } - catch (Exception) - { - _htmlDocument = null!; - } - } - #endregion - } + if(files == null) + { + throw new NullAlbumFilesException(); + } + + foreach(HtmlNode link in files) + { + urls.Enqueue(new AlbumFile() + { + Name = link.Attributes["title"].Value, + Url = link.Attributes["href"].Value + }); + } + + return urls; + } + #endregion + } }