Files
karudhaas/app/Services/Scrapers/HamaScraper.php
2020-10-13 23:01:02 +05:00

60 lines
1.8 KiB
PHP

<?php
namespace App\Services\Scrapers;
use Exception;
use Goutte\Client;
use Illuminate\Support\Carbon;
class HamaScraper
{
protected $client;
protected $title;
protected $content;
protected $author = "unknown";
protected $topics;
public function __construct()
{
$this->client = new Client;
}
public function extract($url, $date)
{
$crawler = $this->client->request('GET', $url);
$crawler->filter('.body > p')->each(function ($node) {
$this->content[] = $node->text();
});
if ($crawler->filter('.author_name')->count() > 0) {
$this->author = $crawler->filter('.author_name')->first()->text();
}
$crawler->filter('.article-tags a')->each(function ($node) {
$this->topics[] = [
"name" => $node->text(),
"slug" => str_replace("https://hama.mv/", "", $node->attr('href'))
];
});
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'Hama',
'title' => $crawler->filter('h1')->first()->text(),
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => $crawler->filter("figure > img")->first()->attr('data-src'),
'content' => $this->content,
'url' => $url,
'date' => Carbon::parse($date)->format("Y-m-d H:i:s"),
'guid' => basename($url),
'author' => $this->author,
'topics' => $this->topics
];
}
}