Add Logic for EuroMillions Draw Information.

This commit is contained in:
2024-02-13 13:18:14 +00:00
parent ea987c52a4
commit 388b1ec23f

View File

@@ -0,0 +1,733 @@
using HtmlAgilityPack;
using lottery_co_uk_scraper.core.Models;
using lottery_co_uk_scraper.Utilities;
using lottery_co_uk_scraper.core.Exceptions;
using System.Globalization;
using System.Text.RegularExpressions;
namespace lottery_co_uk_scraper.EuroMillions
{
internal partial class DrawWinnerInformation
{
[GeneratedRegex(@"(\d+)")]
private static partial Regex MyRegex();
internal class EurosTableRow()
{
internal const string Match1Plus2Stars = "Match 1 and 2 Stars";
internal const string Match2 = "Match 2";
internal const string Match2Plus1Star = "Match 2 and 1 Star";
internal const string Match2Plus2Stars = "Match 2 and 2 Star";
internal const string Match3 = "Match 3";
internal const string Match3Plus1Star = "Match 3 and 1 Star";
internal const string Match3Plus2Stars = "Match 3 and 2 Star";
internal const string Match4 = "Match 4";
internal const string Match4Plus1Star = "Match 4 and 1 Star";
internal const string Match4Plus2Stars = "Match 4 and 2 Star";
internal const string Match5 = "Match 5";
internal const string Match5Plus1Star = "Match 5 and 1 Star";
internal const string Match5Plus2Stars = "Match 5 and 2 Star";
internal const string Total = "Totals";
internal const string Winners = "UK Winners";
internal const string PrizePerWinner = "Prize Per Winner";
internal const string PrizeFundAmountUK = "UK Prize Fund";
internal const string TotalWinners = "Total Winners";
}
public static void ProcessWinnersTable(HtmlDocument doc, EurosResult eurosResult)
{
try
{
var table = doc.DocumentNode.Descendants("table")
.FirstOrDefault(x => x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("euromillions mobFormat"));
if (table != null)
{
ProcessTableSection(table, EurosTableRow.Match1Plus2Stars, eurosResult);
ProcessTableSection(table, EurosTableRow.Match2, eurosResult);
ProcessTableSection(table, EurosTableRow.Match2Plus1Star, eurosResult);
ProcessTableSection(table, EurosTableRow.Match2Plus2Stars, eurosResult);
ProcessTableSection(table, EurosTableRow.Match3, eurosResult);
ProcessTableSection(table, EurosTableRow.Match3Plus1Star, eurosResult);
ProcessTableSection(table, EurosTableRow.Match3Plus2Stars, eurosResult);
ProcessTableSection(table, EurosTableRow.Match4, eurosResult);
ProcessTableSection(table, EurosTableRow.Match4Plus1Star, eurosResult);
ProcessTableSection(table, EurosTableRow.Match4Plus2Stars, eurosResult);
ProcessTableSection(table, EurosTableRow.Match5, eurosResult);
ProcessTableSection(table, EurosTableRow.Match5Plus1Star, eurosResult);
ProcessTableSection(table, EurosTableRow.Match5Plus2Stars, eurosResult);
ProcessTableSection(table, EurosTableRow.Winners, eurosResult);
ProcessTableSection(table, EurosTableRow.PrizePerWinner, eurosResult);
ProcessTableSection(table, EurosTableRow.PrizeFundAmountUK, eurosResult);
ProcessTableSection(table, EurosTableRow.TotalWinners, eurosResult);
}
}
catch (Exception ex)
{
// ToDo: Catch Exception
Console.WriteLine(ex.Message);
}
}
public static void ProcessTableSection(HtmlNode table, string sectionTitle, EurosResult eurosResult)
{
try
{
var sectionRow = GetSectionRowByTitle(table, sectionTitle);
if (sectionRow != null)
{
var ukWinnersNode = GetNodeByDataTitle(sectionRow, EurosTableRow.Winners);
var prizePerWinnerNode = GetNodeByDataTitle(sectionRow, EurosTableRow.PrizePerWinner);
var prizeFundUKNode = GetNodeByDataTitle(sectionRow, EurosTableRow.PrizeFundAmountUK);
var totalWinnersNode = GetNodeByDataTitle(sectionRow, EurosTableRow.TotalWinners);
if (ukWinnersNode != null && prizePerWinnerNode != null && prizeFundUKNode != null)
{
ProcessUKWinners(sectionTitle, ukWinnersNode, eurosResult);
ProcessWinnerPrizeAmount(sectionTitle, prizePerWinnerNode, eurosResult);
ProcessPrizeFundAmountUK(sectionTitle, prizeFundUKNode, eurosResult);
ProcessTotalWinners(sectionTitle, totalWinnersNode, eurosResult);
}
else
{
// ToDo:Logger - Missing winners, prize per winner, or prize fund nodes in the table.
}
}
else
{
// ToDo: Logger - Section title '{sectionTitle}' not found in the table.
}
}
catch (Exception ex)
{
// ToDo: Logger - Exception
Console.WriteLine(ex.Message);
}
}
private static HtmlNode GetSectionRowByTitle(HtmlNode table, string sectionTitle)
{
try
{
var sectionRow = table.Descendants("tr")
.FirstOrDefault(x => x.Descendants("td").Any(y => y.InnerText.Contains(sectionTitle, StringComparison.OrdinalIgnoreCase)));
if (sectionRow != null)
{
return sectionRow;
}
else
{
throw new NullResultException("Machine name is null.");
}
}
catch (Exception ex)
{
// ToDo Logger
Console.WriteLine();
}
return null;
}
private static HtmlNode GetNodeByDataTitle(HtmlNode sectionRow, string dataTitle)
{
try
{
return sectionRow.Descendants("td")
.FirstOrDefault(x => x.Attributes["data-title"] != null && x.Attributes["data-title"].Value == dataTitle)
?? throw new InvalidOperationException($"Section row with title '{dataTitle}' not found.");
}
catch (Exception ex)
{
}
return null;
}
public static void ProcessUKWinners(string sectionTitle, HtmlNode winnersNode, EurosResult eurosResult)
{
try
{
if (int.TryParse(winnersNode.InnerText.Trim().Replace(",", ""), NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out int winners))
{
string columnTitle = winnersNode.Attributes["data-title"].Value;
ParseWinnerCount(sectionTitle, columnTitle, winners, eurosResult);
}
else
{
// ToDo: Logger - Failed to parse {sectionTitle} winners.
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
}
private static void ParseWinnerCount(string sectionTitle, string columnTitle, int winners, EurosResult eurosResult)
{
try
{
AssignValueToModelProperty(sectionTitle, EurosTableRow.Match2, columnTitle, winners, eurosResult);
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
}
public static void ProcessWinnerPrizeAmount(string sectionTitle, HtmlNode prizePerWinnerNode, EurosResult eurosResult)
{
try
{
string prizeText = prizePerWinnerNode.InnerText.Trim().Replace("£", "").Replace(",", "");
string columnTitle = prizePerWinnerNode.Attributes["data-title"].Value;
int prizePerWinner;
if (prizeText == "Free Ticket")
{
prizePerWinner = 999999999;
}
else if (int.TryParse(prizeText, NumberStyles.Currency, CultureInfo.InvariantCulture, out int parsedPrize))
{
prizePerWinner = parsedPrize;
}
else
{
// ToDo: Logger - Failed to parse prize per winner ({sectionTitle}).
return;
}
ParseWinnerPrizeAmount(sectionTitle, columnTitle, prizePerWinner, eurosResult);
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
}
public static void ParseWinnerPrizeAmount(string sectionTitle, string columnTitle, int prizePerWinner, EurosResult eurosResult)
{
try
{
AssignValueToModelProperty(sectionTitle, EurosTableRow.Match2, columnTitle, prizePerWinner, eurosResult);
}
catch
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
}
public static void ProcessPrizeFundAmountUK(string sectionTitle, HtmlNode prizeFundNode, EurosResult eurosResult)
{
try
{
string prizeFundText = prizeFundNode.InnerText.Trim().Replace("£", "").Replace(",", "");
string columnTitle = prizeFundNode.Attributes["data-title"].Value;
int prizeFund;
if (prizeFundText == "-")
{
prizeFund = 999999999;
}
else if (int.TryParse(prizeFundText, NumberStyles.Currency, CultureInfo.InvariantCulture, out int parsedPrizeFund))
{
prizeFund = parsedPrizeFund;
}
else
{
// ToDo: Logger
return;
}
ParsePrizeFundAmountUK(sectionTitle, columnTitle, prizeFund, eurosResult);
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
}
public static void ParsePrizeFundAmountUK(string sectionTitle, string columnTitle, int prizeFundNode, EurosResult eurosResult)
{
try
{
AssignValueToModelProperty(sectionTitle, EurosTableRow.Match2, columnTitle, prizeFundNode, eurosResult);
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
}
public static void ProcessTotalWinners(string sectionTitle, HtmlNode totalWinnersNode, EurosResult eurosResult)
{
try
{
if (int.TryParse(totalWinnersNode.InnerText.Trim().Replace(",", ""), NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out int totalWinners))
{
string columnTitle = totalWinnersNode.Attributes["data-title"].Value;
ParseTotalWinners(sectionTitle, columnTitle, totalWinners, eurosResult);
}
else
{
// ToDo: Logger - Failed to parse {sectionTitle} winners.
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
}
public static void ParseTotalWinners(string sectionTitle, string columnTitle, int totalWinnersNode, EurosResult eurosResult)
{
try
{
AssignValueToModelProperty(sectionTitle, EurosTableRow.Match2, columnTitle, totalWinnersNode, eurosResult);
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
}
public static void AssignValueToModelProperty(string sectionTitle, string propertyName, string columnTitle, int value, EurosResult eurosResult)
{
switch (sectionTitle)
{
#region Match 1 Plus 2 Stars
case EurosTableRow.Match1Plus2Stars:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched1Plus2StarsUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched1Plus2StarsPrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched1Plus2StarsPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched1Plus2Stars), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine(ex);
}
break;
#endregion
#region Match 2, Match 2 Plus 1 Star & Match 2 Plus 2 Stars
case EurosTableRow.Match2:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched2UK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched2PrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched2PrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched2), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine(ex);
}
break;
case EurosTableRow.Match2Plus1Star:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched2Plus1StarUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched2Plus1StarPrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched2Plus1StarPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched2Plus1Star), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
break;
case EurosTableRow.Match2Plus2Stars:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched2Plus2StarsUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched2Plus2StarsPrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched2Plus2StarsPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched2Plus2Stars), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
break;
#endregion
#region Match 3, Match 3 Plus 1 Star & Match 3 Plus 2 Stars
case EurosTableRow.Match3:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched3UK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched3PrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched3PrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched3), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
break;
case EurosTableRow.Match3Plus1Star:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched3Plus1StarUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched3Plus1StarPrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched3Plus1StarPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched3Plus1Star), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
break;
case EurosTableRow.Match3Plus2Stars:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched3Plus2StarsUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched3Plus2StarsPrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched3Plus2StarsPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched3Plus2Stars), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
break;
#endregion
#region Match 4, Match 4 Plus 1 Star & Match 4 Plus 2 Stars
case EurosTableRow.Match4:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched4UK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched4PrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched4PrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched4), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
break;
case EurosTableRow.Match4Plus1Star:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched4Plus1StarUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched4Plus1StarPrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched4Plus1StarPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched4Plus1Star), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
break;
case EurosTableRow.Match4Plus2Stars:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched4Plus2StarsUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched4Plus2StarsPrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched4Plus2StarsPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched4Plus2Stars), eurosResult, value);
}
}
catch (Exception ex)
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
break;
#endregion
#region Match 5, Match 5 Plus 1 Star & Match 5 Plus 2 Stars
case EurosTableRow.Match5:
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched5UK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched5PrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched5PrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched5), eurosResult, value);
}
break;
case EurosTableRow.Match5Plus1Star:
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched5Plus1StarUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched5Plus1StarPrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched5Plus1StarPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched5Plus1Star), eurosResult, value);
}
break;
case EurosTableRow.Match5Plus2Stars:
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched5Plus2StarsUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizePerWinner)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched5Plus2StarsPrizeUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.Matched5Plus2StarsPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.TotalWinners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalMatched5Plus2Stars), eurosResult, value);
}
break;
#endregion
#region Totals
case EurosTableRow.Total:
try
{
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalWinnersUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.PrizeFundAmountUK)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalPrizeFundUK), eurosResult, value);
}
if (columnTitle == EurosTableRow.Winners)
{
PropertyManager.SetProperty(nameof(eurosResult.TotalWinners), eurosResult, value);
}
}
catch
{
// ToDo: Logger
Console.WriteLine("Nothing to match");
}
break;
#endregion
}
}
}
}