Browse Source

update API

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

68
README.md

@ -14,62 +14,32 @@ Twister client communication toolkit @@ -14,62 +14,32 @@ Twister client communication toolkit
### RSS
RSS toolkit for twister
Useful to create twister news bot
###### Init
```
$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.
#### 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; @@ -6,21 +6,12 @@ namespace Twisterarmy\Twister;
class Rss
{
private int $_length = 256;
private string $_format = '{title} {link}';
public function setLength(int $value)
{
$this->_length = $value;
}
public function setFormat(string $value)
{
$this->_format = $value;
}
public function get(string $url, array &$error = []): ?array
public static function feed(
string $url,
string $format = '{title}{nl}{link}',
int $length = 256,
array &$error = []
): ?array
{
if (empty($url))
{
@ -60,13 +51,20 @@ class Rss @@ -60,13 +51,20 @@ class Rss
return null;
}
$messages = [];
$feed = [];
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 link!');
$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 valid link!');
continue;
}
@ -78,6 +76,10 @@ class Rss @@ -78,6 +76,10 @@ class Rss
continue;
}
$pubDate = trim(
(string) $item->pubDate
);
$link = trim(
(string) $item->link
);
@ -92,28 +94,43 @@ class Rss @@ -92,28 +94,43 @@ class Rss
$message = str_replace(
[
'{nl}',
'{link}',
'{title}',
// ..
],
[
PHP_EOL,
$link,
$title,
// ..
],
$this->_format
$format
);
if (mb_strlen($message) > $this->_length)
if (mb_strlen($message) > $length)
{
$error[] = _('Message does not correspond twister protocol length!');
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