From 2b81bbd5fa6e197f312a7246d188b6658cfcab21 Mon Sep 17 00:00:00 2001 From: Mohamed Jinas Date: Sat, 15 Aug 2020 01:26:46 +0500 Subject: [PATCH] Addulive intergration --- .../Commands/ScrapeAdduLiveCommand.php | 73 +++++++++++++++++++ app/Services/AdduLiveService.php | 29 ++++++++ app/Services/Scrapers/AdduLiveScraper.php | 62 ++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 app/Console/Commands/ScrapeAdduLiveCommand.php create mode 100644 app/Services/AdduLiveService.php create mode 100644 app/Services/Scrapers/AdduLiveScraper.php diff --git a/app/Console/Commands/ScrapeAdduLiveCommand.php b/app/Console/Commands/ScrapeAdduLiveCommand.php new file mode 100644 index 0000000..487e2e1 --- /dev/null +++ b/app/Console/Commands/ScrapeAdduLiveCommand.php @@ -0,0 +1,73 @@ +first(); + + $articles = (new AdduLiveService)->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" => Carbon::parse($article["date"])->format("Y-m-d H:i:s"), + "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/AdduLiveService.php b/app/Services/AdduLiveService.php new file mode 100644 index 0000000..cea8a19 --- /dev/null +++ b/app/Services/AdduLiveService.php @@ -0,0 +1,29 @@ +get("https://www.addulive.com/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) { + $link = $article['link']; + $articlesitems[] = (new AdduLiveScraper)->extract($link); + } + + return $articlesitems; + } +} diff --git a/app/Services/Scrapers/AdduLiveScraper.php b/app/Services/Scrapers/AdduLiveScraper.php new file mode 100644 index 0000000..f15150a --- /dev/null +++ b/app/Services/Scrapers/AdduLiveScraper.php @@ -0,0 +1,62 @@ +client = new Client; + } + + public function extract($url) + { + + $crawler = $this->client->request('GET', $url); + + $title = $crawler->filter('h1')->first()->text(); + + + + $image = $crawler->filter('article .entry-featured-image img')->first()->attr('src'); + + $crawler->filter('article p')->each(function ($node) { + $this->content[] = preg_replace("/[a-zA-Z]/","",$node->text()); + }); + + $crawler->filter('.entry-categories a')->each(function ($node) { + + $this->topics[] = [ + "name" => $node->text(), + "slug" => str_replace("https://www.addulive.com/topics/", "", $node->attr('href')) + ]; + }); + + if($crawler->filter(".author a")->count() == 1) + { + $this->author = $crawler->filter('.author a')->first()->text(); + } + + //Remove all the alphabets from string + //preg_replace("/[a-zA-Z]/", "",$string); + return [ + 'source' => 'Addulive', + 'title' => $title, + 'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'), + 'image' => $image, + 'content' => $this->content, + 'url' => $url, + 'date' => $crawler->filter('.entry-meta time')->first()->text(), + 'guid' => str_replace("https://thepress.mv/","",$url), + 'author' => $this->author, + 'topics' => $this->topics + ]; + } +} \ No newline at end of file