Files
karudhaas/app/Services/Scrapers/PsmScraper.php
2020-09-23 02:28:19 +05:00

60 lines
1.7 KiB
PHP

<?php
namespace App\Services\Scrapers;
use Goutte\Client;
class PsmScraper
{
protected $client;
protected $title;
protected $content;
protected $topics = [];
protected $author = "unknown";
public function __construct()
{
$this->client = new Client;
}
public function extract($url, $date)
{
$crawler = $this->client->request('GET', $url);
$crawler->filter('h1')->each(function ($node) {
$this->title = $node->text();
});
$crawler->filter('.content p')->each(function ($node) {
$this->content = explode("<br>",$node->html());
});
if($crawler->filter('div[class*="flex items-center border-grey-light border-solid"] p a')->count() == 1)
{
$this->author = $crawler->filter('div[class*="flex items-center border-grey-light border-solid"] p a')->first()->text();
}
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'PSM',
'title' => $this->title,
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => $crawler->filter("figure img")->first()->attr('src'),
'content' => $this->content,
'url' => $url,
'date' => $date,
'guid' => str_replace("https://www.psmnews.mv/", "", $url),
'author' => $this->author,
'topics' => [
[
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
"slug" => "no-specific-topic"
]
]
];
}
}