Zaviyani news intergration
This commit is contained in:
		
							
								
								
									
										74
									
								
								app/Console/Commands/ScrapeZaviyaniCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								app/Console/Commands/ScrapeZaviyaniCommand.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,74 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Console\Commands;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Console\Command;
 | 
			
		||||
use App\Source;
 | 
			
		||||
use App\Services\ZaviyaniService;
 | 
			
		||||
use App\Topic;
 | 
			
		||||
use Illuminate\Support\Carbon;
 | 
			
		||||
 | 
			
		||||
class ScraoeZaviyaniCommand extends Command
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * The name and signature of the console command.
 | 
			
		||||
     *
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    protected $signature = 'scrape:zaviyani';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The console command description.
 | 
			
		||||
     *
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    protected $description = 'Scrape Zaviyani';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a new command instance.
 | 
			
		||||
     *
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Execute the console command.
 | 
			
		||||
     *
 | 
			
		||||
     * @return int
 | 
			
		||||
     */
 | 
			
		||||
    public function handle()
 | 
			
		||||
    {
 | 
			
		||||
        $source = Source::where('slug', 'zaviyani')->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);
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user