From bb5602f91182e79ab35b2dc0706cf0d98756761b Mon Sep 17 00:00:00 2001 From: Mohamed Jinas Date: Mon, 11 Jan 2021 13:46:06 +0500 Subject: [PATCH] Zaviyani news intergration --- .../Commands/ScrapeZaviyaniCommand.php | 74 +++++++++++++++++++ app/Console/Kernel.php | 4 + app/Services/Feeds/ZaviyaniFeed.php | 39 ++++++++++ app/Services/Scrapers/ZaviyaniScraper.php | 55 ++++++++++++++ app/Services/ZaviyaniService.php | 30 ++++++++ 5 files changed, 202 insertions(+) create mode 100644 app/Console/Commands/ScrapeZaviyaniCommand.php create mode 100644 app/Services/Feeds/ZaviyaniFeed.php create mode 100644 app/Services/Scrapers/ZaviyaniScraper.php create mode 100644 app/Services/ZaviyaniService.php diff --git a/app/Console/Commands/ScrapeZaviyaniCommand.php b/app/Console/Commands/ScrapeZaviyaniCommand.php new file mode 100644 index 0000000..968a6dc --- /dev/null +++ b/app/Console/Commands/ScrapeZaviyaniCommand.php @@ -0,0 +1,74 @@ +first(); + + $articles = (new ZaviyaniService)->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 67ea951..77f4dce 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -92,6 +92,10 @@ class Kernel extends ConsoleKernel $schedule->command('scrape:funadhoo-times')->everyFiveMinutes() ->runInBackground() ->pingOnSuccess(env('APP_URL') . "/api/ping/funadhoo-times"); + + $schedule->command('scrape:zaviyani')->everyFiveMinutes() + ->runInBackground() + ->pingOnSuccess(env('APP_URL') . "/api/ping/zaviyani"); } /** diff --git a/app/Services/Feeds/ZaviyaniFeed.php b/app/Services/Feeds/ZaviyaniFeed.php new file mode 100644 index 0000000..95f495e --- /dev/null +++ b/app/Services/Feeds/ZaviyaniFeed.php @@ -0,0 +1,39 @@ +client = new Client(); + } + /** + * Get all the latest news + * + * @return array + */ + public function get() : array + { + + $crawler = $this->client->request('GET', "https://zaviyani.mv/"); + + $feeds = []; + $crawler->filter('div[class*="jeg_vc_content"] article')->each(function ($node) use (&$feeds) { + + + $feeds[] = [ + "title" => $node->filter('.jeg_post_title')->text(), + "link" => $node->filter('.jeg_post_title a')->attr('href'), + "date" => str_replace("-","",$node->filter('.jeg_meta_date a')->first()->text()) + ]; + + }); + + return array_slice($feeds,0,24); + + } +} \ No newline at end of file diff --git a/app/Services/Scrapers/ZaviyaniScraper.php b/app/Services/Scrapers/ZaviyaniScraper.php new file mode 100644 index 0000000..b9000e6 --- /dev/null +++ b/app/Services/Scrapers/ZaviyaniScraper.php @@ -0,0 +1,55 @@ +client = new Client; + } + + public function extract($url, $date) + { + + $crawler = $this->client->request('GET', $url); + + $crawler->filter('.content-inner p')->each(function ($node) { + $this->content[] = $node->text(); + }); + + + if ($crawler->filter('div[class*="jeg_meta_author"] a')->count() == 1) { + $this->author = $crawler->filter('div[class*="jeg_meta_author"] a')->first()->text(); + } + //Remove all the alphabets from string + //preg_replace("/[a-zA-Z]/", "",$string); + return [ + 'source' => 'Zaviyani', + 'title' => $crawler->filter('h1')->first()->text(), + 'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'), + 'image' => $crawler->filter(".featured_image img")->first()->attr('data-src'), + 'content' => $this->content, + 'url' => $url, + 'date' => Carbon::parse($date)->format("Y-m-d H:i:s"), + 'guid' => str_replace("https://zaviyani.mv/?p=", "", $url), + 'author' => $this->author, + 'topics' => [ + [ + "name" => "ވަކި މަޢުލޫއެއް ނޭންގެ", + "slug" => "no-specific-topic" + ] + ] + ]; + } +} diff --git a/app/Services/ZaviyaniService.php b/app/Services/ZaviyaniService.php new file mode 100644 index 0000000..9b52295 --- /dev/null +++ b/app/Services/ZaviyaniService.php @@ -0,0 +1,30 @@ +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 ZaviyaniScraper)->extract($article["link"], $article["date"]); + } + + return $articlesitems; + } +}