Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updated WikipediaRegistryProvider #221

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/IbanNet.CodeGen/IbanNet.CodeGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

<ItemGroup>
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.67" />
<PackageReference Include="System.Text.Json" Version="6.0.9" />
</ItemGroup>

<ItemGroup>
Expand Down
45 changes: 45 additions & 0 deletions src/IbanNet.CodeGen/Wikipedia/Loader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
using HtmlAgilityPack;

namespace IbanNet.CodeGen.Wikipedia;

public static class Loader
{
public static WikiResult GetWikiData()
{
var uri = new Uri("https://en.wikipedia.org/w/api.php?format=json&action=parse&page=International_Bank_Account_Number&section=16", UriKind.Absolute);
HttpWebRequest req = WebRequest.CreateHttp(uri);
using WebResponse response = req.GetResponse();
using var ms = new MemoryStream();
response.GetResponseStream().CopyTo(ms);
byte[] buffer = ms.ToArray();

var jsonOpts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
WikiResponse? wikiResponse = JsonSerializer.Deserialize<WikiResponse>(buffer, jsonOpts);

var tableRegex = new Regex("(<table[^\\>]*?>.*<\\/table>)", RegexOptions.Singleline);
Match tableMatch = tableRegex.Match(wikiResponse.Parse.Text["*"]);

var doc = new HtmlDocument();
doc.LoadHtml($"<div>${tableMatch.Value}</div>");

IEnumerable<WikiRecord>? records = doc.DocumentNode.SelectNodes("//tr")
.GroupBy(e => e.ParentNode)
.Select(g => g.Where(e => e.Element("td") != null))
.Select(rows => rows.Select(r =>
{
var cells = r.Elements("td").ToList();
return new WikiRecord
{
CountryCode = cells[3].SelectSingleNode(".//code").InnerText.Substring(0, 2).Trim(),
EnglishName = cells[0].SelectSingleNode(".//a").InnerText.Trim(),
Pattern = cells[2].InnerText.Trim().Replace(" ", "")
};
}))
.SelectMany(_ => _);

return new WikiResult(records, wikiResponse.Parse);
}
}
8 changes: 8 additions & 0 deletions src/IbanNet.CodeGen/Wikipedia/ParseResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace IbanNet.CodeGen.Wikipedia;

public class ParseResult
{
public int PageId { get; set; }
public int RevId { get; set; }
public Dictionary<string, string> Text { get; set; }
}
25 changes: 25 additions & 0 deletions src/IbanNet.CodeGen/Wikipedia/WikiRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Globalization;

namespace IbanNet.CodeGen.Wikipedia;

public record WikiRecord
{
public string CountryCode { get; init; } = default!;
public string EnglishName { get; init; } = default!;
public string Pattern { get; init; } = default!;

public string? NativeName
{
get
{
try
{
return new RegionInfo(CountryCode).NativeName;
}
catch (ArgumentException)
{
return null;
}
}
}
}
6 changes: 6 additions & 0 deletions src/IbanNet.CodeGen/Wikipedia/WikiResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace IbanNet.CodeGen.Wikipedia;

public class WikiResponse
{
public ParseResult Parse { get; set; }
}
3 changes: 3 additions & 0 deletions src/IbanNet.CodeGen/Wikipedia/WikiResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace IbanNet.CodeGen.Wikipedia;

public record WikiResult(IEnumerable<WikiRecord> Records, ParseResult Parse);
Loading
Loading