From 0589b198436b01b0a0d7bdddbbfacbdc699438f9 Mon Sep 17 00:00:00 2001 From: Mohamed jinas Date: Sun, 7 Jan 2024 21:42:31 +0500 Subject: [PATCH] fix thiladhun --- app/Services/Feeds/ThiladhunFeed.php | 21 ++++++++-------- app/Services/Scrapers/ThiladhunScraper.php | 29 ++++++++++++---------- app/Services/ThiladhunService.php | 4 +-- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/app/Services/Feeds/ThiladhunFeed.php b/app/Services/Feeds/ThiladhunFeed.php index 798a15d..cf92bc4 100644 --- a/app/Services/Feeds/ThiladhunFeed.php +++ b/app/Services/Feeds/ThiladhunFeed.php @@ -18,22 +18,23 @@ class ThiladhunFeed implements Feed */ public function get() : array { - $crawler = $this->client->request('GET', "https://www.thiladhun.com"); $feeds = []; - $crawler->filter('div[class*="posts-listing posts-list"] article')->each(function ($node) use (&$feeds) { - - - $feeds[] = [ - "title" => $node->filter('.post__title a')->text(), - "link" => $node->filter('.post__title a')->attr('href'), - "date" => $node->filter('time')->first()->attr('datetime') - ]; + // Assuming 'ފަހުގެ' is a unique identifier for the section of interest + $crawler->filter('.elementor-element:contains("ފަހުގެ")')->nextAll('.elementor-post')->each(function ($node) use (&$feeds) { + $title = $node->filter('.elementor-heading-title a')->count() ? $node->filter('.elementor-heading-title a')->text() : null; + $link = $node->filter('.elementor-heading-title a')->count() ? $node->filter('.elementor-heading-title a')->attr('href') : null; + if ($title && $link) { + $feeds[] = [ + "title" => $title, + "link" => $link, + ]; + } }); return $feeds; - } + } diff --git a/app/Services/Scrapers/ThiladhunScraper.php b/app/Services/Scrapers/ThiladhunScraper.php index a22aba4..beef35a 100644 --- a/app/Services/Scrapers/ThiladhunScraper.php +++ b/app/Services/Scrapers/ThiladhunScraper.php @@ -34,7 +34,7 @@ class ThiladhunScraper * * @return array */ - public function extract($url, $date) + public function extract($url) { $crawler = $this->client->request('GET', $url); @@ -51,20 +51,23 @@ class ThiladhunScraper $this->image = $crawler->filter('div[data-widget_type*="theme-post-featured-image.default"] figure img')->first()->attr('src'); $crawler->filter('.entry-tags a')->each(function ($node) { - if(!preg_match('/[^A-Za-z0-9-]/', basename($node->attr('href')))) - { - $this->topics[] = [ - "name" => $node->text(), - "slug" => basename($node->attr('href')) - ]; + if (!preg_match('/[^A-Za-z0-9-]/', basename($node->attr('href')))) { + $this->topics[] = [ + "name" => $node->text(), + "slug" => basename($node->attr('href')) + ]; } - - }); + }); $crawler->filter('a > .elementor-post-info__item--type-author')->each(function ($node) { $this->author = $node->text(); }); + $urlComponents = parse_url($url); + $cleanUrl = $urlComponents['scheme'] . '://' . $urlComponents['host'] . $urlComponents['path']; + + // Extract and modify the guid + $guid = basename($urlComponents['path']); return [ 'source' => 'Thiladhun News', @@ -72,11 +75,11 @@ class ThiladhunScraper 'og_title' => str_replace(" | Thiladhun", "", $crawler->filter('title')->first()->text('content')), 'image' => $this->image, 'content' => $this->content, - 'date' => Carbon::parse($date)->format("Y-m-d H:i:s"), - 'url' => $url, + 'date' => Carbon::now(), + 'url' => $cleanUrl, 'author' => $this->author, - 'guid' => basename($url), - 'topics' => $this->topics ? : [ + 'guid' => $guid, + 'topics' => $this->topics ?: [ [ "name" => "ވަކި މަޢުލޫއެއް ނޭންގެ", "slug" => "no-specific-topic" diff --git a/app/Services/ThiladhunService.php b/app/Services/ThiladhunService.php index 8f4fd20..ac9cef8 100644 --- a/app/Services/ThiladhunService.php +++ b/app/Services/ThiladhunService.php @@ -20,12 +20,12 @@ class ThiladhunService extends Client $response = Http::get("https://thiladhun.com/feed")->body(); $data = json_decode(json_encode(simplexml_load_string($response)), true); $articles = $data["channel"]["item"]; - + $articlesitems = []; //Looping through the articles and scraping and while scraping it creates a new instance of the scraper. foreach ($articles as $article) { //Remove query strings - $articlesitems[] = (new ThiladhunScraper)->extract(Str::before($article["link"], '?'), $article["pubDate"]); + $articlesitems[] = (new ThiladhunScraper)->extract($article["link"]); } return $articlesitems;