diff --git a/app/Console/Commands/ScrapeVnewsCommand.php b/app/Console/Commands/ScrapeVnewsCommand.php new file mode 100644 index 0000000..e9e3e29 --- /dev/null +++ b/app/Console/Commands/ScrapeVnewsCommand.php @@ -0,0 +1,72 @@ +first(); + + $articles = (new VnewsService)->scrape(); + + foreach ($articles as $article) { + + // Attach the relationship between source and article and return the curren article instance + $articleModel = $source->articles()->updateOrCreate(["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/Console/Kernel.php b/app/Console/Kernel.php index cb24bd0..aefec2f 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -63,6 +63,9 @@ class Kernel extends ConsoleKernel $schedule->command('scrape:miadhu')->everyFiveMinutes() ->pingOnSuccess(env('APP_URL') . "/api/ping/miadhu"); + + $schedule->command('scrape:vnews')->everyFiveMinutes() + ->pingOnSuccess(env('APP_URL') . "/api/ping/vnews"); } /** diff --git a/app/Services/Scrapers/VnewsScraper.php b/app/Services/Scrapers/VnewsScraper.php new file mode 100644 index 0000000..6062897 --- /dev/null +++ b/app/Services/Scrapers/VnewsScraper.php @@ -0,0 +1,58 @@ +client = new Client; + } + + public function extract($url, $date) + { + + $crawler = $this->client->request('GET', $url); + + $crawler->filter('.news-content > p')->each(function ($node) { + $this->content[] = $node->text(); + }); + + if ($crawler->filter('.author')->count() > 0) { + $this->author = $crawler->filter('.author')->first()->text(); + } + + $crawler->filter('.news-tags a')->each(function ($node) { + $this->topics[] = [ + "name" => $node->text(), + "slug" => str_replace("#", "", str_replace("https://www.vnews.mv/", "", $node->attr('href'))) + ]; + }); + + + //Remove all the alphabets from string + //preg_replace("/[a-zA-Z]/", "",$string); + return [ + 'source' => 'VNews', + 'title' => $crawler->filter('h1')->first()->text(), + 'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'), + 'image' => $crawler->filter(".news-image img")->first()->attr('src'), + 'content' => $this->content, + 'url' => $url, + 'date' => Carbon::parse($date)->format("Y-m-d H:i:s"), + 'guid' => str_replace("https://www.vnews.mv/", "", $url), + 'author' => $this->author, + 'topics' => $this->topics + ]; + } +} diff --git a/app/Services/VnewsService.php b/app/Services/VnewsService.php new file mode 100644 index 0000000..59aa9cc --- /dev/null +++ b/app/Services/VnewsService.php @@ -0,0 +1,29 @@ +get("https://www.vnews.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']; + $date = $article['pubDate']; + $articlesitems[] = (new VnewsScraper)->extract($link, $date); + } + + return $articlesitems; + } +}