Apply fixes from StyleCI

This commit is contained in:
Gabriele Grillo 2020-04-10 19:54:59 +00:00 committed by StyleCI Bot
parent e953ea39ad
commit 55f96640fd
4 changed files with 218 additions and 214 deletions

View File

@ -1,41 +1,46 @@
<?php <?php
class MastodonAPI {
private $token;
private $instance_url;
public function __construct($token, $instance_url) {
$this->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) { class MastodonAPI
return json_encode(['ok'=>false, 'curl_error_code' => curl_errno($ch_status), 'curl_error' => curl_error(ch_status)]); {
} private $token;
curl_close ($ch); private $instance_url;
return json_decode($reply, true);
} public function __construct($token, $instance_url)
} {
?> $this->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);
}
}

View File

@ -1,32 +1,30 @@
<?php <?php
include_once('Mastodon.php');
include_once 'Mastodon.php';
$token="XXXXX"; // Token of your Mastodon welcome bot account
$base_url="https://botsin.space"; // URL of your instance (Do not include '/' at the end.) $token = 'XXXXX'; // Token of your Mastodon welcome bot account
$visibility="private"; // "Direct" means sending welcome message as a private message. The four tiers of visibility for toots are Public , Unlisted, Private, and Direct (default) $base_url = 'https://botsin.space'; // URL of your instance (Do not include '/' at the end.)
$language="en"; // en for English, zh for Chinese, etc. $visibility = 'private'; // "Direct" means sending welcome message as a private message. The four tiers of visibility for toots are Public , Unlisted, Private, and Direct (default)
$language = 'en'; // en for English, zh for Chinese, etc.
$mastodon = new MastodonAPI($token, $base_url); $mastodon = new MastodonAPI($token, $base_url);
$curl_file = curl_file_create('./imageOnServer.jpg','image/jpg','imagename.jpg'); $curl_file = curl_file_create('./imageOnServer.jpg', 'image/jpg', 'imagename.jpg');
$body = [ $body = [
'file' => $curl_file 'file' => $curl_file,
]; ];
$response = $mastodon -> uploadMedia($body); $response = $mastodon->uploadMedia($body);
$file_id = $response->id; $file_id = $response->id;
$statusText = "This is a status"; $statusText = 'This is a status';
$status_data = array( $status_data = [
"status" => $statusText, 'status' => $statusText,
"visibility" => $visibility, 'visibility' => $visibility,
"language" => $language, 'language' => $language,
"media_ids[]" => $file_id 'media_ids[]' => $file_id,
); ];
$mastodon -> postStatus($status_data); $mastodon->postStatus($status_data);
?>

View File

@ -1,77 +1,77 @@
<?php <?php
include_once('Mastodon.php');
include_once 'Mastodon.php';
$token="XXXXX"; // Token of your Mastodon bot account
$base_url="https://botsin.space"; // URL of your instance (Do not include '/' at the end.) $token = 'XXXXX'; // Token of your Mastodon bot account
$visibility="private"; // "Direct" means sending welcome message as a private message. The four tiers of visibility for toots are Public , Unlisted, Private, and Direct (default) $base_url = 'https://botsin.space'; // URL of your instance (Do not include '/' at the end.)
$language="en"; // en for English, zh for Chinese, etc. $visibility = 'private'; // "Direct" means sending welcome message as a private message. The four tiers of visibility for toots are Public , Unlisted, Private, and Direct (default)
$feed_url = "https://www.reddit.com/r/Subreddit.json"; // the subreddit url ending in *.json $language = 'en'; // en for English, zh for Chinese, etc.
$file = 'dataSubreddit.txt'; $feed_url = 'https://www.reddit.com/r/Subreddit.json'; // the subreddit url ending in *.json
$tagline = " check out on "; $file = 'dataSubreddit.txt';
$tagline = ' check out on ';
if (!($jsonTxt = file_get_contents($feed_url)))
if (!($jsonTxt = file_get_contents($feed_url))) {
die('Error loading the feed url'); die('Error loading the feed url');
}
$json = json_decode($jsonTxt, true);
$first_article = false;
$lastDate = ''; $json = json_decode($jsonTxt, true);
$first_article = false;
if($f = fopen($file, 'r')){ $lastDate = '';
$lastDate = fgets($f);
fclose($f);
}
$statuses = array();
foreach ($json["data"]["children"] as $item) { if ($f = fopen($file, 'r')) {
$itemData = $item["data"]; $lastDate = fgets($f);
$ts = $itemData["created_utc"]; fclose($f);
if (($lastDate == "" || $ts > $lastDate) && !$itemData["is_self"]) { }
$post = new BlogPost();
$post->link = $itemData["url"]; $statuses = [];
$post->title = $itemData["title"];
$post->category = $itemData["subreddit"]; foreach ($json['data']['children'] as $item) {
$post->nsfw = $itemData["over_18"]; $itemData = $item['data'];
$ts = $itemData['created_utc'];
array_push($statuses, $post); if (($lastDate == '' || $ts > $lastDate) && !$itemData['is_self']) {
if (!$first_article) { $post = new BlogPost();
$myfile = fopen($file, "w") or die("Unable to open file!"); $post->link = $itemData['url'];
fwrite($myfile, $ts); $post->title = $itemData['title'];
fclose($myfile); $post->category = $itemData['subreddit'];
$first_article = true; $post->nsfw = $itemData['over_18'];
}
} array_push($statuses, $post);
} if (!$first_article) {
$myfile = fopen($file, 'w') or die('Unable to open file!');
$statuses = array_reverse($statuses); fwrite($myfile, $ts);
$mastodon = new MastodonAPI($token, $base_url); fclose($myfile);
$first_article = true;
foreach ($statuses as $s) { }
$status_data = array( }
"visibility" => $visibility, }
"language" => $language
); $statuses = array_reverse($statuses);
$mastodon = new MastodonAPI($token, $base_url);
$statusText = $s->title . $tagline . $s->link . " ";
foreach ($statuses as $s) {
if ($s->nsfw) { $status_data = [
$status_data["status"] = "NSFW"; 'visibility' => $visibility,
$status_data["spoiler_text"] = $statusText; 'language' => $language,
} else { ];
$status_data["status"] = $statusText;
} $statusText = $s->title.$tagline.$s->link.' ';
$mastodon -> postStatus($status_data); if ($s->nsfw) {
} $status_data['status'] = 'NSFW';
$status_data['spoiler_text'] = $statusText;
} else {
class BlogPost { $status_data['status'] = $statusText;
var $ts; }
var $link;
var $category; $mastodon->postStatus($status_data);
var $title; }
var $nsfw;
} class BlogPost
{
?> public $ts;
public $link;
public $category;
public $title;
public $nsfw;
}

View File

@ -1,78 +1,79 @@
<?php <?php
include_once('Mastodon.php');
include_once 'Mastodon.php';
$token="XXXXX"; // Token of your Mastodon welcome bot account
$base_url="https://botsin.space"; // URL of your instance (Do not include '/' at the end.) $token = 'XXXXX'; // Token of your Mastodon welcome bot account
$visibility="private"; // "Direct" means sending welcome message as a private message. The four tiers of visibility for toots are Public , Unlisted, Private, and Direct (default) $base_url = 'https://botsin.space'; // URL of your instance (Do not include '/' at the end.)
$language="en"; // en for English, zh for Chinese, etc. $visibility = 'private'; // "Direct" means sending welcome message as a private message. The four tiers of visibility for toots are Public , Unlisted, Private, and Direct (default)
$feed_url = "https://wordpressblog.com/feed/"; $language = 'en'; // en for English, zh for Chinese, etc.
$file = 'data.txt'; $feed_url = 'https://wordpressblog.com/feed/';
$file = 'data.txt';
if (!($rss = simplexml_load_file($feed_url)))
if (!($rss = simplexml_load_file($feed_url))) {
die('Error loading the feed url'); die('Error loading the feed url');
}
$first_article = false;
$lastDate = ''; $first_article = false;
if($f = fopen($file, 'r')){ $lastDate = '';
$lastDate = fgets($f);
fclose($f);
}
$statuses = array();
foreach ($rss->channel->item as $item) { if ($f = fopen($file, 'r')) {
$ts = strtotime($item->pubDate); $lastDate = fgets($f);
if ($lastDate == "" || $ts > $lastDate) { fclose($f);
$post = new BlogPost(); }
$post->link = (string) $item->link;
$post->title = (string) $item->title;
$post->category = $item->category;
array_push($statuses, $post); $statuses = [];
if (!$first_article) {
$myfile = fopen($file, "w") or die("Unable to open file!"); foreach ($rss->channel->item as $item) {
fwrite($myfile, $ts); $ts = strtotime($item->pubDate);
fclose($myfile); if ($lastDate == '' || $ts > $lastDate) {
$first_article = true; $post = new BlogPost();
} $post->link = (string) $item->link;
} $post->title = (string) $item->title;
}
$post->category = $item->category;
$statuses = array_reverse($statuses);
$mastodon = new MastodonAPI($token, $base_url); array_push($statuses, $post);
if (!$first_article) {
foreach ($statuses as $s) { $myfile = fopen($file, 'w') or die('Unable to open file!');
$statusText = $s->title . " leggi su " . $s->link . " "; fwrite($myfile, $ts);
fclose($myfile);
foreach ($s->category as $c) { $first_article = true;
$statusText = $statusText . "#" . formatHashTag($c) . " "; }
} }
}
$status_data = array(
"status" => $statusText, $statuses = array_reverse($statuses);
"visibility" => $visibility, $mastodon = new MastodonAPI($token, $base_url);
"language" => $language
); foreach ($statuses as $s) {
$statusText = $s->title.' leggi su '.$s->link.' ';
$mastodon -> postStatus($status_data);
} foreach ($s->category as $c) {
$statusText = $statusText.'#'.formatHashTag($c).' ';
}
function formatHashTag($category) {
$filtered = str_replace('/', ' ', $category); $status_data = [
$upper = ucwords($filtered); 'status' => $statusText,
return str_replace(' ', '', $upper); 'visibility' => $visibility,
} 'language' => $language,
];
class BlogPost { $mastodon->postStatus($status_data);
var $ts; }
var $link;
var $category; function formatHashTag($category)
var $title; {
} $filtered = str_replace('/', ' ', $category);
$upper = ucwords($filtered);
?>
return str_replace(' ', '', $upper);
}
class BlogPost
{
public $ts;
public $link;
public $category;
public $title;
}