Files
karudhaas/app/Services/Scrapers/ThiladhunScraper.php
2024-01-06 15:37:59 +05:00

88 lines
2.3 KiB
PHP

<?php
namespace App\Services\Scrapers;
use Goutte\Client;
use Illuminate\Support\Carbon;
class ThiladhunScraper
{
protected $client;
protected $title;
protected $content;
protected $image;
protected $author = "unknown";
protected $topics = [];
/**
* __construct.
*
* @return void
*/
public function __construct()
{
$this->client = new Client();
}
/**
* extract.
*
* @param mixed $url
* @param mixed $date
* @param mixed $guid
*
* @return array
*/
public function extract($url, $date)
{
$crawler = $this->client->request('GET', $url);
$crawler->filter('meta[property="og:title"]')->each(function ($node) {
$this->title = $node->attr('content');
});
$crawler->filter('div.elementor-widget-container > p')->each(function ($node) {
$this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());;
});
$this->image = $crawler->filter('div[data-widget_type*="theme-post-featured-image.default"] figure img')->first()->attr('src');
$crawler->filter('.entry-tags a')->each(function ($node) {
if(!preg_match('/[^A-Za-z0-9-]/', basename($node->attr('href'))))
{
$this->topics[] = [
"name" => $node->text(),
"slug" => basename($node->attr('href'))
];
}
});
$crawler->filter('a > .elementor-post-info__item--type-author')->each(function ($node) {
$this->author = $node->text();
});
return [
'source' => 'Thiladhun News',
'title' => $this->title,
'og_title' => str_replace(" | Thiladhun", "", $crawler->filter('title')->first()->text('content')),
'image' => $this->image,
'content' => $this->content,
'date' => Carbon::parse($date)->format("Y-m-d H:i:s"),
'url' => $url,
'author' => $this->author,
'guid' => basename($url),
'topics' => $this->topics ? : [
[
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
"slug" => "no-specific-topic"
]
]
];
}
}