diff --git a/app/Console/Commands/ScrapeFeshunCommand.php b/app/Console/Commands/ScrapeFeshunCommand.php new file mode 100644 index 0000000..f51659a --- /dev/null +++ b/app/Console/Commands/ScrapeFeshunCommand.php @@ -0,0 +1,72 @@ +first(); + + $articles = (new FeshunService)->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 8b7280f..009121f 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -57,6 +57,9 @@ class Kernel extends ConsoleKernel $schedule->command('scrape:oneonline')->everyFiveMinutes() ->pingOnSuccess(env('APP_URL') . "/api/ping/oneonline"); + + $schedule->command('scrape:feshun')->everyFiveMinutes() + ->pingOnSuccess(env('APP_URL') . "/api/ping/feshun"); } /** diff --git a/app/Services/FeshunService.php b/app/Services/FeshunService.php new file mode 100644 index 0000000..c728544 --- /dev/null +++ b/app/Services/FeshunService.php @@ -0,0 +1,26 @@ +get("https://feshun.mv/feed")["channel"]["item"]; + + $articlesitems = []; + //Looping through the articles and scraping and while scraping it creates a new instance of the scraper. + foreach ($articles as $article) { + $articlesitems[] = (new FeshunScraper)->extract($article['link'], $article['pubDate']); + } + + return $articlesitems; + } +} diff --git a/app/Services/Scrapers/FeshunScraper.php b/app/Services/Scrapers/FeshunScraper.php new file mode 100644 index 0000000..90ad938 --- /dev/null +++ b/app/Services/Scrapers/FeshunScraper.php @@ -0,0 +1,75 @@ +client = new Client(); + } + + /** + * extract. + * + * @param mixed $url + * @param mixed $date + * @param mixed $guid + * + * @return array + */ + public function extract($url, $date) + { + $this->guid = str_replace('https://feshun.mv/', '', $url); + + $crawler = $this->client->request('GET', $url); + + $crawler->filter('h3')->each(function ($node) { + $this->title = $node->text(); + }); + + + $crawler->filter('.elementor-widget-container p')->each(function ($node) { + $this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());; + }); + + + $crawler->filter('li[itemprop*="author"] span')->each(function ($node) { + $this->author = $node->text(); + }); + + + return [ + 'source' => 'Feshun', + 'title' => $this->title, + 'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'), + 'image' => $crawler->filter('figure img')->first()->attr('src'), + 'content' => $this->content, + 'date' => Carbon::parse($date)->format("Y-m-d H:i:s"), + 'url' => $url, + 'author' => $this->author, + 'guid' => $this->guid, + 'topics' => [ + [ + "name" => "ވަކި މަޢުލޫއެއް ނޭންގެ", + "slug" => "no-specific-topic" + ] + ] + ]; + } +}