mirror of
https://github.com/twisterarmy/twister-php.git
synced 2025-01-30 08:24:25 +00:00
update API
This commit is contained in:
parent
3f2d7d8dcb
commit
75695d73ad
76
README.md
76
README.md
@ -14,62 +14,32 @@ Twister client communication toolkit
|
||||
|
||||
### RSS
|
||||
|
||||
RSS toolkit for twister
|
||||
|
||||
###### 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.
|
||||
|
||||
##### Example
|
||||
|
||||
```
|
||||
$rss->setLength(256);
|
||||
```
|
||||
Useful to create twister news bot
|
||||
|
||||
#### Feed
|
||||
|
||||
Get formatted feed array
|
||||
|
||||
##### Example
|
||||
Read remote URL and convert response to formatted twister messages
|
||||
|
||||
```
|
||||
$feed = $rss->get(url);
|
||||
$array = \Twisterarmy\Twister\Rss::feed('url');
|
||||
```
|
||||
|
||||
##### Attributes
|
||||
|
||||
* `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
|
||||
|
||||
##### Result
|
||||
|
||||
```
|
||||
[
|
||||
time: int,
|
||||
message: string
|
||||
],
|
||||
...
|
||||
```
|
61
src/Rss.php
61
src/Rss.php
@ -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
|
||||
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
|
||||
continue;
|
||||
}
|
||||
|
||||
$pubDate = trim(
|
||||
(string) $item->pubDate
|
||||
);
|
||||
|
||||
$link = trim(
|
||||
(string) $item->link
|
||||
);
|
||||
@ -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…
x
Reference in New Issue
Block a user