Skip to content

Commit

Permalink
Fix Hit'n'Run list DeprecatedException
Browse files Browse the repository at this point in the history
Parsing the Torrent's ratio to float with the CultureInfo who uses comma
instead of dot when parsing float throws FormatException.
  • Loading branch information
PoLaKoSz committed Jun 20, 2020
1 parent f00b2f0 commit 027c6f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
34 changes: 27 additions & 7 deletions src/PoLaKoSz.Ncore/Parsers/HitAndRunParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AngleSharp;
using AngleSharp.Dom;
using PoLaKoSz.Ncore.Exceptions;
Expand All @@ -12,7 +13,7 @@ internal class HitAndRunParser
private static string torrentUrlShema = "torrents.php?action=details&id=";

/// <exception cref="DeprecatedWrapperException"></exception>
internal async System.Threading.Tasks.Task<IEnumerable<HitAndRunTorrent>> ExtractResultsFromAsync(string html)
internal async Task<IEnumerable<HitAndRunTorrent>> ExtractResultsFromAsync(string html)
{
List<HitAndRunTorrent> torrents = new List<HitAndRunTorrent>();

Expand Down Expand Up @@ -50,20 +51,26 @@ internal async System.Threading.Tasks.Task<IEnumerable<HitAndRunTorrent>> Extrac
private HitAndRunTorrent ParseAsTorrent(IElement torrentNode, DateTime currentTime)
{
IElement nameNode = torrentNode.QuerySelector("div.hnr_tname a nobr");
IElement ratioNode = torrentNode.QuerySelector("div.hnr_tratio span");

return new HitAndRunTorrent(
ParseID(torrentNode),
nameNode.TextContent,
ParseEnd(torrentNode, currentTime),
float.Parse(ratioNode.TextContent));
ParseRatio(torrentNode));
}

private int ParseID(IElement torrentNode)
{
IElement idNode = torrentNode.QuerySelector("div.hnr_tname a");
string href = idNode.GetAttribute("href");
return int.Parse(href.Substring(torrentUrlShema.Length));
try
{
IElement idNode = torrentNode.QuerySelector("div.hnr_tname a");
string href = idNode.GetAttribute("href");
return int.Parse(href.Substring(torrentUrlShema.Length));
}
catch (Exception ex)
{
throw new DeprecatedWrapperException("Couldn't parse the ID of the torrent", torrentNode, ex);
}
}

private DateTime ParseEnd(IElement torrentNode, DateTime baseTime)
Expand All @@ -86,5 +93,18 @@ private DateTime ParseEnd(IElement torrentNode, DateTime baseTime)

return baseTime.AddMinutes(minutes);
}

private float ParseRatio(IElement torrentNode)
{
try
{
IElement ratioNode = torrentNode.QuerySelector("div.hnr_tratio span");
return float.Parse(ratioNode.TextContent, System.Globalization.CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
throw new DeprecatedWrapperException("Couldn't parse the ratio of the torrent", torrentNode, ex);
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/PoLaKoSz.Ncore/PoLaKoSz.Ncore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Product>nCore</Product>
<PackageReleaseNotes>First stable release. The enty point to the library is the PoLaKoSz.Ncore.NcoreClient class.</PackageReleaseNotes>
<Description>nCore.cc is the largest hungarian torrent side. This .NET Core 2.1 library helps to access its content (Torrent details, user Hit'n'Runs, Search for Torrent).</Description>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/EndPoints/HitAndRunEndPointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task ListFirstTorrentIDParsedCorrectly()
IEnumerable<HitAndRunTorrent> torrents = await endPoint.List().ConfigureAwait(false);
HitAndRunTorrent firstTorrent = torrents.First();

Assert.That(830948, Is.EqualTo( firstTorrent.ID));
Assert.That(830948, Is.EqualTo(firstTorrent.ID));
}

[Test]
Expand Down

0 comments on commit 027c6f9

Please sign in to comment.