Browse Source

update API

main
ghost 11 months ago
parent
commit
75695d73ad
  1. 66
      README.md
  2. 61
      src/Rss.php

66
README.md

@ -14,62 +14,32 @@ Twister client communication toolkit
### RSS ### RSS
RSS toolkit for twister Useful to create twister news bot
###### Init #### Feed
```
$rss = new \Twisterarmy\Twister\Rss();
```
#### Format
##### Time
Convert RSS time to datetime format, `U` by default
[Documentation](https://www.php.net/manual/en/datetime.format.php)
###### Example
```
$rss->setTimeFormat('c');
```
##### Message
Convert RSS fields to twister message format, `{title} {link}` by default
###### Mask
* `{time}` - formatted time string by `setTimeFormat`
* `{link}` - target link
* `{title}` - item title
###### Example
```
$rss->setMessageFormat('{title} {link}');
```
#### Length
Twister protocol accept messages with 256 chars max but you can define another value.
Formatted messages greater this value will be skipped from feed.
##### Example Read remote URL and convert response to formatted twister messages
``` ```
$rss->setLength(256); $array = \Twisterarmy\Twister\Rss::feed('url');
``` ```
#### Feed ##### Attributes
Get formatted feed array * `url` - feed address
* `format` - `{title} {link}` by default
+ `{nl}` - new line
+ `{link}` - target link
+ `{title}` - item title
* `length` - `256` by default
* `errors` - array of errors
##### Example ##### Result
``` ```
$feed = $rss->get(url); [
time: int,
message: string
],
...
``` ```

61
src/Rss.php

@ -6,21 +6,12 @@ namespace Twisterarmy\Twister;
class Rss class Rss
{ {
private int $_length = 256; public static function feed(
string $url,
private string $_format = '{title} {link}'; string $format = '{title}{nl}{link}',
int $length = 256,
public function setLength(int $value) array &$error = []
{ ): ?array
$this->_length = $value;
}
public function setFormat(string $value)
{
$this->_format = $value;
}
public function get(string $url, array &$error = []): ?array
{ {
if (empty($url)) if (empty($url))
{ {
@ -60,13 +51,20 @@ class Rss
return null; return null;
} }
$messages = []; $feed = [];
foreach ($xml->channel->item as $item) foreach ($xml->channel->item as $item)
{ {
if (empty($item->link)) if (empty($item->pubDate) || !strtotime((string) $item->pubDate))
{
$error[] = _('RSS channel item does not contain valid pubDate!');
continue;
}
if (empty($item->link) || false === (bool) filter_var((string) $item->link, FILTER_VALIDATE_URL))
{ {
$error[] = _('RSS channel item does not contain link!'); $error[] = _('RSS channel item does not contain valid link!');
continue; continue;
} }
@ -78,6 +76,10 @@ class Rss
continue; continue;
} }
$pubDate = trim(
(string) $item->pubDate
);
$link = trim( $link = trim(
(string) $item->link (string) $item->link
); );
@ -92,28 +94,43 @@ class Rss
$message = str_replace( $message = str_replace(
[ [
'{nl}',
'{link}', '{link}',
'{title}', '{title}',
// .. // ..
], ],
[ [
PHP_EOL,
$link, $link,
$title, $title,
// .. // ..
], ],
$this->_format $format
); );
if (mb_strlen($message) > $this->_length) if (mb_strlen($message) > $length)
{ {
$error[] = _('Message does not correspond twister protocol length!'); $error[] = _('Message does not correspond twister protocol length!');
continue; continue;
} }
$messages[] = $message; $feed[] =
[
'time' => strtotime($pubDate),
'message' => $message
];
} }
return $messages; array_multisort(
array_column(
$feed,
'time'
),
SORT_ASC,
$feed
);
return $feed;
} }
} }
Loading…
Cancel
Save