Skip to content

Commit

Permalink
#61 unknown webscraper error (#72)
Browse files Browse the repository at this point in the history
* Update WebScraper.cs

* Update CyberdropDownloader.Avalonia.csproj

* Update MainWindowViewModel.cs
  • Loading branch information
Anequit authored Jan 23, 2022
1 parent 9c81d32 commit e4e99b6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<AssemblyName>Cyberdrop Downloader</AssemblyName>
<Copyright>Copyright © 2022</Copyright>
<Product>Cyberdrop Downloader</Product>
<AssemblyVersion>2.2.5.0</AssemblyVersion>
<FileVersion>2.2.4.0</FileVersion>
<AssemblyVersion>2.2.6.0</AssemblyVersion>
<FileVersion>2.2.6.0</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
<Company />
<Authors />
<PackageIconUrl />
<ApplicationIcon>Assets\duck.ico</ApplicationIcon>
<Win32Resource />
<Version>2.2.5</Version>
<Version>2.2.6</Version>
<PackageProjectUrl>https://github.com/izqalan/cy-client</PackageProjectUrl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
23 changes: 10 additions & 13 deletions src/CyberdropDownloader.Avalonia/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ await Task.Run(async () =>
{
await _webScraper.LoadAlbumAsync(url);
}
catch(Exception exception)
catch(Exception ex)
{
switch(exception)
switch(ex)
{
case NullAlbumTitleException:
Log("Failed to fetch album title.");
Expand All @@ -136,19 +136,16 @@ await Task.Run(async () =>
Log("Failed to fetch album files.");
break;

case UriFormatException:
Log("Invalid URL format.");
continue;

default:
Log("Unknown webscraper error. Please report this to the github repository.");
Log($"Unknown webscraper error. Please report this to the github repository. {ex.Message}");
continue;
}
}

// If the album url is invalid, then log and skip over it
if(!_webScraper.Successful)
{
Log($"Invalid Url: {url}");
continue;
}

// Update album title
UpdateAlbumTitle(_webScraper.Album.Title);

Expand All @@ -174,9 +171,9 @@ await Task.Run(async () =>
continue;
}
}
catch(Exception exception)
catch(Exception ex)
{
switch(exception)
switch(ex)
{
// Drive letter doesn't exist or path doesn't exist
case ArgumentException:
Expand All @@ -194,7 +191,7 @@ await Task.Run(async () =>
break;

default:
Log("Unknown error. Please report this to the github repository.");
Log($"Unknown error. Please report this to the github repository. {ex.Message}");
break;
}
}
Expand Down
72 changes: 55 additions & 17 deletions src/CyberdropDownloader.Core/WebScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ namespace CyberdropDownloader.Core
public class WebScraper
{
private Album _album;
private bool _successful;

public Album Album => _album;
public bool Successful => _successful;

public async Task LoadAlbumAsync(string url)
{
Expand All @@ -28,7 +26,6 @@ await Task.Run(async () =>
(string title, string size, Queue<AlbumFile> files) albumData = FetchAlbumData(htmlDocument);

_album = new Album(albumData.title, albumData.size, albumData.files);
_successful = true;
}
});
}
Expand All @@ -41,36 +38,77 @@ await Task.Run(async () =>

private string FetchAlbumTitle(HtmlDocument htmlDocument)
{
string title = htmlDocument.DocumentNode.SelectNodes("//div/h1[@id='title']").First().Attributes["title"].Value;
string title;

return title ?? throw new NullAlbumTitleException();
try
{
title = htmlDocument.DocumentNode.SelectNodes("//div/h1[@id='title']").First().Attributes["title"].Value;
}
catch(Exception ex)
{
switch(ex)
{
case ArgumentNullException or NullReferenceException:
throw new NullAlbumTitleException();

default:
throw new Exception(ex.Message);
}
}

return title;
}

private string FetchAlbumSize(HtmlDocument htmlDocument)
{
string size = htmlDocument.DocumentNode.SelectNodes("//div/p[@class='title']")[1].InnerHtml;
string size;

try
{
size = htmlDocument.DocumentNode.SelectNodes("//div/p[@class='title']")[1].InnerHtml;
}
catch(Exception ex)
{
switch(ex)
{
case ArgumentNullException or NullReferenceException:
throw new NullAlbumSizeException();

default:
throw new Exception(ex.Message);
}
}

return size ?? throw new NullAlbumFilesException();
return size;
}

private Queue<AlbumFile> FetchAlbumFiles(HtmlDocument htmlDocument)
{
Queue<AlbumFile> urls = new Queue<AlbumFile>();

HtmlNodeCollection files = htmlDocument.DocumentNode.SelectNodes("//a[@class='image'][@href]");

if(files == null)
try
{
throw new NullAlbumFilesException();
HtmlNodeCollection files = htmlDocument.DocumentNode.SelectNodes("//a[@class='image'][@href]");

foreach(HtmlNode link in files)
{
urls.Enqueue(new AlbumFile()
{
Name = link.Attributes["title"].Value,
Url = link.Attributes["href"].Value
});
}
}

foreach(HtmlNode link in files)
catch(Exception ex)
{
urls.Enqueue(new AlbumFile()
switch(ex)
{
Name = link.Attributes["title"].Value,
Url = link.Attributes["href"].Value
});
case ArgumentNullException or NullReferenceException:
throw new NullAlbumFilesException();

default:
throw new Exception(ex.Message);
}
}

return urls;
Expand Down

0 comments on commit e4e99b6

Please sign in to comment.