Miadhu News Support

This commit is contained in:
2020-10-03 03:21:34 +05:00
parent 3b50c8058f
commit cf4e6267b6
5 changed files with 200 additions and 1 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Services\Feeds;
use Goutte\Client;
class MiadhuFeed implements Feed
{
protected $client;
public function __construct()
{
$this->client = new Client();
}
/**
* Return the latest articles from avas
*
* @return array
*/
public function get(): array
{
$crawler = $this->client->request('GET', "https://miadhu.mv");
$feeds = [];
$dates = [];
// scrape the dates for the articles
$crawler->filter('.middle div[class*="col-md-3 col-6 news-block"] em')->each(function ($node) use (&$dates) {
$dates[] = $node->text();
});
$crawler->filter('.middle div[class*="col-md-3 col-6 news-block"] h2 a')->each(function ($node, $i) use (&$feeds, $dates) {
$feeds[] = [
"title" => $node->text(),
"link" => $node->attr('href'),
"date" => $dates[$i]
];
});
return $feeds;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Services;
use App\Services\Feeds\MiadhuFeed;
use App\Services\Scrapers\MiadhuScraper;
class MiadhuService
{
/**
* 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 MiadhuFeed)->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 MiadhuScraper)->extract($article["link"], $article["date"]);
}
return $articlesitems;
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Services\Scrapers;
use Goutte\Client;
use Illuminate\Support\Carbon;
class MiadhuScraper
{
protected $client;
protected $title;
protected $content;
protected $author = "unknown";
public function __construct()
{
$this->client = new Client;
}
public function extract($url, $date)
{
$crawler = $this->client->request('GET', $url);
$crawler->filter('.read-dv-text > p')->each(function ($node) {
$this->content[] = $node->text();
});
if ($crawler->filter('.author-name')->count() == 1) {
$this->author = $crawler->filter('.author-name')->first()->text();
}
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'Miadhu News',
'title' => $crawler->filter('h1')->first()->text(),
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => $crawler->filter(".col-md-12 img")->first()->attr('src'),
'content' => $this->content,
'url' => $url,
'date' => Carbon::parse($date)->format("Y-m-d H:i:s"),
'guid' => str_replace("https://miadhu.mv/article/read/", "", $url),
'author' => $this->author,
'topics' => [
[
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
"slug" => "no-specific-topic"
]
]
];
}
}