The Press intergration
This commit is contained in:
		
							
								
								
									
										62
									
								
								app/Services/Scrapers/ThePressScraper.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								app/Services/Scrapers/ThePressScraper.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace App\Services\Scrapers;
 | 
			
		||||
 | 
			
		||||
use Goutte\Client;
 | 
			
		||||
 | 
			
		||||
class ThePressScraper
 | 
			
		||||
{
 | 
			
		||||
    protected $client;
 | 
			
		||||
 | 
			
		||||
    protected $content;
 | 
			
		||||
    protected $author;
 | 
			
		||||
    protected $topics = [];
 | 
			
		||||
    
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $this->client = new Client;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function extract($url)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        $crawler = $this->client->request('GET', $url);
 | 
			
		||||
 | 
			
		||||
        $title = $crawler->filter('h1')->first()->text();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        $image = $crawler->filter('.article-image img')->first()->attr('src');
 | 
			
		||||
 | 
			
		||||
        $crawler->filter('article p')->each(function ($node) {
 | 
			
		||||
            $this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $crawler->filter('.article-tags a')->each(function ($node) {
 | 
			
		||||
           
 | 
			
		||||
            $this->topics[] = [
 | 
			
		||||
                "name" => $node->text(),
 | 
			
		||||
                "slug" => str_replace("https://thepress.mv/", "", $node->attr('href'))
 | 
			
		||||
            ];
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        if($crawler->filter(".author-details strong")->count() == 1)
 | 
			
		||||
        {
 | 
			
		||||
            $this->author = $crawler->filter('.author-details strong')->first()->text();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Remove all the alphabets from string
 | 
			
		||||
        //preg_replace("/[a-zA-Z]/", "",$string);
 | 
			
		||||
       return [
 | 
			
		||||
            'source'    => 'The Press',
 | 
			
		||||
            'title'      => $title,
 | 
			
		||||
            'og_title'   => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
 | 
			
		||||
            'image'      => $image,
 | 
			
		||||
            'content'    => $this->content,
 | 
			
		||||
            'url'        => $url,
 | 
			
		||||
            'date'       => $crawler->filter('.article-header .datetime')->first()->text(),
 | 
			
		||||
            'guid'       => str_replace("https://thepress.mv/","",$url),
 | 
			
		||||
            'author'     => $this->author,
 | 
			
		||||
            'topics'       => $this->topics
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								app/Services/ThePressService.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								app/Services/ThePressService.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Services;
 | 
			
		||||
 | 
			
		||||
use App\Services\Scrapers\ThePressScraper;
 | 
			
		||||
use Illuminate\Support\Str;
 | 
			
		||||
 | 
			
		||||
class ThePressService extends Client
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Scrap all the rss articles from Press
 | 
			
		||||
     *
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public function scrape(): array
 | 
			
		||||
    {
 | 
			
		||||
        //Return only the rss that contains "news" keyboard in its url
 | 
			
		||||
        $articles = $this->get("https://thepress.mv/rss")["channel"]["item"];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        $articlesitems = [];
 | 
			
		||||
        //Looping through the articles and scraping and while scraping it creates a new instance of the scraper.
 | 
			
		||||
        foreach ($articles as $article) {
 | 
			
		||||
            $link = $article['link'];
 | 
			
		||||
            $articlesitems[] = (new ThePressScraper)->extract($link);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $articlesitems;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user