Files
karudhaas/app/Services/Scrapers/ThiladhunScraper.php

91 lines
2.4 KiB
PHP

<?php
namespace App\Services\Scrapers;
use Goutte\Client;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
class ThiladhunScraper
{
protected $client;
protected $title;
protected $content;
protected $image;
protected $author;
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('h1')->each(function ($node) {
$this->title = $node->text();
});
$crawler->filter('div.single-body.entry-content.typography-copy p')->each(function ($node) {
$this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());;
});
$crawler->filter('div[class*="entry-thumb single-entry-thumb"] img')->each(function ($node) {
$this->image = $node->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[class*="entry-author__name"]')->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' => (integer) Str::before(basename($url), '?'),
'topics' => $this->topics ? : [
[
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
"slug" => "no-specific-topic"
]
]
];
}
}