bin/feedio
#!/usr/bin/env php
<?php
use FeedIo\FeedIo;
use Http\Discovery\HttpClientDiscovery;
foreach( [__DIR__.'/../vendor/autoload.php', __DIR__.'/../../../autoload.php'] as $file ) {
if ( file_exists($file) ) {
require $file;
}
}
main($argc, $argv);
function main($argc, $argv)
{
if ( $argc < 3 || !in_array($argv[1], ['read', 'discover']) ) {
echo 'feed-io: the CLI feed reader' . PHP_EOL;
echo 'Usage : ' . PHP_EOL;
echo "\t feedio read|discover [url]" . PHP_EOL;
exit;
}
$psrClient = HttpClientDiscovery::find();
$feedIo = new FeedIo(new \FeedIo\Adapter\Http\Client($psrClient));
switch($argv[1]) {
case 'discover':
discover($feedIo, $argv[2]);
break;
default:
read($feedIo, $argv[2]);
break;
}
}
function read(FeedIo $feedIo, string $url)
{
$result = $feedIo->read($url);
$feed = $result->getFeed();
$items = array_reverse(iterator_to_array($feed));
foreach( $items as $i => $item ) {
echo "\033[32m{$item->getLastModified()->format(\DateTime::ATOM)} - \033[34m{$item->getTitle()}\033[0m:" . PHP_EOL;
echo "\t". strip_tags(nl2br($item->getContent())) . PHP_EOL;
}
$nextUpdate = $result->getNextUpdate();
echo "\033[32mNext time a new item may be published : \033[34m{$nextUpdate->format(\DATE_ATOM)}\033[0m" . PHP_EOL;
$updateStats = $result->getUpdateStats();
echo "\033[32mMinimum interval between items : \033[34m".formatDateInterval($updateStats->getMedianInterval())."\033[0m" . PHP_EOL;
echo "\033[32mMedian interval : \033[34m".formatDateInterval($updateStats->getMedianInterval())."\033[0m" . PHP_EOL;
echo "\033[32mAverage interval : \033[34m".formatDateInterval($updateStats->getAverageInterval())."\033[0m" . PHP_EOL;
echo "\033[32mMaximum interval : \033[34m".formatDateInterval($updateStats->getMaxInterval())."\033[0m". PHP_EOL;
}
function discover(FeedIo $feedIo, string $url)
{
$urls = $feedIo->discover($url);
foreach( $urls as $i => $url ) {
echo "\033[32m{$i} : \033[34m{$url}\033[0m" . PHP_EOL;
}
}
function formatDateInterval(int $interval): string
{
$zero = new \DateTime('@0');
$diff = $zero->diff(new \DateTime("@{$interval}"));
return $diff->format('%a days, %h hours, %i minutes, %s seconds');
}