shahariaazam / news-aggregator
Aggregate news from various sources. Contribute to add your favorite news source too
Requires
- php: >= 7.2
- ext-json: *
- laminas/laminas-diactoros: ^2.2
- monolog/monolog: ^2.0
- pear/net_url2: ^2.2
- php-http/cache-plugin: ^1.7
- php-http/client-implementation: ^1.0
- php-http/curl-client: ^2.1
- php-http/discovery: ^1.0
- php-http/httplug: ^2.0
- php-http/logger-plugin: ^1.1
- php-http/message-factory: ^1.0
- php-http/stopwatch-plugin: ^1.3
- psr/cache: ^1.0
- psr/http-message: ^1.0
- psr/log: ^1.1
- symfony/cache: ^5.0
- symfony/console: ^5.0
- symfony/css-selector: ^5.0
- symfony/dom-crawler: ^5.0
Requires (Dev)
- php-http/mock-client: ^1.0
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-10-23 07:48:24 UTC
README
An universal News Aggregator for PHP developers. Easy, flexible and extendable library to get news headlines and full news article programmatically.
Table of Contents
- Installation
- Usage
- How to extend
- CLI Commands
- Available News Sources
- Issue & Bug Reports
- Contributing
Installation
With Composer
To use this library in your application, you can install this using composer
. Just run the following command.
composer require shahariaazam/news-aggregator
That's it.
Usage
From Application
You can easily get news from supported news provider from your application. Here is a simple code snippet that can return headlines from a specific news provider;
<?php use Shaharia\NewsAggregator\NewsProvider\BBC\BBC; use Shaharia\NewsAggregator\NewsProvider\BBC\HomepageParser; use Shaharia\NewsAggregator\Entity\Headline; require __DIR__ . "/vendor/autoload.php"; $aggregator = Shaharia\NewsAggregator\Aggregator::init(); /** * To get headlines we need to setup news provider and it's appropriate parser class * that will parse that provider's headlines. * * In this example, we are going to fetch all the headlines from BBC's homepage */ $headlines = $aggregator->getHeadlines( BBC::class, // BBC News provider class HomepageParser::class // BBC Homepage news parser class ); var_dump($headlines); // it will return Headline[] entity object
You can find all supported news provider and it's associated parser class from here
Command Line
You can also get news from specific source from your command line. Here is a simple command to read all the headlines from BBC homepage.
./bin/news headlines --list bbc-home
note: bbc-home
is the provider's slug. You can find all supported news provider and it's associated
provider class from here
List of all supported commands, arguments and options are available here
How to extend it?
/** * Customize your aggregator engine with your own libraries. * This library complies with dependency inversion principle. * * Attach your own PSR-7 compatible HTTP client * Attach your request factory, stream factory * Enable Cache with PSR-6 compatible caching library * Logger with PSR-3 compatible Logging client. */ $aggregator = Shaharia\NewsAggregator\Aggregator::init(); $aggregator->setHttpClient($client); $aggregator->setRequestFactory($requestFactory); $aggregator->setStreamFactory($streamFactory); $aggregator->setCache($cache); $aggregator->setLogger($logger);
Docker Image
You can also fetch news from your favorite source or read news from Docker container. To run your Docker container, write the following command -
docker run -it --rm shaharia/news-aggregator news headlines --list {NEWS_PROVIDER_SOURCE_SLUG} --json --with-url
CLI Commands
Here is the list of command line arguments
- ./bin/news headlines --help
Usage:
headlines [options]
Options:
-s, --source=SOURCE Source of the news
-u, --with-url With news URL
-j, --json Output in JSON format
- ./bin/news sources --help
Usage:
sources [options]
Options:
-l, --list List of available news sources
-s, --show=SHOW Show details about specific news source
Available News Sources
BBC
Homepage
- Provider slug:
bbc-home
- URL: https://www.bbc.com/news
- Provider class:
Shaharia\NewsAggregator\NewsProvider\BBC\BBC
- Parser class:
Shaharia\NewsAggregator\NewsProvider\BBC\HomepageParser
- Supported Format:
Headlines
The Daily Prothom Alo
Homepage
- URL: https://www.prothomalo.com/
- Provider class:
Shaharia\NewsAggregator\NewsProvider\ProthomAlo\ProthomAlo
- Parser class:
Shaharia\NewsAggregator\NewsProvider\ProthomAlo\ParserList
- Supported Format:
Headlines
Issue & bug reporting
Please create a new issue from here
Contributing
There are so many news providers available globally. It's not possible for me to create parser for all of them. But no worry, you are very much welcome to contribute.
You can contribute by -
- Adding new News Provider (w/o parser)
- Add new parser
- Participate in the GitHub community in Issue, discussion, etc.
- Create new issue, bug report
- Fix existing issues/bugs
- Help others
List of Contributors
You can see full lists of contributors from here