Hama news intergration

This commit is contained in:
2020-10-13 23:01:02 +05:00
parent be034116c5
commit fff50a36ac
6 changed files with 205 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Services\Feeds;
use Goutte\Client;
class HamaFeed implements Feed
{
protected $client;
public function __construct()
{
$this->client = new Client();
}
/**
* Get all the latest news
*
* @return array
*/
public function get() : array
{
$crawler = $this->client->request('GET', "https://hama.mv/");
$feeds = [];
$crawler->filter('div[id*="latest"] div[class*="col-md-3 col-6"] a')->each(function ($node) use (&$feeds) {
$feeds[] = [
"title" => $node->filter('h5')->first()->text(),
"link" => $node->attr('href'),
"date" => $node->filter('.datetime')->first()->text()
];
});
return $feeds;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Services;
use App\Services\Feeds\HamaFeed;
use App\Services\Scrapers\HamaScraper;
class HamaService
{
/**
* Scrap all the rss articles from Sun
*
* @return array
*/
public function scrape(): array
{
//Return only the rss that contains "news" keyboard in its url
$articles = (new HamaFeed)->get();
$articlesitems = [];
//Looping through the articles and scraping and while scraping it creates a new instance of the scraper.
foreach ($articles as $article) {
$articlesitems[] = (new HamaScraper)->extract($article["link"], $article["date"]);
}
return $articlesitems;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Services\Scrapers;
use Exception;
use Goutte\Client;
use Illuminate\Support\Carbon;
class HamaScraper
{
protected $client;
protected $title;
protected $content;
protected $author = "unknown";
protected $topics;
public function __construct()
{
$this->client = new Client;
}
public function extract($url, $date)
{
$crawler = $this->client->request('GET', $url);
$crawler->filter('.body > p')->each(function ($node) {
$this->content[] = $node->text();
});
if ($crawler->filter('.author_name')->count() > 0) {
$this->author = $crawler->filter('.author_name')->first()->text();
}
$crawler->filter('.article-tags a')->each(function ($node) {
$this->topics[] = [
"name" => $node->text(),
"slug" => str_replace("https://hama.mv/", "", $node->attr('href'))
];
});
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'Hama',
'title' => $crawler->filter('h1')->first()->text(),
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => $crawler->filter("figure > img")->first()->attr('data-src'),
'content' => $this->content,
'url' => $url,
'date' => Carbon::parse($date)->format("Y-m-d H:i:s"),
'guid' => basename($url),
'author' => $this->author,
'topics' => $this->topics
];
}
}