using System.Net; using System.Net.Http.Json; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; using SatelliteProvider.Common.Configs; using SatelliteProvider.Common.DTO; using SatelliteProvider.Common.Utils; namespace SatelliteProvider.Services; public record DownloadedTileInfoV2(int X, int Y, int ZoomLevel, double CenterLatitude, double CenterLongitude, string FilePath, double TileSizeMeters); public class RateLimitException : Exception { public RateLimitException(string message) : base(message) { } } public class GoogleMapsDownloaderV2 { private const string TILE_URL_TEMPLATE = "https://mt{0}.google.com/vt/lyrs=s&x={1}&y={2}&z={3}&token={4}"; private const string USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36"; private const int TILE_SIZE_PIXELS = 256; private const int MAX_RETRY_DELAY_SECONDS = 30; private const int BASE_RETRY_DELAY_SECONDS = 1; private static readonly int[] ALLOWED_ZOOM_LEVELS = { 15, 16, 17, 18, 19 }; private readonly ILogger _logger; private readonly string _apiKey; private readonly string _tilesDirectory; private readonly IHttpClientFactory _httpClientFactory; public GoogleMapsDownloaderV2( ILogger logger, IOptions mapConfig, IOptions storageConfig, IHttpClientFactory httpClientFactory) { _logger = logger; _apiKey = mapConfig.Value.ApiKey; _tilesDirectory = storageConfig.Value.TilesDirectory; _httpClientFactory = httpClientFactory; } private record SessionResponse(string Session); private async Task GetSessionToken() { var url = $"https://tile.googleapis.com/v1/createSession?key={_apiKey}"; using var httpClient = _httpClientFactory.CreateClient(); try { var str = JsonConvert.SerializeObject(new { mapType = "satellite" }); var response = await httpClient.PostAsync(url, new StringContent(str)); response.EnsureSuccessStatusCode(); var sessionResponse = await response.Content.ReadFromJsonAsync(); return sessionResponse?.Session; } catch (Exception e) { _logger.LogError(e, "Failed to get session token"); throw; } } public async Task DownloadSingleTileAsync(double latitude, double longitude, int zoomLevel, CancellationToken token = default) { if (!ALLOWED_ZOOM_LEVELS.Contains(zoomLevel)) { throw new ArgumentException($"Zoom level {zoomLevel} is not allowed. Allowed zoom levels are: {string.Join(", ", ALLOWED_ZOOM_LEVELS)}", nameof(zoomLevel)); } var geoPoint = new GeoPoint(latitude, longitude); var (tileX, tileY) = GeoUtils.WorldToTilePos(geoPoint, zoomLevel); _logger.LogInformation("Downloading single tile at ({Lat}, {Lon}), zoom {Zoom}, tile coordinates ({X}, {Y})", latitude, longitude, zoomLevel, tileX, tileY); var sessionToken = await GetSessionToken(); var server = 0; var url = string.Format(TILE_URL_TEMPLATE, server, tileX, tileY, zoomLevel, sessionToken); Directory.CreateDirectory(_tilesDirectory); var timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss"); var fileName = $"tile_{zoomLevel}_{tileX}_{tileY}_{timestamp}.jpg"; var filePath = Path.Combine(_tilesDirectory, fileName); var imageBytes = await ExecuteWithRetryAsync(async () => { using var httpClient = _httpClientFactory.CreateClient(); httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(USER_AGENT); var response = await httpClient.GetAsync(url, token); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsByteArrayAsync(token); }, cancellationToken: token); await File.WriteAllBytesAsync(filePath, imageBytes, token); var tileCenter = GeoUtils.TileToWorldPos(tileX, tileY, zoomLevel); var tileSizeMeters = CalculateTileSizeInMeters(zoomLevel, tileCenter.Lat); _logger.LogInformation("Downloaded tile to {FilePath}, size: {Size:F2} meters", filePath, tileSizeMeters); return new DownloadedTileInfoV2( tileX, tileY, zoomLevel, tileCenter.Lat, tileCenter.Lon, filePath, tileSizeMeters ); } private static double CalculateTileSizeInMeters(int zoomLevel, double latitude) { const double EARTH_CIRCUMFERENCE_METERS = 40075016.686; var latRad = latitude * Math.PI / 180.0; var metersPerPixel = (EARTH_CIRCUMFERENCE_METERS * Math.Cos(latRad)) / (Math.Pow(2, zoomLevel) * TILE_SIZE_PIXELS); return metersPerPixel * TILE_SIZE_PIXELS; } private async Task ExecuteWithRetryAsync(Func> action, int maxRetries = 5, CancellationToken cancellationToken = default) { int attempt = 0; int delay = BASE_RETRY_DELAY_SECONDS; while (attempt < maxRetries) { try { return await action(); } catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.TooManyRequests || ex.StatusCode == (HttpStatusCode)429) { attempt++; if (attempt >= maxRetries) { _logger.LogError("Rate limit exceeded after {Attempts} attempts", maxRetries); throw new RateLimitException($"Rate limit exceeded after {maxRetries} attempts"); } delay = Math.Min(delay * 2, MAX_RETRY_DELAY_SECONDS); _logger.LogWarning("Rate limited. Waiting {Delay}s before retry {Attempt}/{Max}", delay, attempt, maxRetries); await Task.Delay(TimeSpan.FromSeconds(delay), cancellationToken); } catch (HttpRequestException ex) when (ex.StatusCode >= HttpStatusCode.InternalServerError) { attempt++; if (attempt >= maxRetries) { _logger.LogError(ex, "Server error after {Attempts} attempts", maxRetries); throw; } delay = Math.Min(delay * 2, MAX_RETRY_DELAY_SECONDS); _logger.LogWarning("Server error. Waiting {Delay}s before retry {Attempt}/{Max}", delay, attempt, maxRetries); await Task.Delay(TimeSpan.FromSeconds(delay), cancellationToken); } } throw new InvalidOperationException("Retry logic failed unexpectedly"); } public async Task> GetTilesWithMetadataAsync( GeoPoint centerGeoPoint, double radiusM, int zoomLevel, IEnumerable existingTiles, CancellationToken token = default) { if (!ALLOWED_ZOOM_LEVELS.Contains(zoomLevel)) { throw new ArgumentException($"Zoom level {zoomLevel} is not allowed. Allowed zoom levels are: {string.Join(", ", ALLOWED_ZOOM_LEVELS)}", nameof(zoomLevel)); } var (latMin, latMax, lonMin, lonMax) = GeoUtils.GetBoundingBox(centerGeoPoint, radiusM); var (xMin, yMin) = GeoUtils.WorldToTilePos(new GeoPoint(latMax, lonMin), zoomLevel); var (xMax, yMax) = GeoUtils.WorldToTilePos(new GeoPoint(latMin, lonMax), zoomLevel); _logger.LogInformation("Checking tiles for region: center=({Lat}, {Lon}), radius={Radius}m, zoom={Zoom}", centerGeoPoint.Lat, centerGeoPoint.Lon, radiusM, zoomLevel); _logger.LogInformation("Tile range: X=[{XMin}, {XMax}], Y=[{YMin}, {YMax}]", xMin, xMax, yMin, yMax); var downloadedTiles = new List(); int skippedCount = 0; for (var y = yMin; y <= yMax; y++) { for (var x = xMin; x <= xMax; x++) { token.ThrowIfCancellationRequested(); var tileCenter = GeoUtils.TileToWorldPos(x, y, zoomLevel); var existingTile = existingTiles.FirstOrDefault(t => Math.Abs(t.Latitude - tileCenter.Lat) < 0.0001 && Math.Abs(t.Longitude - tileCenter.Lon) < 0.0001 && t.ZoomLevel == zoomLevel); if (existingTile != null) { skippedCount++; _logger.LogInformation("Skipping tile ({X}, {Y}) - already exists at {FilePath}", x, y, existingTile.FilePath); continue; } var tileSizeMeters = CalculateTileSizeInMeters(zoomLevel, tileCenter.Lat); try { var sessionToken = await GetSessionToken(); var server = (x + y) % 4; var url = string.Format(TILE_URL_TEMPLATE, server, x, y, zoomLevel, sessionToken); Directory.CreateDirectory(_tilesDirectory); var timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss"); var fileName = $"tile_{zoomLevel}_{x}_{y}_{timestamp}.jpg"; var filePath = Path.Combine(_tilesDirectory, fileName); var imageBytes = await ExecuteWithRetryAsync(async () => { using var httpClient = _httpClientFactory.CreateClient(); httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(USER_AGENT); var response = await httpClient.GetAsync(url, token); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsByteArrayAsync(token); }, cancellationToken: token); await File.WriteAllBytesAsync(filePath, imageBytes, token); _logger.LogInformation("Downloaded tile ({X}, {Y}) to {FilePath}, center=({Lat:F6}, {Lon:F6}), size={Size:F2}m", x, y, filePath, tileCenter.Lat, tileCenter.Lon, tileSizeMeters); downloadedTiles.Add(new DownloadedTileInfoV2( x, y, zoomLevel, tileCenter.Lat, tileCenter.Lon, filePath, tileSizeMeters)); } catch (RateLimitException ex) { _logger.LogError(ex, "Rate limit exceeded for tile ({X}, {Y})", x, y); throw; } catch (Exception ex) { _logger.LogError(ex, "Failed to download tile ({X}, {Y})", x, y); throw; } } } _logger.LogInformation("Downloaded {Count} new tiles, skipped {Skipped} existing tiles", downloadedTiles.Count, skippedCount); return downloadedTiles; } }