Create SunScraper.php
This commit is contained in:
		
							
								
								
									
										57
									
								
								app/Services/Scrapers/SunScraper.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								app/Services/Scrapers/SunScraper.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,57 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Services\Scrapers;
 | 
			
		||||
 | 
			
		||||
use Goutte\Client;
 | 
			
		||||
 | 
			
		||||
class SunScraper
 | 
			
		||||
{
 | 
			
		||||
    protected $client;
 | 
			
		||||
 | 
			
		||||
    protected $title;
 | 
			
		||||
    protected $content;
 | 
			
		||||
    protected $topics = [];
 | 
			
		||||
    protected $author;
 | 
			
		||||
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $this->client = new Client;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function extract($url)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $crawler = $this->client->request('GET', $url);
 | 
			
		||||
 | 
			
		||||
        $crawler->filter('h1')->each(function ($node) {
 | 
			
		||||
            $this->title = $node->text();
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $crawler->filter('.component-article-content p')->each(function ($node) {
 | 
			
		||||
            $this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $crawler->filter('.component-article-tag li a')->each(function ($node) {
 | 
			
		||||
           
 | 
			
		||||
            $this->topics[] = [
 | 
			
		||||
                "name" => $node->first()->text(),
 | 
			
		||||
                "slug" => str_replace("https://sun.mv/", "", $node->first()->attr('href'))
 | 
			
		||||
            ];
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        //Remove all the alphabets from string
 | 
			
		||||
        //preg_replace("/[a-zA-Z]/", "",$string);
 | 
			
		||||
       return [
 | 
			
		||||
            'source'    => 'Sun',
 | 
			
		||||
            'title'      => $this->title,
 | 
			
		||||
            'og_title'   => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
 | 
			
		||||
            'image'      => $crawler->filter(".component-article-featured img")->first()->attr('src'),
 | 
			
		||||
            'content'    => $this->content,
 | 
			
		||||
            'url'        => $url,
 | 
			
		||||
            'date'       => $crawler->filter(".author .time")->first()->text(),
 | 
			
		||||
            'guid'       => str_replace("https://sun.mv/","",$url),
 | 
			
		||||
            'author'     => $crawler->filter(".author .name")->first()->text(),
 | 
			
		||||
            'topics'       => collect($this->topics)->unique()->values()->toArray()
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user