-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* `./bin/feedio` doesn't need Symfony anymore * Removed useless stuff * New Lint rules applied
- Loading branch information
1 parent
0c2c924
commit 7f9e3c7
Showing
14 changed files
with
1,854 additions
and
2,609 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,69 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
foreach( [__DIR__.'/../vendor/autoload.php', __DIR__.'/../../../autoload.php'] as $file ) { | ||
if ( file_exists($file) ) { | ||
require $file; | ||
} | ||
} | ||
|
||
use Symfony\Component\Console\Application; | ||
|
||
if ( class_exists('Symfony\Component\Console\Application') ) { | ||
$application = new Application(); | ||
$application->setName('feed-io : the CLI feed reader'); | ||
use FeedIo\FeedIo; | ||
|
||
$application->add(new \FeedIo\Command\CheckCommand()); | ||
$application->add(new \FeedIo\Command\ReadCommand()); | ||
$application->add(new \FeedIo\Command\DiscoverCommand()); | ||
$application->run(); | ||
} else { | ||
main($argc, $argv); | ||
exit; | ||
$file = __DIR__.'/../vendor/autoload.php'; | ||
if ( file_exists($file) ) { | ||
require $file; | ||
} | ||
|
||
/** | ||
* This function is invoked if symfony/console is not installed | ||
*/ | ||
function main($argc, $argv) { | ||
if ( $argc < 2 ) { | ||
echo 'feed-io version 2.4' . PHP_EOL; | ||
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 feed-io [url]" . PHP_EOL; | ||
echo "\t feedio read|discover [url]" . PHP_EOL; | ||
exit; | ||
} | ||
|
||
$feedIo = \FeedIo\Factory::create()->getFeedIo(); | ||
$feed = array_reverse(iterator_to_array($feedIo->read($argv[$argc-1])->getFeed())); | ||
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(); | ||
|
||
foreach( $feed as $i => $item ) { | ||
echo "\033[32m{$item->getLastModified()->format(\DateTime::ATOM)} : \033[34m{$item->getTitle()}\033[0m"; | ||
echo strip_tags(nl2br($item->getDescription())) . PHP_EOL; | ||
$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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.