From 45c14d85802b6d1ab4c048cb63d030418de3079b Mon Sep 17 00:00:00 2001 From: Matthias Czech Date: Mon, 17 Mar 2025 17:19:33 +0100 Subject: [PATCH] search API und refresh (only initial) --- refresh.php | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++ search.php | 64 ++++++++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 refresh.php create mode 100644 search.php diff --git a/refresh.php b/refresh.php new file mode 100644 index 0000000..da95fdf --- /dev/null +++ b/refresh.php @@ -0,0 +1,141 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, +]; + +try { + $pdo = new PDO($dsn, $user, $pass, $options); +} catch (PDOException $e) { + echo "Database connection failed: " . $e->getMessage(); + exit; +} + +// Bearer Token für den Mastodon API-Aufruf (bitte anpassen) +$bearerToken = '99jJEXMrMPSGKyg0wp-OrGJ-5s38XPLlE0lmNmv6QSI'; + +// Alle Hashtags aus der Tabelle abrufen +$stmt = $pdo->query("SELECT hashtag, hashtag_id FROM hashtags"); +$hashtags = $stmt->fetchAll(); + +if (!$hashtags) { + echo "Keine Hashtags gefunden.\n"; + exit; +} + +// SQL-Statement zum Einfügen in die findings-Tabelle +// Hier wird INSERT IGNORE verwendet, um doppelte Einträge (basierend auf dem Primary Key (hashtag_id, post_id)) zu überspringen. +$insertSql = "INSERT IGNORE INTO findings ( + hashtag_id, + post_id, + created_at, + in_reply_to_id, + replies_count, + reblogs_count, + favorites_count, + acc_id, + username, + acct +) VALUES ( + :hashtag_id, :post_id, :created_at, :in_reply_to_id, :replies_count, :reblogs_count, :favorites_count, :acc_id, :username, :acct +)"; +$insertStmt = $pdo->prepare($insertSql); + +// Für jeden Hashtag den Mastodon API-Aufruf tätigen +foreach ($hashtags as $row) { + $hashtag = $row['hashtag']; + $hashtag_id = $row['hashtag_id']; + + // Baue den API-URL: Beispiel: https://mastodon.social/api/v1/timelines/tag/fashionforwardvienna + $apiUrl = "https://mastodon.social/api/v1/timelines/tag/" . urlencode($hashtag); + + // cURL initialisieren + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $apiUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "Authorization: Bearer $bearerToken" + ]); + + $response = curl_exec($ch); + if(curl_errno($ch)) { + echo "cURL Fehler für Hashtag $hashtag: " . curl_error($ch) . "\n"; + curl_close($ch); + continue; + } + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if ($httpCode != 200) { + echo "API-Aufruf für Hashtag $hashtag lieferte HTTP-Code $httpCode\n"; + continue; + } + + // JSON-Antwort dekodieren + $data = json_decode($response, true); + if (!$data) { + echo "Fehler beim Dekodieren des JSON für Hashtag $hashtag.\n"; + continue; + } + + // Jeden Post im Ergebnis verarbeiten und in die findings-Tabelle einfügen + foreach ($data as $post) { + // Mapping der Felder aus dem JSON zur Tabelle findings: + // "id" -> post_id + // "created_at" -> created_at + // "in_reply_to_id" -> in_reply_to_id + // "replies_count" -> replies_count + // "reblogs_count" -> reblogs_count + // "favourites_count" -> favorites_count + // account -> "id" -> acc_id + // account -> "username" -> username + // account -> "acct" -> acct + + $post_id = isset($post['id']) ? $post['id'] : null; + $created_at = isset($post['created_at']) ? $post['created_at'] : null; + $in_reply_to_id = isset($post['in_reply_to_id']) ? $post['in_reply_to_id'] : null; + $replies_count = isset($post['replies_count']) ? $post['replies_count'] : 0; + $reblogs_count = isset($post['reblogs_count']) ? $post['reblogs_count'] : 0; + $favorites_count = isset($post['favourites_count']) ? $post['favourites_count'] : 0; + + $acc_id = isset($post['account']['id']) ? $post['account']['id'] : null; + $username = isset($post['account']['username']) ? $post['account']['username'] : null; + $acct = isset($post['account']['acct']) ? $post['account']['acct'] : null; + + // Datensatz in die Tabelle einfügen + try { + $insertStmt->execute([ + 'hashtag_id' => $hashtag_id, + 'post_id' => $post_id, + 'created_at' => $created_at, + 'in_reply_to_id' => $in_reply_to_id, + 'replies_count' => $replies_count, + 'reblogs_count' => $reblogs_count, + 'favorites_count' => $favorites_count, + 'acc_id' => $acc_id, + 'username' => $username, + 'acct' => $acct + ]); + } catch (PDOException $e) { + echo "Fehler beim Einfügen des Posts $post_id für Hashtag $hashtag: " . $e->getMessage() . "\n"; + } + } + + echo "Hashtag '$hashtag' verarbeitet.\n"; +} + +echo "Refresh abgeschlossen.\n"; +?> diff --git a/search.php b/search.php new file mode 100644 index 0000000..6906eb4 --- /dev/null +++ b/search.php @@ -0,0 +1,64 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, +]; + +try { + $pdo = new PDO($dsn, $user, $pass, $options); +} catch (PDOException $e) { + http_response_code(500); + echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]); + exit; +} + +// Überprüfe, ob der Parameter "q" übergeben wurde +if (!isset($_GET['q'])) { + http_response_code(400); + echo json_encode(['error' => 'Missing query parameter "q".']); + exit; +} + +// Den Query-Parameter auslesen und URL-dekodieren +$q = $_GET['q']; +$q_decoded = urldecode($q); + +// Erwartetes Format: "from:username#hashtag" +// Beispiel: "from:silvesterhasani#bevysionary" +if (preg_match('/^from:([^#]+)#(.+)$/i', $q_decoded, $matches)) { + $username = $matches[1]; + $hashtag = $matches[2]; +} else { + http_response_code(400); + echo json_encode(['error' => 'Invalid query format. Expected format: "from:username#hashtag".']); + exit; +} + +// SQL-Abfrage: Finde alle findings, die zum extrahierten username und hashtag passen +$sql = "SELECT h.hashtag, f.post_id, f.created_at, f.in_reply_to_id, + f.replies_count, f.reblogs_count, f.favorites_count, + f.acc_id, f.username, f.acct + FROM findings f + JOIN hashtags h ON f.hashtag_id = h.hashtag_id + WHERE f.username = :username AND h.hashtag = :hashtag"; + +$stmt = $pdo->prepare($sql); +$stmt->execute(['username' => $username, 'hashtag' => $hashtag]); +$results = $stmt->fetchAll(); + +// Gib das Ergebnis als JSON aus +echo json_encode($results); +?>