From fd2fc06115cb1552e27db6b003f9c48f37f83f02 Mon Sep 17 00:00:00 2001 From: Mohamed Jinas Date: Mon, 10 Aug 2020 03:21:56 +0500 Subject: [PATCH] Mihaaru scraper support --- app/Console/Commands/ScrapeMihaaruCommand.php | 23 ++++++++++++++++++- app/Console/Kernel.php | 3 ++- app/Services/MihaaruService.php | 6 ++--- app/Services/Scrapers/MihaaruScraper.php | 4 +++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/Console/Commands/ScrapeMihaaruCommand.php b/app/Console/Commands/ScrapeMihaaruCommand.php index e3137fa..a572ad9 100644 --- a/app/Console/Commands/ScrapeMihaaruCommand.php +++ b/app/Console/Commands/ScrapeMihaaruCommand.php @@ -2,7 +2,9 @@ namespace App\Console\Commands; +use App\Source; use Illuminate\Console\Command; +use App\Services\MihaaruService; class ScrapeMihaaruCommand extends Command { @@ -37,6 +39,25 @@ class ScrapeMihaaruCommand extends Command */ public function handle() { - return 0; + $source = Source::where('slug', 'mihaaru')->first(); + + $articles = (new MihaaruService)->scrape(); + + foreach($articles as $article) + { + $source->articles()->firstOrCreate([ + "title" => $article["title"], + "url" => $article["url"], + "author" => $article["author"], + "featured_image" => $article["image"], + "body" => $article["content"], + "guid" => $article["guid"], + "published_date" => $article["date"], + + ]); + } + + + } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 69914e9..9486625 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,6 +2,7 @@ namespace App\Console; +use App\Console\Commands\ScrapeMihaaruCommand; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -13,7 +14,7 @@ class Kernel extends ConsoleKernel * @var array */ protected $commands = [ - // + ScrapeMihaaruCommand::class ]; /** diff --git a/app/Services/MihaaruService.php b/app/Services/MihaaruService.php index 7387371..f7315f0 100644 --- a/app/Services/MihaaruService.php +++ b/app/Services/MihaaruService.php @@ -15,15 +15,15 @@ class MihaaruService extends Client { $articles = $this->get("https://mihaaru.com/rss")["channel"]["item"]; - + $articlesitems = []; - $emihaaru = new MihaaruScraper(); + $scraper = new MihaaruScraper(); foreach ($articles as $article) { $link = $article['link']; $date = $article['pubDate']; $guid = $article['guid']; - $articlesitems[] = $emihaaru->extract($link, $date, $guid); + $articlesitems[] = $scraper->extract($link, $date, $guid); } return $articlesitems; diff --git a/app/Services/Scrapers/MihaaruScraper.php b/app/Services/Scrapers/MihaaruScraper.php index e94af7e..7be4cbd 100644 --- a/app/Services/Scrapers/MihaaruScraper.php +++ b/app/Services/Scrapers/MihaaruScraper.php @@ -19,7 +19,7 @@ class MihaaruScraper $this->client = new Client; } - public function extract($url) + public function extract($url, $date = null, $guid = null) { $crawler = $this->client->request('GET', $url); @@ -66,6 +66,8 @@ class MihaaruScraper 'image' => $this->image, 'content' => $this->content, 'url' => $url, + 'date' => $date, + 'guid' => $guid, 'author' => $this->author, 'topics' => $this->tags, ];