using MySql.Data.MySqlClient; using Newtonsoft.Json.Linq; using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView; namespace webCrawlerGoogle { public partial class Form1 : Form { private const string ApiKey = "AIzaSyCjdysuuyc2bs4ikqT8xyIpPJHiDZ4CEo4"; // Füge hier deinen API-Schlüssel ein private string connectionString = "Server=192.168.178.201;Database=domainchecker;User ID=root;Password=1td5rugut8;"; public Form1() { InitializeComponent(); } private async void btnSearch_Click(object sender, EventArgs e) { string category = txtCategory.Text; string city = txtCity.Text; if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(city)) { MessageBox.Show("Bitte sowohl eine Rubrik als auch eine Stadt eingeben."); return; } string requestUri = $"https://maps.googleapis.com/maps/api/place/textsearch/json?query={category}+in+{city}&key={ApiKey}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(requestUri); if (response.IsSuccessStatusCode) { string jsonResult = await response.Content.ReadAsStringAsync(); JObject result = JObject.Parse(jsonResult); lstResults.Items.Clear(); foreach (var place in result["results"]) { string website = await GetPlaceDetails((string)place["place_id"]); if (!string.IsNullOrEmpty(website)) { lstResults.Items.Add(website); SaveToDatabase(website); } } } else { MessageBox.Show("Fehler bei der Anfrage: " + response.StatusCode); } } } private async Task GetPlaceDetails(string placeId) { string requestUri = $"https://maps.googleapis.com/maps/api/place/details/json?place_id={placeId}&fields=website&key={ApiKey}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(requestUri); if (response.IsSuccessStatusCode) { string jsonResult = await response.Content.ReadAsStringAsync(); JObject result = JObject.Parse(jsonResult); return (string)result["result"]["website"]; } return null; } } private void SaveToDatabase(string website) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.Open(); string queryCheck = "SELECT COUNT(*) FROM webCrawler WHERE webseite = @website"; using (MySqlCommand cmdCheck = new MySqlCommand(queryCheck, conn)) { cmdCheck.Parameters.AddWithValue("@website", website); int count = Convert.ToInt32(cmdCheck.ExecuteScalar()); if (count == 0) { string queryInsert = "INSERT INTO webCrawler (webseite) VALUES (@website)"; using (MySqlCommand cmdInsert = new MySqlCommand(queryInsert, conn)) { cmdInsert.Parameters.AddWithValue("@website", website); cmdInsert.ExecuteNonQuery(); } } } } } } }