67 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Services\Scrapers;
 | |
| 
 | |
| use Goutte\Client;
 | |
| use Illuminate\Support\Carbon;
 | |
| 
 | |
| class OneOnlineScraper
 | |
| {
 | |
|     protected $client;
 | |
| 
 | |
|     protected $title;
 | |
|     protected $content;
 | |
|     protected $topics = [];
 | |
|     protected $author = "unknown";
 | |
| 
 | |
|     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('.content p')->each(function ($node) {
 | |
|             $this->content[] = $node->text();
 | |
|         });
 | |
| 
 | |
|         $crawler->filter('.tags a')->each(function ($node) {
 | |
|             //Removing the show more tags button
 | |
|             if ($node->text() == "ގުޅޭ ޓެގު") {
 | |
|                 return;
 | |
|             }
 | |
| 
 | |
|             $this->topics[] = [
 | |
|                 "name" => $node->text(),
 | |
|                 "slug" => str_replace("https://oneonline.mv/", "", $node->attr('href'))
 | |
|             ];
 | |
|         });
 | |
| 
 | |
| 
 | |
|         if ($crawler->filter('div[class*="text-grey-dark font-waheed flex flex-row items-center"] span')->count() == 1) {
 | |
|             $this->author = $crawler->filter('div[class*="text-grey-dark font-waheed flex flex-row items-center"] span')->first()->text();
 | |
|         }
 | |
|         //Remove all the alphabets from string
 | |
|         //preg_replace("/[a-zA-Z]/", "",$string);
 | |
|         return [
 | |
|             'source'    => 'OneOnline',
 | |
|             'title'      => $this->title,
 | |
|             '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($crawler->filter('time')->first()->attr('datetime'))->format("Y-m-d H:i:s"),
 | |
|             'guid'       => str_replace("https://oneonline.mv/", "", $url),
 | |
|             'author'     => $this->author,
 | |
|             'topics'       => $this->topics
 | |
|         ];
 | |
|     }
 | |
| }
 |