Voice intergration
This commit is contained in:
		
							
								
								
									
										73
									
								
								app/Console/Commands/ScrapeVoiceCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								app/Console/Commands/ScrapeVoiceCommand.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Console\Commands; | ||||
|  | ||||
| use App\Source; | ||||
| use Illuminate\Console\Command; | ||||
| use App\Services\VoiceService; | ||||
| use App\Topic; | ||||
| use Illuminate\Support\Carbon; | ||||
|  | ||||
| class ScrapeVoiceCommand extends Command | ||||
| { | ||||
|     /** | ||||
|      * The name and signature of the console command. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
|     protected $signature = 'scrape:voice'; | ||||
|  | ||||
|     /** | ||||
|      * The console command description. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
|     protected $description = 'Scrape Voice'; | ||||
|  | ||||
|     /** | ||||
|      * Create a new command instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         parent::__construct(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Execute the console command. | ||||
|      * | ||||
|      * @return int | ||||
|      */ | ||||
|     public function handle() | ||||
|     { | ||||
|         $source = Source::where('slug', 'voice')->first(); | ||||
|  | ||||
|         $articles = (new VoiceService)->scrape(); | ||||
|  | ||||
|         foreach ($articles as $article) { | ||||
|  | ||||
|             // Attach the relationship between source and article and return the curren article instance | ||||
|             $articleModel = $source->articles()->firstOrCreate(["guid" => $article["guid"]], | ||||
|                 [ | ||||
|                 "title" => $article["title"], | ||||
|                 "url" => $article["url"], | ||||
|                 "author" => $article["author"], | ||||
|                 "featured_image" => $article["image"], | ||||
|                 "body" => $article["content"], | ||||
|                 "published_date" => $article["date"], | ||||
|                 "meta" => [ | ||||
|                     "title" => $article["og_title"] | ||||
|                 ] | ||||
|  | ||||
|             ]); | ||||
|  | ||||
|             collect($article["topics"])->each(function($topic) use ($articleModel) { | ||||
|                 $topicModel = Topic::firstOrCreate(["slug" => $topic["slug"]],["name" => $topic["name"]]); | ||||
|  | ||||
|                 $topicModel->articles()->syncWithoutDetaching($articleModel); | ||||
|             }); | ||||
|  | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										28
									
								
								app/Services/Feeds/VoiceFeed.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								app/Services/Feeds/VoiceFeed.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| <?php | ||||
| namespace App\Services\Feeds; | ||||
|  | ||||
| use Goutte\Client; | ||||
|  | ||||
| class VoiceFeed | ||||
| { | ||||
|     protected $client; | ||||
|  | ||||
|     public function __construct() | ||||
|     { | ||||
|         $this->client = new Client();         | ||||
|     }     | ||||
|     /** | ||||
|      * Get all the latest news | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     public function get() : array | ||||
|     { | ||||
|        | ||||
|         $crawler = $this->client->request('GET', "https://voice.mv/"); | ||||
|  | ||||
|         return $crawler->filter('div[id*="latest-news"] .content a') | ||||
|                             ->extract(['_text', '_attr' => 'href']);              | ||||
|  | ||||
|     } | ||||
| } | ||||
							
								
								
									
										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 | ||||
|         ]; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										26
									
								
								app/Services/VoiceService.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								app/Services/VoiceService.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| <?php | ||||
| namespace App\Services; | ||||
|  | ||||
| use App\Services\Feeds\VoiceFeed; | ||||
| use App\Services\Scrapers\VoiceScraper; | ||||
|  | ||||
| class VoiceService | ||||
| { | ||||
|     /** | ||||
|      * Scrap all the rss articles from Sun | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     public function scrape(): array | ||||
|     { | ||||
|         $articles = (new VoiceFeed)->get(); | ||||
|  | ||||
|         $articlesitems = []; | ||||
|         //Looping through the articles and scraping and while scraping it creates a new instance of the scraper. | ||||
|         foreach ($articles as $article) { | ||||
|             $articlesitems[] = (new VoiceScraper)->extract("https://voice.mv/".$article[1]); | ||||
|         } | ||||
|  | ||||
|         return $articlesitems; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user