67 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Services\Scrapers;
 | |
| 
 | |
| use Goutte\Client;
 | |
| use Illuminate\Support\Carbon;
 | |
| 
 | |
| class DhiyaresScraper
 | |
| {
 | |
|     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('.content p')->each(function ($node) {
 | |
|             $this->content[] = $node->text();
 | |
|         });
 | |
| 
 | |
|         $crawler->filter('a[class*="bg-blue-50 border border-blue-50 font-normal text-blue-400 hover:text-white hover:bg-base p-1 px-4 mx-2 mb-4 rounded-full font-mv-bold text-base"]')->each(function ($node) {
 | |
|            if(!preg_match('/[^A-Za-z0-9-]/', basename($node->attr('href'))))
 | |
|            {
 | |
|             $this->topics[] = [
 | |
|                 "name" => $node->text(),
 | |
|                 "slug" => basename($node->attr('href'))
 | |
|             ];
 | |
|            }
 | |
|             
 | |
|         });
 | |
| 
 | |
| 
 | |
|         if ($crawler->filter('div[class*="text-base font-mv-bold"] a[class*="py-4"]')->count() == 1) {
 | |
|             $this->author = $crawler->filter('div[class*="text-base font-mv-bold"] a[class*="py-4"]')->first()->text();
 | |
|         }
 | |
|         //Remove all the alphabets from string
 | |
|         //preg_replace("/[a-zA-Z]/", "",$string);
 | |
|         return [
 | |
|             'source'    => 'Dhiyares',
 | |
|             'title'      => $crawler->filter('h1')->first()->text(),
 | |
|             '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'       => Carbon::parse($date)->format("Y-m-d H:i:s"),
 | |
|             'guid'       =>basename($url),
 | |
|             'author'     => $this->author,
 | |
|             'topics'       => $this->topics ? : [
 | |
|                 [
 | |
|                     "name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
 | |
|                     "slug" => "no-specific-topic"
 | |
|                 ]
 | |
|             ]
 | |
|         ];
 | |
|     }
 | |
| }
 |