Hama news intergration
This commit is contained in:
39
app/Services/Feeds/HamaFeed.php
Normal file
39
app/Services/Feeds/HamaFeed.php
Normal 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;
|
||||
|
||||
}
|
||||
}
|
||||
27
app/Services/HamaService.php
Normal file
27
app/Services/HamaService.php
Normal 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;
|
||||
}
|
||||
}
|
||||
59
app/Services/Scrapers/HamaScraper.php
Normal file
59
app/Services/Scrapers/HamaScraper.php
Normal 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
|
||||
];
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user