mastodonapi.php aktualisiert

This commit is contained in:
simonpipe 2025-08-13 16:46:23 +02:00
parent 0fe5607616
commit 60341a408c

View file

@ -1,8 +1,4 @@
<?php <?php
// MastodonAPI & GoToSocial extension
// Kompatibel mit Mastodon-Servern & GoToSocial-Instanzen
// https://codeberg.org/simonpipe/yellow-mastodonapi (angepasst)
class YellowMastodonAPI { class YellowMastodonAPI {
const VERSION = "0.6"; const VERSION = "0.6";
public $yellow; public $yellow;
@ -27,7 +23,7 @@ class YellowMastodonAPI {
$this->sendResponse( $this->sendResponse(
$postsCreated !== false $postsCreated !== false
? "Mastodon/GoToSocial API feed processed. Created $postsCreated posts." ? "Mastodon/GoToSocial API feed processed. Created $postsCreated posts."
: "Error processing Mastodon/GoToSocial API feed. Check logs for details.", : "Error processing Mastodon/GoToSocial API feed.",
$postsCreated !== false ? 200 : 500 $postsCreated !== false ? 200 : 500
); );
return 1; return 1;
@ -50,7 +46,7 @@ class YellowMastodonAPI {
private function processMastodonFeed() { private function processMastodonFeed() {
$instanceUrl = $this->yellow->system->get("mastodonapiInstanceUrl"); $instanceUrl = $this->yellow->system->get("mastodonapiInstanceUrl");
$accountId = $this->yellow->system->get("mastodonapiAccountId"); $accountId = $this->yellow->system->get("mastodonapiAccountId");
$accessToken = $this->yellow->system->get("mastodonapiAccessToken"); // optional $accessToken = $this->yellow->system->get("mastodonapiAccessToken");
$hashtag = ltrim($this->yellow->system->get("mastodonapiHashtag"), '#'); $hashtag = ltrim($this->yellow->system->get("mastodonapiHashtag"), '#');
$defaultAuthor = $this->yellow->system->get("mastodonapiAuthor"); $defaultAuthor = $this->yellow->system->get("mastodonapiAuthor");
$defaultTag = $this->yellow->system->get("mastodonapiTag"); $defaultTag = $this->yellow->system->get("mastodonapiTag");
@ -58,7 +54,6 @@ class YellowMastodonAPI {
$limit = intval($this->yellow->system->get("mastodonapiLimit")); $limit = intval($this->yellow->system->get("mastodonapiLimit"));
if (empty($instanceUrl) || empty($accountId)) { if (empty($instanceUrl) || empty($accountId)) {
error_log("ERROR: MastodonAPI: Missing configuration values.");
return false; return false;
} }
@ -69,7 +64,6 @@ class YellowMastodonAPI {
$headers[] = "Authorization: Bearer " . $accessToken; $headers[] = "Authorization: Bearer " . $accessToken;
} }
// Daten via cURL abrufen (funktioniert bei Mastodon & GoToSocial)
$ch = curl_init($apiUrl); $ch = curl_init($apiUrl);
curl_setopt_array($ch, [ curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
@ -81,17 +75,14 @@ class YellowMastodonAPI {
]); ]);
$json = curl_exec($ch); $json = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch); curl_close($ch);
if ($json === false || $httpCode < 200 || $httpCode >= 300) { if ($json === false || $httpCode < 200 || $httpCode >= 300) {
error_log("ERROR: Could not fetch API data from $apiUrl (HTTP $httpCode, Error: $curlError)");
return false; return false;
} }
$posts = json_decode($json, true); $posts = json_decode($json, true);
if (!is_array($posts)) { if (!is_array($posts)) {
error_log("ERROR: MastodonAPI: Invalid JSON returned by API.");
return false; return false;
} }
@ -99,7 +90,6 @@ class YellowMastodonAPI {
$postsCreated = 0; $postsCreated = 0;
foreach ($posts as $post) { foreach ($posts as $post) {
// Keine Boosts / Antworten
if (!empty($post['reblog']) || (!empty($post['reblogged']) && $post['reblogged'] === true)) continue; if (!empty($post['reblog']) || (!empty($post['reblogged']) && $post['reblogged'] === true)) continue;
if (!is_null($post['in_reply_to_id'])) continue; if (!is_null($post['in_reply_to_id'])) continue;
@ -164,7 +154,6 @@ class YellowMastodonAPI {
?? ($media['meta']['small']['url'] ?? null); ?? ($media['meta']['small']['url'] ?? null);
if (empty($imageUrl)) { if (empty($imageUrl)) {
error_log("ERROR: No usable image URL found in media attachment: " . json_encode($media));
return false; return false;
} }
@ -190,11 +179,9 @@ class YellowMastodonAPI {
]); ]);
$imgData = curl_exec($ch); $imgData = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch); curl_close($ch);
if ($imgData === false || $httpCode < 200 || $httpCode >= 300) { if ($imgData === false || $httpCode < 200 || $httpCode >= 300) {
error_log("ERROR: Could not download image from $imageUrl (HTTP $httpCode, Error: $curlError)");
return false; return false;
} }
@ -202,8 +189,6 @@ class YellowMastodonAPI {
return $fileName; return $fileName;
} }
// Rest der Helper-Methoden wie bei dir (generateTitle, generateTitleSlug, generateMarkdown, getYellowConfig, sendResponse) ...
private function generateTitle($content) { private function generateTitle($content) {
$text = strip_tags($content); $text = strip_tags($content);
$text = preg_replace('/https?:\/\/[^\s]+|#\w+/u', '', $text); $text = preg_replace('/https?:\/\/[^\s]+|#\w+/u', '', $text);
@ -227,12 +212,10 @@ class YellowMastodonAPI {
$content = strip_tags($html); $content = strip_tags($html);
$content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5, 'UTF-8'); $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5, 'UTF-8');
// Hashtag-Block extrahieren
$hashtagString = ''; $hashtagString = '';
if (preg_match('/(\s*(#\w+))+$/u', $content, $matches)) { if (preg_match('/(\s*(#\w+))+$/u', $content, $matches)) {
$content = substr($content, 0, -strlen($matches[0])); $content = substr($content, 0, -strlen($matches[0]));
if (preg_match_all('/#(\w+)/u', $matches[0], $tags)) { if (preg_match_all('/#(\w+)/u', $matches[0], $tags)) {
// Ersetzt '#' durch '&num;' für die Ausgabe
$safeHashtags = array_map(function($tag) { $safeHashtags = array_map(function($tag) {
return str_replace('#', '&num;', $tag); return str_replace('#', '&num;', $tag);
}, $tags[0]); }, $tags[0]);
@ -250,7 +233,6 @@ class YellowMastodonAPI {
$featuredImage = ''; $featuredImage = '';
$featuredImageAlt = ''; $featuredImageAlt = '';
if (!empty($images)) { if (!empty($images)) {
$featuredImage = "Image: " . $images[0] . "\n"; $featuredImage = "Image: " . $images[0] . "\n";
if (isset($alts[0]) && !empty($alts[0])) { if (isset($alts[0]) && !empty($alts[0])) {
@ -260,7 +242,6 @@ class YellowMastodonAPI {
$htmlSafeAuthorName = str_replace('@', '&#64;', $origAuthor); $htmlSafeAuthorName = str_replace('@', '&#64;', $origAuthor);
// Baut den Blockquote-String robust und schrittweise zusammen
$blockquote = "> \"$content\""; $blockquote = "> \"$content\"";
if (!empty($hashtagString)) { if (!empty($hashtagString)) {
$blockquote .= "\n>\n> " . $hashtagString; $blockquote .= "\n>\n> " . $hashtagString;