From b36746721a11846329b2526df4345918acc64ce0 Mon Sep 17 00:00:00 2001 From: e2800 Date: Fri, 10 Apr 2020 21:48:35 +0200 Subject: [PATCH] First version --- Mastodon.php | 41 ++++++++++++++++++++++ examples/media_bot.php | 32 +++++++++++++++++ examples/reddit_bot.php | 77 ++++++++++++++++++++++++++++++++++++++++ examples/rss_bot.php | 78 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 228 insertions(+) create mode 100644 Mastodon.php create mode 100644 examples/media_bot.php create mode 100644 examples/reddit_bot.php create mode 100644 examples/rss_bot.php diff --git a/Mastodon.php b/Mastodon.php new file mode 100644 index 0000000..44a8c40 --- /dev/null +++ b/Mastodon.php @@ -0,0 +1,41 @@ +token = $token; + $this->instance_url = $instance_url; + } + + public function postStatus($status) { + return $this->callAPI("/api/v1/statuses", "POST", $status); + } + + public function uploadMedia($media) { + return $this->callAPI("/api/v1/media", "POST", $media); + } + + + public function callAPI($endpoint, $method, $data) { + $headers = [ + 'Authorization: Bearer ' . $this->token, + 'Content-Type: multipart/form-data' + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->instance_url . $endpoint); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $reply = curl_exec($ch); + + if (!$reply) { + return json_encode(['ok'=>false, 'curl_error_code' => curl_errno($ch_status), 'curl_error' => curl_error(ch_status)]); + } + curl_close ($ch); + return json_decode($reply, true); + } + } +?> \ No newline at end of file diff --git a/examples/media_bot.php b/examples/media_bot.php new file mode 100644 index 0000000..b3bf82c --- /dev/null +++ b/examples/media_bot.php @@ -0,0 +1,32 @@ + $curl_file + ]; + + $response = $mastodon -> uploadMedia($body); + + $file_id = $response->id; + + $statusText = "This is a status"; + + $status_data = array( + "status" => $statusText, + "visibility" => $visibility, + "language" => $language, + "media_ids[]" => $file_id + ); + + $mastodon -> postStatus($status_data); + +?> \ No newline at end of file diff --git a/examples/reddit_bot.php b/examples/reddit_bot.php new file mode 100644 index 0000000..97aa3d6 --- /dev/null +++ b/examples/reddit_bot.php @@ -0,0 +1,77 @@ + $lastDate) && !$itemData["is_self"]) { + $post = new BlogPost(); + $post->link = $itemData["url"]; + $post->title = $itemData["title"]; + $post->category = $itemData["subreddit"]; + $post->nsfw = $itemData["over_18"]; + + array_push($statuses, $post); + if (!$first_article) { + $myfile = fopen($file, "w") or die("Unable to open file!"); + fwrite($myfile, $ts); + fclose($myfile); + $first_article = true; + } + } + } + + $statuses = array_reverse($statuses); + $mastodon = new MastodonAPI($token, $base_url); + + foreach ($statuses as $s) { + $status_data = array( + "visibility" => $visibility, + "language" => $language + ); + + $statusText = $s->title . $tagline . $s->link . " "; + + if ($s->nsfw) { + $status_data["status"] = "NSFW"; + $status_data["spoiler_text"] = $statusText; + } else { + $status_data["status"] = $statusText; + } + + $mastodon -> postStatus($status_data); + } + + + class BlogPost { + var $ts; + var $link; + var $category; + var $title; + var $nsfw; + } + +?> \ No newline at end of file diff --git a/examples/rss_bot.php b/examples/rss_bot.php new file mode 100644 index 0000000..2625fd2 --- /dev/null +++ b/examples/rss_bot.php @@ -0,0 +1,78 @@ +channel->item as $item) { + $ts = strtotime($item->pubDate); + if ($lastDate == "" || $ts > $lastDate) { + $post = new BlogPost(); + $post->link = (string) $item->link; + $post->title = (string) $item->title; + + $post->category = $item->category; + + array_push($statuses, $post); + if (!$first_article) { + $myfile = fopen($file, "w") or die("Unable to open file!"); + fwrite($myfile, $ts); + fclose($myfile); + $first_article = true; + } + } + } + + $statuses = array_reverse($statuses); + $mastodon = new MastodonAPI($token, $base_url); + + foreach ($statuses as $s) { + $statusText = $s->title . " leggi su " . $s->link . " "; + + foreach ($s->category as $c) { + $statusText = $statusText . "#" . formatHashTag($c) . " "; + } + + $status_data = array( + "status" => $statusText, + "visibility" => $visibility, + "language" => $language + ); + + $mastodon -> postStatus($status_data); + } + + + function formatHashTag($category) { + $filtered = str_replace('/', ' ', $category); + $upper = ucwords($filtered); + return str_replace(' ', '', $upper); + } + + + class BlogPost { + var $ts; + var $link; + var $category; + var $title; + } + +?> \ No newline at end of file