From 090d72abcac3ca6b0ec02c1d161b72f172ddd66d Mon Sep 17 00:00:00 2001 From: simonpipe Date: Sat, 9 Aug 2025 17:04:28 +0200 Subject: [PATCH] mastodonapi.php aktualisiert --- mastodonapi.php | 121 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 103 insertions(+), 18 deletions(-) diff --git a/mastodonapi.php b/mastodonapi.php index f01842b..ed88b13 100644 --- a/mastodonapi.php +++ b/mastodonapi.php @@ -1,23 +1,95 @@ -$hashtag = $this->yellow->system->get("mastodonapiHashtag"); // Filter-Hashtag -$tag = $this->yellow->system->get("mastodonapiTag"); // Yellow-Tag +yellow = $yellow; + $yellow->system->setDefault("mastodonapiInstance", "https://mastodon.social"); + $yellow->system->setDefault("mastodonapiUserId", ""); + $yellow->system->setDefault("mastodonapiToken", ""); + $yellow->system->setDefault("mastodonapiHashtag", ""); // Nur Posts mit diesem Hashtag werden importiert + $yellow->system->setDefault("mastodonapiTag", "Fediverse"); // Dieser Tag wird im Yellow CMS gesetzt + $yellow->system->setDefault("mastodonapiAuthor", "YellowUser"); + $yellow->system->setDefault("mastodonapiFolder", "content/1-blog/"); + $yellow->system->setDefault("mastodonapiTriggerPath", "/mastodonapi-webhook-CHANGE_THIS_SECRET_KEY"); } - $content = strip_tags($status["content"]); - $createdAt = $status["created_at"]; - $title = mb_substr(preg_replace("/\\s+/", " ", $content), 0, 50); - $slug = strtolower(preg_replace("/[^a-z0-9]+/", "-", $title)); - $datePrefix = date("Y-m-d", strtotime($createdAt)); - $fileName = "$datePrefix-$slug.md"; - $path = $this->yellow->system->get("coreServerDocument") . $folder . $fileName; + public function onRequest($scheme, $address, $base, $location, $fileName) { + $triggerPath = $this->yellow->system->get("mastodonapiTriggerPath"); + if ($location == $triggerPath) { + $postsCreated = $this->processMastodonAPI(); + if ($postsCreated !== false) { + $this->sendResponse("Mastodon API processed. Created $postsCreated posts.", 200); + } else { + $this->sendResponse("Error processing Mastodon API. Check logs.", 500); + } + return 1; + } + return 0; + } - if (file_exists($path)) continue; + public function onCommand($command, $text) { + if ($command == "mastodonapi") { + $postsCreated = $this->processMastodonAPI(); + return $postsCreated !== false ? 0 : 500; + } + return 0; + } - $markdown = <<yellow->system->get("mastodonapiInstance"), "/"); + $userId = $this->yellow->system->get("mastodonapiUserId"); + $token = $this->yellow->system->get("mastodonapiToken"); + $hashtag = $this->yellow->system->get("mastodonapiHashtag"); + $tag = $this->yellow->system->get("mastodonapiTag"); + $author = $this->yellow->system->get("mastodonapiAuthor"); + $folder = $this->yellow->system->get("mastodonapiFolder"); + + if (empty($userId) || empty($token)) { + error_log("ERROR: MastodonAPI: Missing user ID or token."); + return false; + } + + $url = "$instance/api/v1/accounts/$userId/statuses?exclude_replies=true&exclude_reblogs=true&limit=5"; + $opts = [ + "http" => [ + "method" => "GET", + "header" => "Authorization: Bearer $token\r\nUser-Agent: YellowCMS\r\n" + ] + ]; + $context = stream_context_create($opts); + $json = @file_get_contents($url, false, $context); + if ($json === false) { + error_log("ERROR: MastodonAPI: Failed to fetch API data."); + return false; + } + + $statuses = json_decode($json, true); + if (!is_array($statuses)) { + error_log("ERROR: MastodonAPI: Invalid JSON response."); + return false; + } + + $postsCreated = 0; + foreach ($statuses as $status) { + // Nur Posts mit dem gewünschten Hashtag importieren + if (!empty($hashtag) && stripos($status["content"], "#$hashtag") === false) { + continue; + } + + $content = strip_tags($status["content"]); + $createdAt = $status["created_at"]; + $title = mb_substr(preg_replace("/\\s+/", " ", $content), 0, 50); + $slug = strtolower(preg_replace("/[^a-z0-9]+/", "-", $title)); + $datePrefix = date("Y-m-d", strtotime($createdAt)); + $fileName = "$datePrefix-$slug.md"; + $path = $this->yellow->system->get("coreServerDocument") . $folder . $fileName; + + if (file_exists($path)) continue; + + $markdown = << — [@{$status["account"]["acct"]}]({$status["account"]["url"]}) MARKDOWN; - if (file_put_contents($path, $markdown) !== false) { - $postsCreated++; + if (file_put_contents($path, $markdown) !== false) { + $postsCreated++; + } + } + + return $postsCreated; } -} \ No newline at end of file + + private function sendResponse($message, $statusCode = 200) { + if (!headers_sent()) { + header("HTTP/1.1 $statusCode"); + header("Content-Type: text/plain"); + } + echo $message; + exit; + } +}