shahariaazam/news-aggregator

There is no license information available for the latest version (dev-development) of this package.

Aggregate news from various sources. Contribute to add your favorite news source too

dev-development 2020-05-22 22:25 UTC

This package is auto-updated.

Last update: 2024-11-23 08:01:48 UTC


README

CI Code Coverage Code Quality

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

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