-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync videos list of channel and list videos paginated
- Remove identifier to remote_identifier in Channel - Add Video Sync and list paginated with newest one first - Add jobs to make jobs run in background - Sync videos from youtube to database and insert only the new ones
- Loading branch information
1 parent
f9dc94f
commit 38acda0
Showing
18 changed files
with
907 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Jobs\SyncVideosForGivenChannelJob; | ||
use App\Models\Channel; | ||
|
||
class SyncVideosController | ||
{ | ||
public function __invoke(Channel $channel) | ||
{ | ||
dispatch(new SyncVideosForGivenChannelJob($channel)); | ||
return back(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\Channel; | ||
use App\Services\SyncVideosForGivenChannelService; | ||
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; | ||
|
||
class SyncVideosForGivenChannelJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* @var Channel | ||
*/ | ||
private $channel; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Channel $channel) | ||
{ | ||
$this->channel = $channel; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
(new SyncVideosForGivenChannelService())->execute($this->channel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
class Video extends Model | ||
{ | ||
use SoftDeletes; | ||
use HasFactory; | ||
|
||
protected $dates = [ | ||
'favorite_at', | ||
'seen_at', | ||
'published_at' | ||
]; | ||
protected $fillable = [ | ||
'title', | ||
'description', | ||
'thumbnail', | ||
'channel_id', | ||
'published_at', | ||
'seen_at', | ||
'favorite_at', | ||
'remote_identifier', | ||
]; | ||
|
||
protected $appends=[ | ||
'show_more', | ||
'link' | ||
]; | ||
|
||
public function channel() | ||
{ | ||
return $this->belongsTo(Channel::class); | ||
} | ||
|
||
public function getShowMoreAttribute() | ||
{ | ||
return false; | ||
} | ||
|
||
public function getLinkAttribute() | ||
{ | ||
return config('youtube.urls.video_base_link').$this->remote_identifier; | ||
} | ||
|
||
public function getPublishedAtAttribute($val) | ||
{ | ||
$dateTime=Carbon::parse($val); | ||
return $dateTime->diffForHumans().' | '.$dateTime->format('d-m-Y h:i:s a'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
|
||
namespace App\Services; | ||
|
||
|
||
use App\Models\Channel; | ||
use App\Models\Video; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class SyncVideosForGivenChannelService | ||
{ | ||
|
||
private $videosData = []; | ||
|
||
public function execute(Channel $channel) | ||
{ | ||
$response = Http::get(config('youtube.urls.channel'), [ | ||
'id' => $channel->remote_identifier, | ||
'part' => 'contentDetails', | ||
'key' => config('youtube.key') | ||
])->json(); | ||
$playlist = Arr::get($response, 'items.0.contentDetails.relatedPlaylists.uploads'); | ||
$response = Http::get(config('youtube.urls.videos'), [ | ||
'part' => 'snippet', | ||
'playlistId' => $playlist, | ||
'maxResults' => '50', | ||
'key' => config('youtube.key') | ||
])->json(); | ||
$this->AddMoreVideosData(Arr::get($response, 'items', []), $channel); | ||
while (isset($response['nextPageToken'])) { | ||
$response = Http::get(config('youtube.urls.videos'), [ | ||
'part' => 'snippet', | ||
'playlistId' => $playlist, | ||
'maxResults' => '50', | ||
'key' => config('youtube.key'), | ||
'pageToken' => Arr::get($response, 'nextPageToken') | ||
])->json(); | ||
|
||
$this->AddMoreVideosData(Arr::get($response, 'items', []), $channel); | ||
} | ||
|
||
$videos = $this->filterVideosToNotExistsVideos($this->getExistsRemoteIds()); | ||
|
||
if ($videos->isNotEmpty()) { | ||
return Video::insert($videos->toArray()); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @param array $videosData | ||
* @param $channel | ||
*/ | ||
private function AddMoreVideosData(array $videosData, $channel) | ||
{ | ||
foreach ($videosData as $video) { | ||
$this->videosData[] = [ | ||
'remote_identifier' => Arr::get($video, 'snippet.resourceId.videoId'), | ||
'title' => Arr::get($video, 'snippet.title'), | ||
'description' => Arr::get($video, 'snippet.description'), | ||
'thumbnail' => Arr::get($video, 'snippet.thumbnails.high.url'), | ||
'channel_id' => $channel->id, | ||
'published_at' => Carbon::parse(Arr::get($video, 'snippet.publishedAt')), | ||
'created_at' => now(), | ||
'updated_at' => now(), | ||
]; | ||
|
||
} | ||
} | ||
|
||
/** | ||
* @param $existsRemoteIds | ||
* @return Collection | ||
*/ | ||
public function filterVideosToNotExistsVideos($existsRemoteIds): Collection | ||
{ | ||
return collect($this->videosData)->filter(function ($video) use ($existsRemoteIds) { | ||
return !$existsRemoteIds->contains($video['remote_identifier']); | ||
}); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getExistsRemoteIds() | ||
{ | ||
return Video::Select('remote_identifier')->pluck('remote_identifier'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
'urls' => [ | ||
'base'=> env('YOUTUBE_BASE_URL_API'), | ||
'channel' => env('YOUTUBE_CHANNEL_URL_API'), | ||
'videos' => env('YOUTUBE_CHANNEL_videos_URL_API'), | ||
'video_base_link' => env('YOUTUBE_VIDEO_BASE_LINK'), | ||
], | ||
|
||
|
||
'key' => env('YOUTUBE_API_KEY'), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
database/migrations/2021_09_03_214916_create_videos_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateVideosTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('videos', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('title'); | ||
$table->text('description')->nullable(); | ||
$table->string('remote_identifier'); | ||
$table->string('thumbnail'); | ||
$table->foreignId('channel_id')->constrained(); | ||
$table->timestamp('favorite_at')->nullable(); | ||
$table->timestamp('published_at')->nullable(); | ||
$table->timestamp('seen_at')->nullable(); | ||
$table->softDeletes(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('videos'); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
database/migrations/2021_09_03_224355_create_jobs_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateJobsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('jobs', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('queue')->index(); | ||
$table->longText('payload'); | ||
$table->unsignedTinyInteger('attempts'); | ||
$table->unsignedInteger('reserved_at')->nullable(); | ||
$table->unsignedInteger('available_at'); | ||
$table->unsignedInteger('created_at'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('jobs'); | ||
} | ||
} |
Oops, something went wrong.