Voice intergration
This commit is contained in:
		
							
								
								
									
										64
									
								
								app/Services/Scrapers/VoiceScraper.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								app/Services/Scrapers/VoiceScraper.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Services\Scrapers;
 | 
			
		||||
 | 
			
		||||
use Goutte\Client;
 | 
			
		||||
use Illuminate\Support\Carbon;
 | 
			
		||||
 | 
			
		||||
class VoiceScraper
 | 
			
		||||
{
 | 
			
		||||
    protected $client;
 | 
			
		||||
 | 
			
		||||
    protected $author;
 | 
			
		||||
    protected $content;
 | 
			
		||||
    protected $topics = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $this->client = new Client;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function extract($url)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $crawler = $this->client->request('GET', $url);
 | 
			
		||||
 | 
			
		||||
        $title = $crawler->filter('.content h1')->first()->text();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        $image = $crawler->filter('.image img')->first()->attr('src');
 | 
			
		||||
 | 
			
		||||
        $crawler->filter('article p')->each(function ($node) {
 | 
			
		||||
            $this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $crawler->filter('.related-tags-holder a')->each(function ($node) {
 | 
			
		||||
 | 
			
		||||
            $this->topics[] = [
 | 
			
		||||
                "name" => $node->text(),
 | 
			
		||||
                "slug" => str_replace("/", "", $node->attr('href'))
 | 
			
		||||
            ];
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        if ($crawler->filter(".authorname a")->count() == 1) {
 | 
			
		||||
            $this->author = str_replace("- ", "", $crawler->filter('.authorname a')->first()->text());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        //Remove all the alphabets from string
 | 
			
		||||
        //preg_replace("/[a-zA-Z]/", "",$string);
 | 
			
		||||
        return [
 | 
			
		||||
            'source'    => 'Voice',
 | 
			
		||||
            'title'      => $title,
 | 
			
		||||
            'og_title'   => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
 | 
			
		||||
            'image'      => $image,
 | 
			
		||||
            'content'    => $this->content,
 | 
			
		||||
            'url'        => $url,
 | 
			
		||||
            'date'       => Carbon::parse(str_replace("- ", "", $crawler->filter('.authordatecomment .date')->first()->text()))->format("Y-m-d H:i:s"),
 | 
			
		||||
            'guid'       => str_replace("https://voice.mv/", "", $url),
 | 
			
		||||
            'author'     => $this->author,
 | 
			
		||||
            'topics'       => $this->topics
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user