69 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Services\Scrapers;
 | 
						|
 | 
						|
use Goutte\Client;
 | 
						|
use Illuminate\Support\Carbon;
 | 
						|
 | 
						|
class DhenScraper
 | 
						|
{
 | 
						|
    protected $client;
 | 
						|
 | 
						|
    protected $content;
 | 
						|
    protected $author;
 | 
						|
 | 
						|
    public function __construct()
 | 
						|
    {
 | 
						|
        $this->client = new Client;
 | 
						|
    }
 | 
						|
 | 
						|
    public function extract($url)
 | 
						|
    {
 | 
						|
 | 
						|
        $crawler = $this->client->request('GET', $url);
 | 
						|
 | 
						|
        $title = $crawler->filter('h1')->first()->text();
 | 
						|
 | 
						|
        if ($crawler->filter('article .article-visual img')->count() == 1) {
 | 
						|
            $image = $crawler->filter('article .article-visual img')->first()->attr('src');
 | 
						|
        }
 | 
						|
 | 
						|
        if ($crawler->filter('article .article-entry p')->count() > 0) {
 | 
						|
            $crawler->filter('article .article-entry p')->each(function ($node) {
 | 
						|
                $this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());
 | 
						|
            });
 | 
						|
        } else {
 | 
						|
            $crawler->filter('.im_message_body .im_message_text')->each(function ($node) {
 | 
						|
                $this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());
 | 
						|
            });
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
        if ($crawler->filter(".story-item--meta a")->count() == 1) {
 | 
						|
            $this->author = $crawler->filter('.story-item--meta a')->first()->text();
 | 
						|
        }
 | 
						|
 | 
						|
        //Remove all the alphabets from string
 | 
						|
        //preg_replace("/[a-zA-Z]/", "",$string);
 | 
						|
        return [
 | 
						|
            'source'    => 'Dhen',
 | 
						|
            'title'      => $title,
 | 
						|
            'og_title'   => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
 | 
						|
            'image'      => $image ?? "images/noimg.jpg",
 | 
						|
            'content'    => $this->content,
 | 
						|
            'url'        => $url,
 | 
						|
            'date'       =>  Carbon::parse(str_replace("- ", "", $crawler->filter('.story-item--meta time')->first()->text()))->format("Y-m-d H:i:s"),
 | 
						|
            'guid'       => str_replace("https://dhen.mv/", "", $url),
 | 
						|
            'author'     => $this->author,
 | 
						|
            'topics'       =>  [
 | 
						|
                [
 | 
						|
                    "name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
 | 
						|
                    "slug" => "no-specific-topic"
 | 
						|
                ]
 | 
						|
            ]
 | 
						|
        ];
 | 
						|
    }
 | 
						|
}
 |