mastodonapi.php aktualisiert
This commit is contained in:
parent
2c6033ede1
commit
c4cba211f0
1 changed files with 27 additions and 21 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
// MastodonAPI extension (public-only), https://codeberg.org/simonpipe/yellow-mastodonapi
|
// MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi
|
||||||
|
|
||||||
class YellowMastodonAPI {
|
class YellowMastodonAPI {
|
||||||
const VERSION = "0.2";
|
const VERSION = "0.21";
|
||||||
public $yellow;
|
public $yellow;
|
||||||
|
|
||||||
// Initialisierung
|
// Initialisierung
|
||||||
|
|
@ -19,8 +19,9 @@ class YellowMastodonAPI {
|
||||||
|
|
||||||
// Webhook-Request
|
// Webhook-Request
|
||||||
public function onRequest($scheme, $address, $base, $location, $fileName) {
|
public function onRequest($scheme, $address, $base, $location, $fileName) {
|
||||||
$triggerPath = $this->yellow->system->get("mastodonapiTriggerPath");
|
$triggerPath = rtrim($this->yellow->system->get("mastodonapiTriggerPath"), "/");
|
||||||
if ($location == $triggerPath) {
|
$locationClean = rtrim($location, "/");
|
||||||
|
if ($locationClean === $triggerPath) {
|
||||||
$postsCreated = $this->processMastodonFeed();
|
$postsCreated = $this->processMastodonFeed();
|
||||||
if ($postsCreated !== false) {
|
if ($postsCreated !== false) {
|
||||||
$this->sendResponse("Mastodon API feed processed. Created " . $postsCreated . " posts.", 200);
|
$this->sendResponse("Mastodon API feed processed. Created " . $postsCreated . " posts.", 200);
|
||||||
|
|
@ -45,7 +46,7 @@ class YellowMastodonAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// API-Logik
|
// API-Logik ohne Access Token
|
||||||
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");
|
||||||
|
|
@ -60,21 +61,27 @@ class YellowMastodonAPI {
|
||||||
}
|
}
|
||||||
|
|
||||||
$apiUrl = rtrim($instanceUrl, "/") . "/api/v1/accounts/" . $accountId . "/statuses?limit=20";
|
$apiUrl = rtrim($instanceUrl, "/") . "/api/v1/accounts/" . $accountId . "/statuses?limit=20";
|
||||||
$context = stream_context_create([
|
|
||||||
'http' => [
|
|
||||||
'method' => "GET",
|
|
||||||
'header' => "User-Agent: YellowMastodonAPI\r\n",
|
|
||||||
'timeout' => 30
|
|
||||||
],
|
|
||||||
'ssl' => [
|
|
||||||
'verify_peer' => true,
|
|
||||||
'verify_peer_name' => true
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
|
|
||||||
$json = @file_get_contents($apiUrl, false, $context);
|
// Abruf mit curl
|
||||||
|
$ch = curl_init($apiUrl);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_USERAGENT, "YellowMastodonAPI/0.2-public");
|
||||||
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
||||||
|
$json = curl_exec($ch);
|
||||||
|
|
||||||
if ($json === false) {
|
if ($json === false) {
|
||||||
error_log("ERROR: MastodonAPI: Could not fetch API data from $apiUrl");
|
error_log("ERROR: MastodonAPI: Curl error: " . curl_error($ch));
|
||||||
|
curl_close($ch);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
if ($httpCode !== 200) {
|
||||||
|
error_log("ERROR: MastodonAPI: HTTP status $httpCode from $apiUrl");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,9 +97,9 @@ class YellowMastodonAPI {
|
||||||
foreach ($posts as $post) {
|
foreach ($posts as $post) {
|
||||||
$contentHtml = $post['content'];
|
$contentHtml = $post['content'];
|
||||||
$createdAt = $post['created_at'];
|
$createdAt = $post['created_at'];
|
||||||
$tags = array_column($post['tags'], 'name');
|
$tags = array_map('strtolower', array_column($post['tags'], 'name'));
|
||||||
|
|
||||||
if (!empty($hashtag) && !in_array(strtolower($hashtag), array_map('strtolower', $tags))) {
|
if (!empty($hashtag) && !in_array(strtolower($hashtag), $tags)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,7 +110,6 @@ class YellowMastodonAPI {
|
||||||
$fullPath = $config->get("coreServerDocument") . $targetFolder . $fileName;
|
$fullPath = $config->get("coreServerDocument") . $targetFolder . $fileName;
|
||||||
|
|
||||||
if (file_exists($fullPath)) continue;
|
if (file_exists($fullPath)) continue;
|
||||||
|
|
||||||
if (!is_dir(dirname($fullPath))) mkdir(dirname($fullPath), 0755, true);
|
if (!is_dir(dirname($fullPath))) mkdir(dirname($fullPath), 0755, true);
|
||||||
|
|
||||||
$authorName = $post['account']['display_name'] ?: $defaultAuthor;
|
$authorName = $post['account']['display_name'] ?: $defaultAuthor;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue