Browse Source

feat: webscraping job in the background

master
erdar2 4 years ago
parent
commit
e8c4082351
  1. 2
      app/Console/Kernel.php
  2. 56
      app/Jobs/UpdateCovidData.php
  3. 13
      app/Models/CovidData.php
  4. 44
      app/Models/User.php
  5. 4
      app/Providers/RouteServiceProvider.php
  6. 3
      composer.json
  7. 36
      database/migrations/2021_12_16_145224_create_covid_data_table.php

2
app/Console/Kernel.php

@ -2,6 +2,7 @@
namespace App\Console; namespace App\Console;
use App\Jobs\UpdateCovidData;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -16,6 +17,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule) protected function schedule(Schedule $schedule)
{ {
// $schedule->command('inspire')->hourly(); // $schedule->command('inspire')->hourly();
$schedule->job(new UpdateCovidData)->hourly();
} }
/** /**

56
app/Jobs/UpdateCovidData.php

@ -0,0 +1,56 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use App\Models\CovidData;
use Carbon\Carbon;
class UpdateCovidData implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$url = 'https://api.apify.com/v2/datasets/Gm6qjTgGqxkEZTkuJ/items?format=json&clean=1';
$res = Http::get($url);
CovidData::whereNotNull('id')->delete();
foreach ($res->collect() as $data) {
try {
$attr = [];
$attr["infected"] = $data["infected"] ?? null;
$attr["deceased"] = $data["deceased"] ?? null;
$attr["recovered"] = $data["recovered"] ?? null;
$attr["quarantined"] = $data["quarantined"] ?? null;
$attr["tested"] = $data["tested"] ?? null;
$attr["created_at"] = Carbon::createFromFormat('Y-m-d\TH:i:s+', $data["lastUpdatedAtApify"]);
$d = new CovidData($attr);
$d->save();
} catch (\Exception $e) {
dd($data, $e);
}
};
//dd(CovidData::all());
}
}

13
app/Models/CovidData.php

@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CovidData extends Model
{
protected $table = 'covid_data';
protected $fillable = ['infected','deceased','recovered','quarantined','tested','created_at'];
public $timestamps = false;
}

44
app/Models/User.php

@ -1,44 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}

4
app/Providers/RouteServiceProvider.php

@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider
* *
* @var string * @var string
*/ */
public const HOME = '/home'; public const HOME = '/';
/** /**
* The controller namespace for the application. * The controller namespace for the application.
@ -26,7 +26,7 @@ class RouteServiceProvider extends ServiceProvider
* *
* @var string|null * @var string|null
*/ */
// protected $namespace = 'App\\Http\\Controllers'; protected $namespace = 'App\\Http\\Controllers';
/** /**
* Define your route model bindings, pattern filters, etc. * Define your route model bindings, pattern filters, etc.

3
composer.json

@ -7,9 +7,8 @@
"require": { "require": {
"php": "^7.3|^8.0", "php": "^7.3|^8.0",
"fruitcake/laravel-cors": "^2.0", "fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1", "guzzlehttp/guzzle": "^7.4",
"laravel/framework": "^8.65", "laravel/framework": "^8.65",
"laravel/sanctum": "^2.11",
"laravel/tinker": "^2.5", "laravel/tinker": "^2.5",
"laravel/ui": "^3.4" "laravel/ui": "^3.4"
}, },

36
database/migrations/2021_12_16_145224_create_covid_data_table.php

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCovidDataTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('covid_data', function (Blueprint $table) {
$table->id();
$table->integer('infected')->nullable();
$table->integer('deceased')->nullable();
$table->integer('recovered')->nullable();
$table->integer('quarantined')->nullable();
$table->integer('tested')->nullable();
$table->dateTime('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('covid_data');
}
}
Loading…
Cancel
Save