Dhuvas intergrated
This commit is contained in:
29
app/Services/DhuvasService.php
Normal file
29
app/Services/DhuvasService.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Services\Scrapers\DhuvasScraper;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class DhuvasService extends Client
|
||||
{
|
||||
/**
|
||||
* Scrap all the rss articles from Press
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function scrape(): array
|
||||
{
|
||||
//Return only the rss that contains "news" keyboard in its url
|
||||
$articles = $this->get("https://dhuvas.mv/feed")["channel"]["item"];
|
||||
$articlesitems = [];
|
||||
//Looping through the articles and scraping and while scraping it creates a new instance of the scraper.
|
||||
foreach ($articles as $article) {
|
||||
$link = $article['link'];
|
||||
$date = $article['pubDate'];
|
||||
$articlesitems[] = (new DhuvasScraper)->extract($link,$date);
|
||||
}
|
||||
|
||||
return $articlesitems;
|
||||
}
|
||||
}
|
@@ -50,10 +50,10 @@ class DhenScraper
|
||||
'date' => Carbon::parse(str_replace("- ", "", $crawler->filter('.story-item--meta time')->first()->text()))->format("Y-m-d H:i:s"),
|
||||
'guid' => str_replace("https://dhen.mv/","",$url),
|
||||
'author' => $this->author,
|
||||
'topics' => [
|
||||
'topics' => [
|
||||
[
|
||||
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
|
||||
"slug" => "uncategorized"
|
||||
"slug" => "no-specific-topic"
|
||||
]
|
||||
]
|
||||
];
|
||||
|
62
app/Services/Scrapers/DhuvasScraper.php
Normal file
62
app/Services/Scrapers/DhuvasScraper.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Scrapers;
|
||||
|
||||
use Goutte\Client;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class DhuvasScraper
|
||||
{
|
||||
protected $client;
|
||||
|
||||
protected $title;
|
||||
protected $content;
|
||||
protected $author;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client;
|
||||
}
|
||||
|
||||
public function extract($url, $date = null)
|
||||
{
|
||||
|
||||
$crawler = $this->client->request('GET', $url);
|
||||
|
||||
$crawler->filter('h1')->each(function ($node) {
|
||||
$this->title = $node->text();
|
||||
});
|
||||
|
||||
$crawler->filter('.elementor-widget-container > p')->each(function ($node) {
|
||||
$this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
|
||||
});
|
||||
|
||||
if($crawler->filter('span[class*="elementor-icon-list-text elementor-post-info__item elementor-post-info__item--type-author"]')->count() == 1)
|
||||
{
|
||||
$this->author = $crawler->filter('span[class*="elementor-icon-list-text elementor-post-info__item elementor-post-info__item--type-author"]')->first()->text();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//Remove all the alphabets from string
|
||||
//preg_replace("/[a-zA-Z]/", "",$string);
|
||||
return [
|
||||
'source' => 'Dhuvas',
|
||||
'title' => $this->title,
|
||||
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
|
||||
'image' => $crawler->filter('.elementor-widget-theme-post-featured-image .elementor-image a img')->first()->attr('src'),
|
||||
'content' => $this->content,
|
||||
'url' => $url,
|
||||
'date' => Carbon::parse($date)->format("Y-m-d H:i:s"),
|
||||
'guid' => str_replace("https://dhuvas.mv/","",$url),
|
||||
'author' => $this->author,
|
||||
'topics' => [
|
||||
[
|
||||
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
|
||||
"slug" => "no-specific-topic"
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user