95 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Services\Scrapers;
 | |
| 
 | |
| use Goutte\Client;
 | |
| use Illuminate\Support\Carbon;
 | |
| 
 | |
| class ThiladhunScraper
 | |
| {
 | |
|     protected $client;
 | |
| 
 | |
|     protected $title;
 | |
|     protected $content;
 | |
|     protected $image;
 | |
|     protected $author = "unknown";
 | |
|     protected $topics = [];
 | |
| 
 | |
|     /**
 | |
|      * __construct.
 | |
|      *
 | |
|      * @return void
 | |
|      */
 | |
|     public function __construct()
 | |
|     {
 | |
|         $this->client = new Client();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * extract.
 | |
|      *
 | |
|      * @param mixed $url
 | |
|      * @param mixed $date
 | |
|      * @param mixed $guid
 | |
|      *
 | |
|      * @return array
 | |
|      */
 | |
|     public function extract($url)
 | |
|     {
 | |
| 
 | |
|         if (strpos($url, 'gallery') !== false) {
 | |
|             return null;
 | |
|         }
 | |
| 
 | |
|         $crawler = $this->client->request('GET', $url);
 | |
| 
 | |
|         $crawler->filter('meta[property="og:title"]')->each(function ($node) {
 | |
|             $this->title = $node->attr('content');
 | |
|         });
 | |
| 
 | |
| 
 | |
|         $crawler->filter('div.elementor-widget-container > p')->each(function ($node) {
 | |
|             $this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());;
 | |
|         });
 | |
| 
 | |
|         $this->image = $crawler->filter('div[data-widget_type*="theme-post-featured-image.default"] figure img')->first()->attr('src');
 | |
| 
 | |
|         $crawler->filter('.entry-tags a')->each(function ($node) {
 | |
|             if (!preg_match('/[^A-Za-z0-9-]/', basename($node->attr('href')))) {
 | |
|                 $this->topics[] = [
 | |
|                     "name" => $node->text(),
 | |
|                     "slug" => basename($node->attr('href'))
 | |
|                 ];
 | |
|             }
 | |
|         });
 | |
| 
 | |
|         $crawler->filter('a > .elementor-post-info__item--type-author')->each(function ($node) {
 | |
|             $this->author = $node->text();
 | |
|         });
 | |
| 
 | |
|         $urlComponents = parse_url($url);
 | |
|         $cleanUrl = $urlComponents['scheme'] . '://' . $urlComponents['host'] . $urlComponents['path'];
 | |
| 
 | |
|         // Extract and modify the guid
 | |
|         $guid = basename($urlComponents['path']);
 | |
| 
 | |
|         return [
 | |
|             'source'    => 'Thiladhun News',
 | |
|             'title'      => $this->title,
 | |
|             'og_title'   => str_replace(" | Thiladhun", "", $crawler->filter('title')->first()->text('content')),
 | |
|             'image'      => $this->image,
 | |
|             'content'    => $this->content,
 | |
|             'date'       => Carbon::now(),
 | |
|             'url'       => $cleanUrl,
 | |
|             'author'     => $this->author,
 | |
|             'guid'       => $guid,
 | |
|             'topics'       =>  $this->topics ?: [
 | |
|                 [
 | |
|                     "name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
 | |
|                     "slug" => "no-specific-topic"
 | |
|                 ]
 | |
|             ]
 | |
|         ];
 | |
|     }
 | |
| }
 |