update ergänzt
This commit is contained in:
parent
45c14d8580
commit
e349d57bbe
142
refresh.php
142
refresh.php
|
|
@ -1,9 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
// refresh.php
|
// refresh.php
|
||||||
|
|
||||||
// Ausgabe als reiner Text (alternativ kann JSON verwendet werden)
|
// Setze den Content-Type als reinen Text für Log-Ausgaben.
|
||||||
header('Content-Type: text/plain');
|
header('Content-Type: text/plain');
|
||||||
|
|
||||||
|
$startTime = microtime(true);
|
||||||
|
echo "Refresh gestartet um " . date('Y-m-d H:i:s') . "\n";
|
||||||
|
|
||||||
// Datenbank-Verbindungsparameter (bitte anpassen)
|
// Datenbank-Verbindungsparameter (bitte anpassen)
|
||||||
$host = 'localhost';
|
$host = 'localhost';
|
||||||
$db = 'web35_vysion';
|
$db = 'web35_vysion';
|
||||||
|
|
@ -20,11 +23,11 @@ $options = [
|
||||||
try {
|
try {
|
||||||
$pdo = new PDO($dsn, $user, $pass, $options);
|
$pdo = new PDO($dsn, $user, $pass, $options);
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
echo "Database connection failed: " . $e->getMessage();
|
echo "Datenbankverbindung fehlgeschlagen: " . $e->getMessage();
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bearer Token für den Mastodon API-Aufruf (bitte anpassen)
|
// Bearer-Token für den Mastodon API-Aufruf (bitte anpassen)
|
||||||
$bearerToken = '99jJEXMrMPSGKyg0wp-OrGJ-5s38XPLlE0lmNmv6QSI';
|
$bearerToken = '99jJEXMrMPSGKyg0wp-OrGJ-5s38XPLlE0lmNmv6QSI';
|
||||||
|
|
||||||
// Alle Hashtags aus der Tabelle abrufen
|
// Alle Hashtags aus der Tabelle abrufen
|
||||||
|
|
@ -36,29 +39,34 @@ if (!$hashtags) {
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SQL-Statement zum Einfügen in die findings-Tabelle
|
// Bereite SQL-Statements vor:
|
||||||
// Hier wird INSERT IGNORE verwendet, um doppelte Einträge (basierend auf dem Primary Key (hashtag_id, post_id)) zu überspringen.
|
// Statement zum Überprüfen, ob ein Eintrag existiert
|
||||||
$insertSql = "INSERT IGNORE INTO findings (
|
$checkStmt = $pdo->prepare("SELECT COUNT(*) as count FROM findings WHERE hashtag_id = :hashtag_id AND post_id = :post_id");
|
||||||
hashtag_id,
|
// Statement zum Einfügen eines neuen Eintrags
|
||||||
post_id,
|
$insertStmt = $pdo->prepare("INSERT INTO findings (
|
||||||
created_at,
|
hashtag_id, post_id, created_at, in_reply_to_id, replies_count, reblogs_count, favorites_count, acc_id, username, acct
|
||||||
in_reply_to_id,
|
|
||||||
replies_count,
|
|
||||||
reblogs_count,
|
|
||||||
favorites_count,
|
|
||||||
acc_id,
|
|
||||||
username,
|
|
||||||
acct
|
|
||||||
) VALUES (
|
) VALUES (
|
||||||
:hashtag_id, :post_id, :created_at, :in_reply_to_id, :replies_count, :reblogs_count, :favorites_count, :acc_id, :username, :acct
|
:hashtag_id, :post_id, :created_at, :in_reply_to_id, :replies_count, :reblogs_count, :favorites_count, :acc_id, :username, :acct
|
||||||
)";
|
)");
|
||||||
$insertStmt = $pdo->prepare($insertSql);
|
// Statement zum Aktualisieren eines bestehenden Eintrags
|
||||||
|
$updateStmt = $pdo->prepare("UPDATE findings SET
|
||||||
|
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
|
||||||
|
WHERE hashtag_id = :hashtag_id AND post_id = :post_id");
|
||||||
|
|
||||||
// Für jeden Hashtag den Mastodon API-Aufruf tätigen
|
// Für jeden Hashtag den Mastodon API-Aufruf tätigen
|
||||||
foreach ($hashtags as $row) {
|
foreach ($hashtags as $row) {
|
||||||
$hashtag = $row['hashtag'];
|
$hashtag = $row['hashtag'];
|
||||||
$hashtag_id = $row['hashtag_id'];
|
$hashtag_id = $row['hashtag_id'];
|
||||||
|
|
||||||
|
echo "Verarbeite Hashtag '$hashtag' (ID: $hashtag_id)...\n";
|
||||||
|
|
||||||
// Baue den API-URL: Beispiel: https://mastodon.social/api/v1/timelines/tag/fashionforwardvienna
|
// Baue den API-URL: Beispiel: https://mastodon.social/api/v1/timelines/tag/fashionforwardvienna
|
||||||
$apiUrl = "https://mastodon.social/api/v1/timelines/tag/" . urlencode($hashtag);
|
$apiUrl = "https://mastodon.social/api/v1/timelines/tag/" . urlencode($hashtag);
|
||||||
|
|
||||||
|
|
@ -71,8 +79,8 @@ foreach ($hashtags as $row) {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
if(curl_errno($ch)) {
|
if (curl_errno($ch)) {
|
||||||
echo "cURL Fehler für Hashtag $hashtag: " . curl_error($ch) . "\n";
|
echo "cURL-Fehler für Hashtag $hashtag: " . curl_error($ch) . "\n";
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -84,28 +92,32 @@ foreach ($hashtags as $row) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON-Antwort dekodieren
|
|
||||||
$data = json_decode($response, true);
|
$data = json_decode($response, true);
|
||||||
if (!$data) {
|
if (!$data) {
|
||||||
echo "Fehler beim Dekodieren des JSON für Hashtag $hashtag.\n";
|
echo "Fehler beim Dekodieren des JSON für Hashtag $hashtag.\n";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Jeden Post im Ergebnis verarbeiten und in die findings-Tabelle einfügen
|
$totalPosts = count($data);
|
||||||
|
$newCount = 0;
|
||||||
|
$updateCount = 0;
|
||||||
|
|
||||||
|
// Jeden Post aus der API-Antwort verarbeiten
|
||||||
foreach ($data as $post) {
|
foreach ($data as $post) {
|
||||||
// Mapping der Felder aus dem JSON zur Tabelle findings:
|
// Mapping der Felder aus dem JSON zur Tabelle findings:
|
||||||
// "id" -> post_id
|
// "id" -> post_id
|
||||||
// "created_at" -> created_at
|
// "created_at" -> created_at (Konvertierung von ISO8601 zu MySQL-Format)
|
||||||
// "in_reply_to_id" -> in_reply_to_id
|
// "in_reply_to_id" -> in_reply_to_id
|
||||||
// "replies_count" -> replies_count
|
// "replies_count" -> replies_count
|
||||||
// "reblogs_count" -> reblogs_count
|
// "reblogs_count" -> reblogs_count
|
||||||
// "favourites_count" -> favorites_count
|
// "favourites_count" -> favorites_count
|
||||||
// account -> "id" -> acc_id
|
// account -> "id" -> acc_id
|
||||||
// account -> "username" -> username
|
// account -> "username" -> username
|
||||||
// account -> "acct" -> acct
|
// account -> "acct" -> acct
|
||||||
|
|
||||||
$post_id = isset($post['id']) ? $post['id'] : null;
|
$post_id = isset($post['id']) ? $post['id'] : null;
|
||||||
$created_at = isset($post['created_at']) ? $post['created_at'] : null;
|
$created_at_raw = isset($post['created_at']) ? $post['created_at'] : null;
|
||||||
|
$created_at = $created_at_raw ? (new DateTime($created_at_raw))->format('Y-m-d H:i:s') : null;
|
||||||
$in_reply_to_id = isset($post['in_reply_to_id']) ? $post['in_reply_to_id'] : 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;
|
$replies_count = isset($post['replies_count']) ? $post['replies_count'] : 0;
|
||||||
$reblogs_count = isset($post['reblogs_count']) ? $post['reblogs_count'] : 0;
|
$reblogs_count = isset($post['reblogs_count']) ? $post['reblogs_count'] : 0;
|
||||||
|
|
@ -115,27 +127,57 @@ foreach ($hashtags as $row) {
|
||||||
$username = isset($post['account']['username']) ? $post['account']['username'] : null;
|
$username = isset($post['account']['username']) ? $post['account']['username'] : null;
|
||||||
$acct = isset($post['account']['acct']) ? $post['account']['acct'] : null;
|
$acct = isset($post['account']['acct']) ? $post['account']['acct'] : null;
|
||||||
|
|
||||||
// Datensatz in die Tabelle einfügen
|
// Vor dem Einfügen prüfen, ob der Eintrag bereits existiert
|
||||||
try {
|
$checkStmt->execute([
|
||||||
$insertStmt->execute([
|
'hashtag_id' => $hashtag_id,
|
||||||
'hashtag_id' => $hashtag_id,
|
'post_id' => $post_id
|
||||||
'post_id' => $post_id,
|
]);
|
||||||
'created_at' => $created_at,
|
$rowCount = $checkStmt->fetch();
|
||||||
'in_reply_to_id' => $in_reply_to_id,
|
if ($rowCount['count'] == 0) {
|
||||||
'replies_count' => $replies_count,
|
// Neuer Eintrag: Einfügen
|
||||||
'reblogs_count' => $reblogs_count,
|
try {
|
||||||
'favorites_count' => $favorites_count,
|
$insertStmt->execute([
|
||||||
'acc_id' => $acc_id,
|
'hashtag_id' => $hashtag_id,
|
||||||
'username' => $username,
|
'post_id' => $post_id,
|
||||||
'acct' => $acct
|
'created_at' => $created_at,
|
||||||
]);
|
'in_reply_to_id' => $in_reply_to_id,
|
||||||
} catch (PDOException $e) {
|
'replies_count' => $replies_count,
|
||||||
echo "Fehler beim Einfügen des Posts $post_id für Hashtag $hashtag: " . $e->getMessage() . "\n";
|
'reblogs_count' => $reblogs_count,
|
||||||
|
'favorites_count' => $favorites_count,
|
||||||
|
'acc_id' => $acc_id,
|
||||||
|
'username' => $username,
|
||||||
|
'acct' => $acct
|
||||||
|
]);
|
||||||
|
$newCount++;
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "Fehler beim Einfügen von Post $post_id für Hashtag $hashtag: " . $e->getMessage() . "\n";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Eintrag existiert bereits: Aktualisieren
|
||||||
|
try {
|
||||||
|
$updateStmt->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
|
||||||
|
]);
|
||||||
|
$updateCount++;
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo "Fehler beim Aktualisieren von Post $post_id für Hashtag $hashtag: " . $e->getMessage() . "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
echo "Hashtag '$hashtag': $totalPosts Posts gefunden. Neue: $newCount, Aktualisierte: $updateCount.\n";
|
||||||
echo "Hashtag '$hashtag' verarbeitet.\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "Refresh abgeschlossen.\n";
|
$endTime = microtime(true);
|
||||||
|
$totalTime = $endTime - $startTime;
|
||||||
|
echo "Refresh abgeschlossen in " . round($totalTime, 2) . " Sekunden.\n";
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue