| | | 1 | | using AngleSharp; |
| | | 2 | | using AngleSharp.Dom; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace Orchestrator.Commands.Utility.Snapshots; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A simple HTTP client for fetching HTML snapshots from Kicktipp. |
| | | 9 | | /// This client is specifically for snapshot generation and does not parse the HTML. |
| | | 10 | | /// </summary> |
| | | 11 | | public class SnapshotClient |
| | | 12 | | { |
| | | 13 | | private readonly HttpClient _httpClient; |
| | | 14 | | private readonly ILogger _logger; |
| | | 15 | | private readonly IBrowsingContext _browsingContext; |
| | | 16 | | |
| | 0 | 17 | | public SnapshotClient(HttpClient httpClient, ILogger logger) |
| | | 18 | | { |
| | 0 | 19 | | _httpClient = httpClient; |
| | 0 | 20 | | _logger = logger; |
| | 0 | 21 | | var config = Configuration.Default.WithDefaultLoader(); |
| | 0 | 22 | | _browsingContext = BrowsingContext.New(config); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Fetches the login page. |
| | | 27 | | /// Note: This is fetched without authentication to capture the login form structure. |
| | | 28 | | /// </summary> |
| | | 29 | | public async Task<string?> FetchLoginPageAsync() |
| | | 30 | | { |
| | 0 | 31 | | var url = "info/profil/login"; |
| | 0 | 32 | | return await FetchPageAsync(url, "login"); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Fetches the standings page (tabellen). |
| | | 37 | | /// </summary> |
| | | 38 | | public async Task<string?> FetchStandingsPageAsync(string community) |
| | | 39 | | { |
| | 0 | 40 | | var url = $"{community}/tabellen"; |
| | 0 | 41 | | return await FetchPageAsync(url, "tabellen"); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Fetches the main betting page (tippabgabe). |
| | | 46 | | /// </summary> |
| | | 47 | | public async Task<string?> FetchTippabgabePageAsync(string community) |
| | | 48 | | { |
| | 0 | 49 | | var url = $"{community}/tippabgabe"; |
| | 0 | 50 | | return await FetchPageAsync(url, "tippabgabe"); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Fetches the bonus questions page (tippabgabe?bonus=true). |
| | | 55 | | /// </summary> |
| | | 56 | | public async Task<string?> FetchBonusPageAsync(string community) |
| | | 57 | | { |
| | 0 | 58 | | var url = $"{community}/tippabgabe?bonus=true"; |
| | 0 | 59 | | return await FetchPageAsync(url, "tippabgabe-bonus"); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Fetches all spielinfo pages (default view) by traversing through them. |
| | | 64 | | /// Returns a list of (fileName, content) tuples. |
| | | 65 | | /// </summary> |
| | | 66 | | public async Task<List<(string fileName, string content)>> FetchAllSpielinfoAsync(string community) |
| | | 67 | | { |
| | 0 | 68 | | return await FetchAllSpielinfoVariantAsync(community, fileNameSuffix: null, ansichtParam: null); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Fetches all spielinfo pages with home/away history (ansicht=2) by traversing through them. |
| | | 73 | | /// Returns a list of (fileName, content) tuples with "-homeaway" suffix. |
| | | 74 | | /// </summary> |
| | | 75 | | public async Task<List<(string fileName, string content)>> FetchAllSpielinfoHomeAwayAsync(string community) |
| | | 76 | | { |
| | 0 | 77 | | return await FetchAllSpielinfoVariantAsync(community, fileNameSuffix: "-homeaway", ansichtParam: "2"); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Fetches all spielinfo pages with head-to-head history (ansicht=3) by traversing through them. |
| | | 82 | | /// Returns a list of (fileName, content) tuples with "-h2h" suffix. |
| | | 83 | | /// </summary> |
| | | 84 | | public async Task<List<(string fileName, string content)>> FetchAllSpielinfoHeadToHeadAsync(string community) |
| | | 85 | | { |
| | 0 | 86 | | return await FetchAllSpielinfoVariantAsync(community, fileNameSuffix: "-h2h", ansichtParam: "3"); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Fetches all spielinfo pages with an optional ansicht parameter. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="community">The community name.</param> |
| | | 93 | | /// <param name="fileNameSuffix">Optional suffix to append to file names (e.g., "-homeaway", "-h2h").</param> |
| | | 94 | | /// <param name="ansichtParam">Optional ansicht parameter value (e.g., "2" for home/away, "3" for head-to-head).</pa |
| | | 95 | | private async Task<List<(string fileName, string content)>> FetchAllSpielinfoVariantAsync( |
| | | 96 | | string community, string? fileNameSuffix, string? ansichtParam) |
| | | 97 | | { |
| | 0 | 98 | | var results = new List<(string fileName, string content)>(); |
| | | 99 | | |
| | | 100 | | // First, get the tippabgabe page to find the link to spielinfos |
| | 0 | 101 | | var tippabgabeUrl = $"{community}/tippabgabe"; |
| | 0 | 102 | | var response = await _httpClient.GetAsync(tippabgabeUrl); |
| | | 103 | | |
| | 0 | 104 | | if (!response.IsSuccessStatusCode) |
| | | 105 | | { |
| | 0 | 106 | | _logger.LogError("Failed to fetch tippabgabe page. Status: {StatusCode}", response.StatusCode); |
| | 0 | 107 | | return results; |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | var content = await response.Content.ReadAsStringAsync(); |
| | 0 | 111 | | var document = await _browsingContext.OpenAsync(req => req.Content(content)); |
| | | 112 | | |
| | | 113 | | // Find the "Tippabgabe mit Spielinfos" link |
| | 0 | 114 | | var spielinfoLink = document.QuerySelector("a[href*='spielinfo']"); |
| | 0 | 115 | | if (spielinfoLink == null) |
| | | 116 | | { |
| | 0 | 117 | | _logger.LogWarning("Could not find Spielinfo link on tippabgabe page"); |
| | 0 | 118 | | return results; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | var spielinfoUrl = spielinfoLink.GetAttribute("href"); |
| | 0 | 122 | | if (string.IsNullOrEmpty(spielinfoUrl)) |
| | | 123 | | { |
| | 0 | 124 | | _logger.LogWarning("Spielinfo link has no href attribute"); |
| | 0 | 125 | | return results; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | // Make URL absolute if it's relative |
| | 0 | 129 | | if (spielinfoUrl.StartsWith("/")) |
| | | 130 | | { |
| | 0 | 131 | | spielinfoUrl = spielinfoUrl.Substring(1); |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | var variantDescription = ansichtParam != null ? $" (ansicht={ansichtParam})" : ""; |
| | 0 | 135 | | _logger.LogInformation("Starting to fetch spielinfo pages{Variant}...", variantDescription); |
| | | 136 | | |
| | | 137 | | // Navigate through all matches using the right arrow navigation |
| | 0 | 138 | | var currentUrl = spielinfoUrl; |
| | 0 | 139 | | var matchCount = 0; |
| | | 140 | | |
| | 0 | 141 | | while (!string.IsNullOrEmpty(currentUrl)) |
| | | 142 | | { |
| | | 143 | | try |
| | | 144 | | { |
| | | 145 | | // Apply ansicht parameter if specified |
| | 0 | 146 | | var fetchUrl = ApplyAnsichtParam(currentUrl, ansichtParam); |
| | | 147 | | |
| | 0 | 148 | | var spielinfoResponse = await _httpClient.GetAsync(fetchUrl); |
| | 0 | 149 | | if (!spielinfoResponse.IsSuccessStatusCode) |
| | | 150 | | { |
| | 0 | 151 | | _logger.LogWarning("Failed to fetch spielinfo page: {Url}. Status: {StatusCode}", |
| | 0 | 152 | | fetchUrl, spielinfoResponse.StatusCode); |
| | 0 | 153 | | break; |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | var spielinfoContent = await spielinfoResponse.Content.ReadAsStringAsync(); |
| | 0 | 157 | | matchCount++; |
| | | 158 | | |
| | | 159 | | // Generate filename from URL or index, with optional suffix |
| | 0 | 160 | | var fileName = $"spielinfo-{matchCount:D2}{fileNameSuffix ?? ""}"; |
| | 0 | 161 | | results.Add((fileName, spielinfoContent)); |
| | 0 | 162 | | _logger.LogDebug("Fetched spielinfo page {Count}: {Url}", matchCount, fetchUrl); |
| | | 163 | | |
| | | 164 | | // Parse to find next link (use the content without ansicht param for navigation) |
| | 0 | 165 | | var spielinfoDocument = await _browsingContext.OpenAsync(req => req.Content(spielinfoContent)); |
| | 0 | 166 | | var nextLink = FindNextMatchLink(spielinfoDocument); |
| | | 167 | | |
| | 0 | 168 | | if (nextLink != null) |
| | | 169 | | { |
| | 0 | 170 | | currentUrl = nextLink; |
| | 0 | 171 | | if (currentUrl.StartsWith("/")) |
| | | 172 | | { |
| | 0 | 173 | | currentUrl = currentUrl.Substring(1); |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | else |
| | | 177 | | { |
| | | 178 | | // No more matches |
| | 0 | 179 | | break; |
| | | 180 | | } |
| | 0 | 181 | | } |
| | 0 | 182 | | catch (Exception ex) |
| | | 183 | | { |
| | 0 | 184 | | _logger.LogError(ex, "Error fetching spielinfo page: {Url}", currentUrl); |
| | 0 | 185 | | break; |
| | | 186 | | } |
| | | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | _logger.LogInformation("Fetched {Count} spielinfo pages{Variant}", results.Count, variantDescription); |
| | 0 | 190 | | return results; |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Applies the ansicht query parameter to a URL. |
| | | 195 | | /// </summary> |
| | | 196 | | private static string ApplyAnsichtParam(string url, string? ansichtParam) |
| | | 197 | | { |
| | 0 | 198 | | if (string.IsNullOrEmpty(ansichtParam)) |
| | | 199 | | { |
| | 0 | 200 | | return url; |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | return url.Contains('?') |
| | 0 | 204 | | ? $"{url}&ansicht={ansichtParam}" |
| | 0 | 205 | | : $"{url}?ansicht={ansichtParam}"; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | private async Task<string?> FetchPageAsync(string url, string pageName) |
| | | 209 | | { |
| | | 210 | | try |
| | | 211 | | { |
| | 0 | 212 | | _logger.LogDebug("Fetching {PageName} from {Url}", pageName, url); |
| | 0 | 213 | | var response = await _httpClient.GetAsync(url); |
| | | 214 | | |
| | 0 | 215 | | if (!response.IsSuccessStatusCode) |
| | | 216 | | { |
| | 0 | 217 | | _logger.LogError("Failed to fetch {PageName}. Status: {StatusCode}", pageName, response.StatusCode); |
| | 0 | 218 | | return null; |
| | | 219 | | } |
| | | 220 | | |
| | 0 | 221 | | var content = await response.Content.ReadAsStringAsync(); |
| | 0 | 222 | | _logger.LogDebug("Successfully fetched {PageName} ({Length} bytes)", pageName, content.Length); |
| | 0 | 223 | | return content; |
| | | 224 | | } |
| | 0 | 225 | | catch (Exception ex) |
| | | 226 | | { |
| | 0 | 227 | | _logger.LogError(ex, "Exception fetching {PageName}", pageName); |
| | 0 | 228 | | return null; |
| | | 229 | | } |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Finds the next match link (right arrow navigation) on a spielinfo page. |
| | | 234 | | /// This mirrors the logic in KicktippClient.FindNextMatchLink. |
| | | 235 | | /// </summary> |
| | | 236 | | private string? FindNextMatchLink(IDocument document) |
| | | 237 | | { |
| | | 238 | | try |
| | | 239 | | { |
| | | 240 | | // Look for the right arrow button in the match navigation |
| | 0 | 241 | | var nextButton = document.QuerySelector(".prevnextNext a"); |
| | 0 | 242 | | if (nextButton == null) |
| | | 243 | | { |
| | 0 | 244 | | _logger.LogDebug("No next match button found"); |
| | 0 | 245 | | return null; |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | // Check if the button is disabled |
| | 0 | 249 | | var parentDiv = nextButton.ParentElement; |
| | 0 | 250 | | if (parentDiv?.ClassList.Contains("disabled") == true) |
| | | 251 | | { |
| | 0 | 252 | | _logger.LogDebug("Next match button is disabled - reached end of matches"); |
| | 0 | 253 | | return null; |
| | | 254 | | } |
| | | 255 | | |
| | 0 | 256 | | var href = nextButton.GetAttribute("href"); |
| | 0 | 257 | | if (string.IsNullOrEmpty(href)) |
| | | 258 | | { |
| | 0 | 259 | | _logger.LogDebug("Next match button has no href"); |
| | 0 | 260 | | return null; |
| | | 261 | | } |
| | | 262 | | |
| | 0 | 263 | | return href; |
| | | 264 | | } |
| | 0 | 265 | | catch (Exception ex) |
| | | 266 | | { |
| | 0 | 267 | | _logger.LogDebug(ex, "Error finding next match link"); |
| | 0 | 268 | | return null; |
| | | 269 | | } |
| | 0 | 270 | | } |
| | | 271 | | } |