Commit 7caf7995 by Phạm Văn Đoan

thiết lập thời gian chạy cho lấy bài hát mới phát hành

parent 8063f024
<?php
namespace App\Console\Commands\Zing;
use App\Helpers\Constants;
use App\Models\Crontjob;
use App\Repositories\TrackRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ZingCrawlerNewRealeaseCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'zing:get-new-release-track';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Lấy những bài hát mới phát hành';
protected $trackRepository;
/**
* Create a new command instance.
*
* ZingCrawlerNewRealeaseCommand constructor.
* @param TrackRepository $trackRepository
*/
public function __construct(TrackRepository $trackRepository)
{
parent::__construct();
$this->trackRepository = $trackRepository;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
echo "\nBắt đầu xử lý lấy bài hát mới phát hành từ zing mp3.";
// Lấy mảng ID của Zing những hát đã insert
$inserted = $this->trackRepository->getTrackInserted('title', 'original', []);;
// Lấy ds BXH về
$domain = config('api.zing.domain');
$url = config('api.zing.new_release');
$curl = curlZingMp3($url);
$data = json_decode($curl);
$tracks = $data->data->items;
$bulk_track_insert = [];
$bulk_track_update = [];
$key_insert = 0;
$key_update = 0;
foreach ($tracks as $key => $track) {
if (isset($track->title)) {
// Lấy ds ca sĩ thể hiện
$singer_list = 'ZingMp3';
$artists = isset($track->artists) ? $track->artists : [];
if (is_array($artists) && count($artists) > 0) {
$artist_list = collect($artists)->map(function ($item){ return $item->name; })->all();
$singer_list = implode(',', $artist_list);
}
// Xử lý insert/update dựa vào tên bài hát
if (is_array($inserted) && count($inserted) > 0 && in_array($track->title, $inserted)) {
$bulk_track_update[$key_update]['title'] = isset($track->title) ? $track->title : 'unknown-title';
$bulk_track_update[$key_update]['slug'] = isset($track->alias) ? $track->alias : null;
$bulk_track_update[$key_update]['userid'] = 0;
$bulk_track_update[$key_update]['tag'] = 'vmusic';
$bulk_track_update[$key_update]['genre'] = 1;
$bulk_track_update[$key_update]['download_hash'] = md5($track->alias);
$bulk_track_update[$key_update]['time'] = time();
$bulk_track_update[$key_update]['link'] = isset($track->link) ? $domain . $track->link : null;
$bulk_track_update[$key_update]['id_zing'] = isset($track->id) ? $track->id : null;
$bulk_track_update[$key_update]['singer_list'] = $singer_list;
$bulk_track_update[$key_update]['track_duration'] = isset($track->duration) ? $track->duration : 0;
$bulk_track_update[$key_update]['src_thumbnail'] = isset($track->thumbnail) ? $track->thumbnail : null;
$bulk_track_update[$key_update]['src_thumbnail_medium'] = isset($track->thumbnail_medium) ? $track->thumbnail_medium : null;
$bulk_track_update[$key_update]['link_mv'] = isset($track->mv_link) ? $domain . $track->mv_link : null;
$key_update++;
} else {
$bulk_track_insert[$key_insert]['title'] = isset($track->title) ? $track->title : 'unknown-title';
$bulk_track_insert[$key_insert]['slug'] = isset($track->alias) ? $track->alias : null;
$bulk_track_insert[$key_insert]['userid'] = 0;
$bulk_track_insert[$key_insert]['tag'] = 'vmusic';
$bulk_track_insert[$key_insert]['genre'] = 1;
$bulk_track_insert[$key_insert]['download_hash'] = md5($track->alias);
$bulk_track_insert[$key_insert]['time'] = time();
$bulk_track_insert[$key_insert]['link'] = isset($track->link) ? $domain . $track->link : null;
$bulk_track_insert[$key_insert]['id_zing'] = isset($track->id) ? $track->id : null;
$bulk_track_insert[$key_insert]['singer_list'] = $singer_list;
$bulk_track_insert[$key_insert]['track_duration'] = isset($track->duration) ? $track->duration : 0;
$bulk_track_insert[$key_insert]['src_thumbnail'] = isset($track->thumbnail) ? $track->thumbnail : null;
$bulk_track_insert[$key_insert]['src_thumbnail_medium'] = isset($track->thumbnail_medium) ? $track->thumbnail_medium : null;
$bulk_track_insert[$key_insert]['link_mv'] = isset($track->mv_link) ? $domain . $track->mv_link : null;
$bulk_track_insert[$key_insert]['crawler_at'] = Carbon::now();
$key_insert++;
}
}
}
echo "\nSố bài hát insert: " . count($bulk_track_insert);
echo "\nSố bài hát update: " . count($bulk_track_update);
if (count($bulk_track_insert) > 0) {
DB::table(Constants::TABLE_TRACKS)->insert($bulk_track_insert);
echo "\nInsert thành công.";
} else {
echo "\nKhông lấy được bài hát nào.";
}
// Ghi log
Crontjob::create(['message' => 'ZingCrawlerNewRealeaseCommand is called at ' . Carbon::now()]);
}
}
......@@ -3,6 +3,7 @@
namespace App\Console;
use App\Console\Commands\CrontjobCommand;
use App\Console\Commands\Zing\ZingCrawlerNewRealeaseCommand;
use App\Console\Commands\Zing\ZingCrawlerTrackCommand;
use App\Console\Commands\Zing\ZingCrawlerTrackImageCommand;
use Illuminate\Console\Scheduling\Schedule;
......@@ -18,6 +19,7 @@ class Kernel extends ConsoleKernel
protected $commands = [
CrontjobCommand::class,
ZingCrawlerTrackCommand::class,
ZingCrawlerNewRealeaseCommand::class,
ZingCrawlerTrackImageCommand::class,
];
......@@ -36,7 +38,8 @@ class Kernel extends ConsoleKernel
$schedule->command('crontjob:test')->hourly();
$schedule->command('zing:get-track')->everyThirtyMinutes();
$schedule->command('zing:get-track-image')->hourly();
$schedule->command('zing:get-new-release-track')->hourlyAt(10);
$schedule->command('zing:get-track-image')->hourlyAt(15);
$schedule->command('queue:work --once --timeout=120')->everyMinute()->withoutOverlapping();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment