75 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Services\Scrapers;
 | 
						|
 | 
						|
use Goutte\Client;
 | 
						|
use Illuminate\Support\Str;
 | 
						|
use Symfony\Component\HttpClient\HttpClient;
 | 
						|
 | 
						|
class AdhadhuScraper
 | 
						|
{
 | 
						|
    protected $client;
 | 
						|
 | 
						|
    protected $title;
 | 
						|
    protected $content;
 | 
						|
    protected $image;
 | 
						|
    protected $topics = [];
 | 
						|
    protected $author;
 | 
						|
 | 
						|
    public function __construct()
 | 
						|
    {
 | 
						|
        $this->client = new Client(
 | 
						|
            HttpClient::create([
 | 
						|
                "proxy" => config('karudhaas.proxy.host')
 | 
						|
            ])
 | 
						|
        );
 | 
						|
    }
 | 
						|
 | 
						|
    public function extract($url, $date = null)
 | 
						|
    {
 | 
						|
        $crawler = $this->client->request('GET', $url);
 | 
						|
 | 
						|
        // Extract title
 | 
						|
        $this->title = $crawler->filter('h1.font-52')->first()->text();
 | 
						|
 | 
						|
        // Extract image URL
 | 
						|
        $this->image = $crawler->filter('img.img-fluid.hero-img')->first()->attr('src');
 | 
						|
 | 
						|
        $authorNode = $crawler->filter('.MuiAvatar-circle img');
 | 
						|
        if ($authorNode->count() > 0) {
 | 
						|
            $this->author = $authorNode->first()->attr('alt');
 | 
						|
        } else {
 | 
						|
            $this->author = 'unknown'; // Set to 'Unknown' if author element is not found
 | 
						|
        }
 | 
						|
 | 
						|
        // Extract content
 | 
						|
        $crawler->filter('.body > p')->each(function ($node) {
 | 
						|
            $this->content[] = $node->text();
 | 
						|
        });
 | 
						|
 | 
						|
        // Extract topics (tags)
 | 
						|
        $crawler->filter('a[href^="/tags/"]')->each(function ($node) {
 | 
						|
            $href = $node->attr('href');
 | 
						|
            $slug = basename($href); // Extracts the last segment of the URL
 | 
						|
 | 
						|
            $this->topics[] = [
 | 
						|
                "name" => trim($node->filter('.tag')->first()->text()),
 | 
						|
                "slug" => Str::slug($slug)
 | 
						|
            ];
 | 
						|
        });
 | 
						|
 | 
						|
        return [
 | 
						|
            'source'    => 'Adhadhu',
 | 
						|
            'title'     => $this->title,
 | 
						|
            'og_title'   => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
 | 
						|
            'image'     => $this->image,
 | 
						|
            'content'   => $this->content,
 | 
						|
            'url'       => $url,
 | 
						|
            'date'      => $date,
 | 
						|
            'guid' =>   basename($url),
 | 
						|
            'author'    => $this->author,
 | 
						|
            'topics'    => $this->topics
 | 
						|
        ];
 | 
						|
    }
 | 
						|
}
 |