mastodonapi.php aktualisiert
This commit is contained in:
parent
6f52cfa71d
commit
246adf20cf
1 changed files with 25 additions and 22 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
// MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi
|
// MastodonAPI extension, https://codeberg.org/simonpipe/yellow-mastodonapi
|
||||||
|
|
||||||
class YellowMastodonAPI {
|
class YellowMastodonAPI {
|
||||||
const VERSION = "0.2.1";
|
const VERSION = "0.2";
|
||||||
public $yellow;
|
public $yellow;
|
||||||
|
|
||||||
// Initialisierung
|
// Initialisierung
|
||||||
|
|
@ -10,6 +10,7 @@ class YellowMastodonAPI {
|
||||||
$this->yellow = $yellow;
|
$this->yellow = $yellow;
|
||||||
$this->yellow->system->setDefault("mastodonapiInstanceUrl", "https://example.social");
|
$this->yellow->system->setDefault("mastodonapiInstanceUrl", "https://example.social");
|
||||||
$this->yellow->system->setDefault("mastodonapiAccountId", "");
|
$this->yellow->system->setDefault("mastodonapiAccountId", "");
|
||||||
|
$this->yellow->system->setDefault("mastodonapiAccessToken", ""); // optional
|
||||||
$this->yellow->system->setDefault("mastodonapiHashtag", "");
|
$this->yellow->system->setDefault("mastodonapiHashtag", "");
|
||||||
$this->yellow->system->setDefault("mastodonapiAuthor", "YellowUser");
|
$this->yellow->system->setDefault("mastodonapiAuthor", "YellowUser");
|
||||||
$this->yellow->system->setDefault("mastodonapiTag", "Fediverse");
|
$this->yellow->system->setDefault("mastodonapiTag", "Fediverse");
|
||||||
|
|
@ -19,9 +20,8 @@ class YellowMastodonAPI {
|
||||||
|
|
||||||
// Webhook-Request
|
// Webhook-Request
|
||||||
public function onRequest($scheme, $address, $base, $location, $fileName) {
|
public function onRequest($scheme, $address, $base, $location, $fileName) {
|
||||||
$triggerPath = rtrim($this->yellow->system->get("mastodonapiTriggerPath"), "/");
|
$triggerPath = $this->yellow->system->get("mastodonapiTriggerPath");
|
||||||
$locationClean = rtrim($location, "/");
|
if ($location == $triggerPath) {
|
||||||
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);
|
||||||
|
|
@ -46,10 +46,11 @@ class YellowMastodonAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// API-Logik ohne Access Token
|
// API-Logik
|
||||||
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
|
||||||
$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");
|
||||||
|
|
@ -62,26 +63,27 @@ class YellowMastodonAPI {
|
||||||
|
|
||||||
$apiUrl = rtrim($instanceUrl, "/") . "/api/v1/accounts/" . $accountId . "/statuses?limit=20";
|
$apiUrl = rtrim($instanceUrl, "/") . "/api/v1/accounts/" . $accountId . "/statuses?limit=20";
|
||||||
|
|
||||||
// Abruf mit curl
|
// Header dynamisch aufbauen
|
||||||
$ch = curl_init($apiUrl);
|
$headers = "User-Agent: YellowMastodonAPI\r\n";
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
if (!empty($accessToken)) {
|
||||||
curl_setopt($ch, CURLOPT_USERAGENT, "YellowMastodonAPI/0.2-public");
|
$headers .= "Authorization: Bearer " . $accessToken . "\r\n";
|
||||||
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) {
|
|
||||||
error_log("ERROR: MastodonAPI: Curl error: " . curl_error($ch));
|
|
||||||
curl_close($ch);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$context = stream_context_create([
|
||||||
curl_close($ch);
|
'http' => [
|
||||||
|
'method' => "GET",
|
||||||
|
'header' => $headers,
|
||||||
|
'timeout' => 30
|
||||||
|
],
|
||||||
|
'ssl' => [
|
||||||
|
'verify_peer' => true,
|
||||||
|
'verify_peer_name' => true
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
if ($httpCode !== 200) {
|
$json = @file_get_contents($apiUrl, false, $context);
|
||||||
error_log("ERROR: MastodonAPI: HTTP status $httpCode from $apiUrl");
|
if ($json === false) {
|
||||||
|
error_log("ERROR: MastodonAPI: Could not fetch API data from $apiUrl");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -110,6 +112,7 @@ 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