59 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Services\Scrapers;
 | 
						|
 | 
						|
use Goutte\Client;
 | 
						|
use Illuminate\Support\Carbon;
 | 
						|
 | 
						|
class VnewsScraper
 | 
						|
{
 | 
						|
    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('.news-content > p')->each(function ($node) {
 | 
						|
            $this->content[] = $node->text();
 | 
						|
        });
 | 
						|
 | 
						|
        if ($crawler->filter('.author')->count() > 0) {
 | 
						|
            $this->author = $crawler->filter('.author')->first()->text();
 | 
						|
        }
 | 
						|
 | 
						|
        $crawler->filter('.news-tags a')->each(function ($node) {
 | 
						|
            $this->topics[] = [
 | 
						|
                "name" => $node->text(),
 | 
						|
                "slug" => str_replace("#", "", str_replace("https://www.vnews.mv/", "", $node->attr('href')))
 | 
						|
            ];
 | 
						|
        });
 | 
						|
 | 
						|
 | 
						|
        //Remove all the alphabets from string
 | 
						|
        //preg_replace("/[a-zA-Z]/", "",$string);
 | 
						|
        return [
 | 
						|
            'source'    => 'VNews',
 | 
						|
            'title'      => $crawler->filter('h1')->first()->text(),
 | 
						|
            'og_title'   => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
 | 
						|
            'image'      => $crawler->filter(".news-image img")->first()->attr('src'),
 | 
						|
            'content'    => $this->content,
 | 
						|
            'url'        => $url,
 | 
						|
            'date'       => Carbon::parse($date)->format("Y-m-d H:i:s"),
 | 
						|
            'guid'       => str_replace("https://www.vnews.mv/", "", $url),
 | 
						|
            'author'     => $this->author,
 | 
						|
            'topics'       =>  $this->topics
 | 
						|
        ];
 | 
						|
    }
 | 
						|
}
 |