mastodonapi.php aktualisiert

This commit is contained in:
simonpipe 2025-08-09 17:04:28 +02:00
parent 078c04af51
commit 090d72abca

View file

@ -1,6 +1,78 @@
$hashtag = $this->yellow->system->get("mastodonapiHashtag"); // Filter-Hashtag
$tag = $this->yellow->system->get("mastodonapiTag"); // Yellow-Tag
<?php
// MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi
class YellowMastodonAPI {
const VERSION = "0.1";
public $yellow;
public function onLoad($yellow) {
$this->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");
}
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;
}
public function onCommand($command, $text) {
if ($command == "mastodonapi") {
$postsCreated = $this->processMastodonAPI();
return $postsCreated !== false ? 0 : 500;
}
return 0;
}
private function processMastodonAPI() {
$instance = rtrim($this->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) {
@ -34,3 +106,16 @@ MARKDOWN;
$postsCreated++;
}
}
return $postsCreated;
}
private function sendResponse($message, $statusCode = 200) {
if (!headers_sent()) {
header("HTTP/1.1 $statusCode");
header("Content-Type: text/plain");
}
echo $message;
exit;
}
}