|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | + |
| 5 | +class Program |
| 6 | +{ |
| 7 | + static async Task Main(string[] args) |
| 8 | + { |
| 9 | + string filePath = "../Style.ultra"; |
| 10 | + |
| 11 | + var lines = File.ReadAllLines(filePath); |
| 12 | + |
| 13 | + bool inPreamble = true; |
| 14 | + |
| 15 | + var mascots = new List<string>(); |
| 16 | + |
| 17 | + for (int i = 0; i < lines.Length; i++) |
| 18 | + { |
| 19 | + if (lines[i] == " - - match") |
| 20 | + break; |
| 21 | + |
| 22 | + if (inPreamble) |
| 23 | + { |
| 24 | + if (lines[i] == " - [ \"get\", \"mascot\" ]") |
| 25 | + { |
| 26 | + inPreamble = false; |
| 27 | + } |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + if (lines[i].StartsWith(" - - ")) |
| 32 | + { |
| 33 | + mascots.Add(lines[i].Replace(" - - ", "")); |
| 34 | + } |
| 35 | + else if (lines[i].StartsWith(" - ")) |
| 36 | + { |
| 37 | + mascots.Add(lines[i].Replace(" - ", "")); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + string apiUrl = "https://taginfo.openstreetmap.org/api/4/key/values?key=mascot"; |
| 43 | + // Call the function to fetch and compare results |
| 44 | + List<string> newResults = await FetchAndReturnValues(apiUrl); |
| 45 | + PrintNewItems(newResults, mascots); |
| 46 | + } |
| 47 | + |
| 48 | + static async Task<List<string>> FetchAndReturnValues(string url) |
| 49 | + { |
| 50 | + using (HttpClient client = new HttpClient()) |
| 51 | + { |
| 52 | + var values = new List<string>(); |
| 53 | + |
| 54 | + try |
| 55 | + { |
| 56 | + HttpResponseMessage response = await client.GetAsync(url); |
| 57 | + response.EnsureSuccessStatusCode(); |
| 58 | + |
| 59 | + string responseBody = await response.Content.ReadAsStringAsync(); |
| 60 | + |
| 61 | + // Parse the JSON and extract only "value" fields |
| 62 | + var jsonData = System.Text.Json.JsonDocument.Parse(responseBody); |
| 63 | + var dataArray = jsonData.RootElement.GetProperty("data"); |
| 64 | + |
| 65 | + foreach (var item in dataArray.EnumerateArray()) |
| 66 | + { |
| 67 | + if (item.TryGetProperty("value", out var value)) |
| 68 | + { |
| 69 | + values.Add(value.GetString()); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + catch (Exception e) |
| 74 | + { |
| 75 | + Console.WriteLine($"Error: {e.Message}"); |
| 76 | + } |
| 77 | + |
| 78 | + return values; |
| 79 | + } |
| 80 | + } |
| 81 | + static void PrintNewItems(List<string> newResults, List<string> oldResults) |
| 82 | + { |
| 83 | + string fileName = "../NewMascots.txt"; |
| 84 | + |
| 85 | + try |
| 86 | + { |
| 87 | + using (StreamWriter writer = new StreamWriter(fileName)) |
| 88 | + { |
| 89 | + foreach (var newItem in newResults) |
| 90 | + { |
| 91 | + if (!oldResults.Contains(newItem)) |
| 92 | + { |
| 93 | + writer.WriteLine(newItem); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + Console.WriteLine($"New items have been written to {fileName}"); |
| 99 | + } |
| 100 | + catch (Exception ex) |
| 101 | + { |
| 102 | + Console.WriteLine($"Error writing to file: {ex.Message}"); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments