From e0f0e29bd46478a80d5127e4079e862a510a5a6e Mon Sep 17 00:00:00 2001 From: Mohamed Jinas Date: Tue, 18 Aug 2020 00:47:08 +0500 Subject: [PATCH] Voice intergration --- app/Console/Commands/ScrapeVoiceCommand.php | 73 +++++++++++++++++++++ app/Services/Feeds/VoiceFeed.php | 28 ++++++++ app/Services/Scrapers/VoiceScraper.php | 64 ++++++++++++++++++ app/Services/VoiceService.php | 26 ++++++++ 4 files changed, 191 insertions(+) create mode 100644 app/Console/Commands/ScrapeVoiceCommand.php create mode 100644 app/Services/Feeds/VoiceFeed.php create mode 100644 app/Services/Scrapers/VoiceScraper.php create mode 100644 app/Services/VoiceService.php diff --git a/app/Console/Commands/ScrapeVoiceCommand.php b/app/Console/Commands/ScrapeVoiceCommand.php new file mode 100644 index 0000000..c4394fe --- /dev/null +++ b/app/Console/Commands/ScrapeVoiceCommand.php @@ -0,0 +1,73 @@ +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); + }); + + } + } +} diff --git a/app/Services/Feeds/VoiceFeed.php b/app/Services/Feeds/VoiceFeed.php new file mode 100644 index 0000000..c7b24dc --- /dev/null +++ b/app/Services/Feeds/VoiceFeed.php @@ -0,0 +1,28 @@ +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']); + + } +} \ No newline at end of file diff --git a/app/Services/Scrapers/VoiceScraper.php b/app/Services/Scrapers/VoiceScraper.php new file mode 100644 index 0000000..60d3eff --- /dev/null +++ b/app/Services/Scrapers/VoiceScraper.php @@ -0,0 +1,64 @@ +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 + ]; + } +} \ No newline at end of file diff --git a/app/Services/VoiceService.php b/app/Services/VoiceService.php new file mode 100644 index 0000000..91b5593 --- /dev/null +++ b/app/Services/VoiceService.php @@ -0,0 +1,26 @@ +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; + } +} \ No newline at end of file