| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Spectre.Console; |
| | | 3 | | using Spectre.Console.Cli; |
| | | 4 | | using Orchestrator.Infrastructure.Factories; |
| | | 5 | | |
| | | 6 | | namespace Orchestrator.Commands.Utility.Snapshots; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Command for fetching HTML snapshots from Kicktipp. |
| | | 10 | | /// </summary> |
| | | 11 | | public class SnapshotsFetchCommand : AsyncCommand<SnapshotsFetchSettings> |
| | | 12 | | { |
| | | 13 | | private readonly IAnsiConsole _console; |
| | | 14 | | private readonly IKicktippClientFactory _kicktippClientFactory; |
| | | 15 | | private readonly ILogger<SnapshotsFetchCommand> _logger; |
| | | 16 | | |
| | 0 | 17 | | public SnapshotsFetchCommand( |
| | 0 | 18 | | IAnsiConsole console, |
| | 0 | 19 | | IKicktippClientFactory kicktippClientFactory, |
| | 0 | 20 | | ILogger<SnapshotsFetchCommand> logger) |
| | | 21 | | { |
| | 0 | 22 | | _console = console; |
| | 0 | 23 | | _kicktippClientFactory = kicktippClientFactory; |
| | 0 | 24 | | _logger = logger; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public override async Task<int> ExecuteAsync(CommandContext context, SnapshotsFetchSettings settings) |
| | | 28 | | { |
| | | 29 | | |
| | | 30 | | try |
| | | 31 | | { |
| | | 32 | | // Validate settings |
| | 0 | 33 | | if (string.IsNullOrWhiteSpace(settings.Community)) |
| | | 34 | | { |
| | 0 | 35 | | _console.MarkupLine("[red]Error: Community is required[/]"); |
| | 0 | 36 | | return 1; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | _console.MarkupLine("[green]Fetching snapshots...[/]"); |
| | 0 | 40 | | _console.MarkupLine($"[blue]Community:[/] [yellow]{settings.Community}[/]"); |
| | 0 | 41 | | _console.MarkupLine($"[blue]Output directory:[/] [yellow]{settings.OutputDirectory}[/]"); |
| | | 42 | | |
| | | 43 | | // Create output directory |
| | 0 | 44 | | var outputPath = Path.GetFullPath(settings.OutputDirectory); |
| | 0 | 45 | | Directory.CreateDirectory(outputPath); |
| | | 46 | | |
| | | 47 | | // Warn if not gitignored |
| | 0 | 48 | | WarnIfNotGitignored(_console, outputPath); |
| | | 49 | | |
| | | 50 | | // Create snapshot client using factory (factory handles env var loading) |
| | 0 | 51 | | var snapshotClient = _kicktippClientFactory.CreateSnapshotClient(); |
| | | 52 | | |
| | 0 | 53 | | var savedCount = await FetchSnapshotsAsync(_console, snapshotClient, settings.Community, outputPath); |
| | | 54 | | |
| | 0 | 55 | | _console.WriteLine(); |
| | 0 | 56 | | _console.MarkupLine($"[green]Done![/] Saved {savedCount} snapshot(s) to [yellow]{outputPath}[/]"); |
| | 0 | 57 | | _console.WriteLine(); |
| | 0 | 58 | | _console.MarkupLine("[dim]Next step: Run 'snapshots encrypt' to encrypt them for committing[/]"); |
| | | 59 | | |
| | 0 | 60 | | return 0; |
| | | 61 | | } |
| | 0 | 62 | | catch (Exception ex) |
| | | 63 | | { |
| | 0 | 64 | | _logger.LogError(ex, "Error fetching snapshots"); |
| | 0 | 65 | | _console.MarkupLine($"[red]Error:[/] {ex.Message}"); |
| | 0 | 66 | | return 1; |
| | | 67 | | } |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | internal static async Task<int> FetchSnapshotsAsync(IAnsiConsole console, SnapshotClient snapshotClient, string comm |
| | | 71 | | { |
| | 0 | 72 | | var savedCount = 0; |
| | | 73 | | |
| | 0 | 74 | | await console.Status() |
| | 0 | 75 | | .StartAsync("Fetching snapshots...", async ctx => |
| | 0 | 76 | | { |
| | 0 | 77 | | // 0. Login page (fetched without community context) |
| | 0 | 78 | | ctx.Status("Fetching login page..."); |
| | 0 | 79 | | var loginContent = await snapshotClient.FetchLoginPageAsync(); |
| | 0 | 80 | | if (loginContent != null) |
| | 0 | 81 | | { |
| | 0 | 82 | | await SaveSnapshotAsync(outputPath, "login.html", loginContent); |
| | 0 | 83 | | savedCount++; |
| | 0 | 84 | | console.MarkupLine("[green]✓[/] Saved login.html"); |
| | 0 | 85 | | } |
| | 0 | 86 | | else |
| | 0 | 87 | | { |
| | 0 | 88 | | console.MarkupLine("[red]✗[/] Failed to fetch login page"); |
| | 0 | 89 | | } |
| | 0 | 90 | | |
| | 0 | 91 | | // 1. Tabellen (standings) |
| | 0 | 92 | | ctx.Status("Fetching tabellen..."); |
| | 0 | 93 | | var tabellenContent = await snapshotClient.FetchStandingsPageAsync(community); |
| | 0 | 94 | | if (tabellenContent != null) |
| | 0 | 95 | | { |
| | 0 | 96 | | await SaveSnapshotAsync(outputPath, "tabellen.html", tabellenContent); |
| | 0 | 97 | | savedCount++; |
| | 0 | 98 | | console.MarkupLine("[green]✓[/] Saved tabellen.html"); |
| | 0 | 99 | | } |
| | 0 | 100 | | else |
| | 0 | 101 | | { |
| | 0 | 102 | | console.MarkupLine("[red]✗[/] Failed to fetch tabellen"); |
| | 0 | 103 | | } |
| | 0 | 104 | | |
| | 0 | 105 | | // 2. Tippabgabe (main betting page) |
| | 0 | 106 | | ctx.Status("Fetching tippabgabe..."); |
| | 0 | 107 | | var tippabgabeContent = await snapshotClient.FetchTippabgabePageAsync(community); |
| | 0 | 108 | | if (tippabgabeContent != null) |
| | 0 | 109 | | { |
| | 0 | 110 | | await SaveSnapshotAsync(outputPath, "tippabgabe.html", tippabgabeContent); |
| | 0 | 111 | | savedCount++; |
| | 0 | 112 | | console.MarkupLine("[green]✓[/] Saved tippabgabe.html"); |
| | 0 | 113 | | } |
| | 0 | 114 | | else |
| | 0 | 115 | | { |
| | 0 | 116 | | console.MarkupLine("[red]✗[/] Failed to fetch tippabgabe"); |
| | 0 | 117 | | } |
| | 0 | 118 | | |
| | 0 | 119 | | // 3. Tippabgabe bonus (bonus questions) |
| | 0 | 120 | | ctx.Status("Fetching tippabgabe-bonus..."); |
| | 0 | 121 | | var bonusContent = await snapshotClient.FetchBonusPageAsync(community); |
| | 0 | 122 | | if (bonusContent != null) |
| | 0 | 123 | | { |
| | 0 | 124 | | await SaveSnapshotAsync(outputPath, "tippabgabe-bonus.html", bonusContent); |
| | 0 | 125 | | savedCount++; |
| | 0 | 126 | | console.MarkupLine("[green]✓[/] Saved tippabgabe-bonus.html"); |
| | 0 | 127 | | } |
| | 0 | 128 | | else |
| | 0 | 129 | | { |
| | 0 | 130 | | console.MarkupLine("[red]✗[/] Failed to fetch tippabgabe-bonus"); |
| | 0 | 131 | | } |
| | 0 | 132 | | |
| | 0 | 133 | | // 4. Spielinfo pages (match details with history) |
| | 0 | 134 | | ctx.Status("Fetching spielinfo pages..."); |
| | 0 | 135 | | var spielinfoPages = await snapshotClient.FetchAllSpielinfoAsync(community); |
| | 0 | 136 | | |
| | 0 | 137 | | foreach (var (fileName, content) in spielinfoPages) |
| | 0 | 138 | | { |
| | 0 | 139 | | await SaveSnapshotAsync(outputPath, $"{fileName}.html", content); |
| | 0 | 140 | | savedCount++; |
| | 0 | 141 | | } |
| | 0 | 142 | | |
| | 0 | 143 | | if (spielinfoPages.Count > 0) |
| | 0 | 144 | | { |
| | 0 | 145 | | console.MarkupLine($"[green]✓[/] Saved {spielinfoPages.Count} spielinfo pages"); |
| | 0 | 146 | | } |
| | 0 | 147 | | else |
| | 0 | 148 | | { |
| | 0 | 149 | | console.MarkupLine("[yellow]![/] No spielinfo pages found"); |
| | 0 | 150 | | } |
| | 0 | 151 | | |
| | 0 | 152 | | // 5. Spielinfo pages with home/away history (ansicht=2) |
| | 0 | 153 | | ctx.Status("Fetching spielinfo home/away pages..."); |
| | 0 | 154 | | var homeAwayPages = await snapshotClient.FetchAllSpielinfoHomeAwayAsync(community); |
| | 0 | 155 | | |
| | 0 | 156 | | foreach (var (fileName, content) in homeAwayPages) |
| | 0 | 157 | | { |
| | 0 | 158 | | await SaveSnapshotAsync(outputPath, $"{fileName}.html", content); |
| | 0 | 159 | | savedCount++; |
| | 0 | 160 | | } |
| | 0 | 161 | | |
| | 0 | 162 | | if (homeAwayPages.Count > 0) |
| | 0 | 163 | | { |
| | 0 | 164 | | console.MarkupLine($"[green]✓[/] Saved {homeAwayPages.Count} spielinfo home/away pages"); |
| | 0 | 165 | | } |
| | 0 | 166 | | else |
| | 0 | 167 | | { |
| | 0 | 168 | | console.MarkupLine("[yellow]![/] No spielinfo home/away pages found"); |
| | 0 | 169 | | } |
| | 0 | 170 | | |
| | 0 | 171 | | // 6. Spielinfo pages with head-to-head history (ansicht=3) |
| | 0 | 172 | | ctx.Status("Fetching spielinfo head-to-head pages..."); |
| | 0 | 173 | | var h2hPages = await snapshotClient.FetchAllSpielinfoHeadToHeadAsync(community); |
| | 0 | 174 | | |
| | 0 | 175 | | foreach (var (fileName, content) in h2hPages) |
| | 0 | 176 | | { |
| | 0 | 177 | | await SaveSnapshotAsync(outputPath, $"{fileName}.html", content); |
| | 0 | 178 | | savedCount++; |
| | 0 | 179 | | } |
| | 0 | 180 | | |
| | 0 | 181 | | if (h2hPages.Count > 0) |
| | 0 | 182 | | { |
| | 0 | 183 | | console.MarkupLine($"[green]✓[/] Saved {h2hPages.Count} spielinfo head-to-head pages"); |
| | 0 | 184 | | } |
| | 0 | 185 | | else |
| | 0 | 186 | | { |
| | 0 | 187 | | console.MarkupLine("[yellow]![/] No spielinfo head-to-head pages found"); |
| | 0 | 188 | | } |
| | 0 | 189 | | }); |
| | | 190 | | |
| | 0 | 191 | | return savedCount; |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | internal static async Task SaveSnapshotAsync(string outputPath, string fileName, string content) |
| | | 195 | | { |
| | 0 | 196 | | var filePath = Path.Combine(outputPath, fileName); |
| | 0 | 197 | | await File.WriteAllTextAsync(filePath, content); |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | private static void WarnIfNotGitignored(IAnsiConsole console, string outputPath) |
| | | 201 | | { |
| | 0 | 202 | | var relativePath = Path.GetRelativePath(Directory.GetCurrentDirectory(), outputPath); |
| | 0 | 203 | | var directoryName = Path.GetFileName(outputPath.TrimEnd(Path.DirectorySeparatorChar)); |
| | | 204 | | |
| | 0 | 205 | | var commonIgnoredPatterns = new[] { "snapshots", "temp", "tmp", "output", "out", ".local" }; |
| | 0 | 206 | | var looksIgnored = commonIgnoredPatterns.Any(p => |
| | 0 | 207 | | directoryName.Contains(p, StringComparison.OrdinalIgnoreCase)); |
| | | 208 | | |
| | 0 | 209 | | if (!looksIgnored) |
| | | 210 | | { |
| | 0 | 211 | | console.MarkupLine( |
| | 0 | 212 | | $"[yellow]⚠ Warning:[/] Output directory '{relativePath}' may not be gitignored."); |
| | 0 | 213 | | console.MarkupLine( |
| | 0 | 214 | | "[yellow] Make sure to add it to .gitignore before committing![/]"); |
| | 0 | 215 | | console.WriteLine(); |
| | | 216 | | } |
| | 0 | 217 | | } |
| | | 218 | | } |