Commit 12f15b9a by Phạm Văn Đoan

task cập nhật thông tin ca sĩ cho bài hát lấy từ zing

parent 2194ab71
<?php
namespace App\Console\Commands\Update;
use App\Models\Track;
use App\Models\TrackUser;
use App\Repositories\TrackRepository;
use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
class UpdateTrackArtistCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'track:update-artist {src?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cập nhật thông tin ca sĩ, nhạc sĩ của bài hát.';
protected $trackRepository;
/**
* Create a new command instance.
*
* CalculateDailyListenCommand constructor.
* @param TrackRepository $trackRepository
*/
public function __construct(TrackRepository $trackRepository)
{
parent::__construct();
ini_set('max_execution_time', -1);
$this->trackRepository = $trackRepository;
}
/**
* Cập nhật thông tin ca sĩ cho bài hát theo từng hệ thống: zing, nct, keeng
*
* B1: Lấy thông tin ca sĩ về
* B2: Check trong bảng users xem đã có chưa, có thì lấy ID, chưa có thì tạo và lấy ID
* B3: Check trong bảng track_user:
* - Check cặp (track_id, name): nếu tồn tại thì update user_id, nếu chưa thì insert (track_id, user_id, name)
* - Check cặp (track_id, user_id): nếu có thì thôi, nếu chưa thì insert (track_id, usser_id, name)
*
* @return mixed
*/
public function handle()
{
$src = $this->argument('src');
if (empty($src) || !in_array($src, ['zing', 'nct', 'keeng', 'all'])) {
$src = $this->choice('Chọn nguồn cập nhật ', ['zing', 'nct', 'keeng', 'all']);
}
echo "\n Bắt đầu cập nhật ca sĩ cho bài hát.";
switch ($src) {
case 'zing':
$track_id = [];
$track_id_fullname = [];
// Lấy playlist ----------------------------------------------------------------------------------------
$url = config('api.zing.chart_realtime');
$curl = curlZingMp3($url);
$data = json_decode($curl);
$tracks = $data->data->items;
if ($tracks) {
foreach ($tracks as $key => $track) {
if (isset($track->title) && isset($track->artists_names) && !empty($track->artists_names)) {
$track_id[] = $track->id;
$track_id_fullname[$track->id] = $track->artists_names;
}
}
}
// Xử lý cập nhật
if (count($track_id_fullname) > 0) {
$track_db = Track::select(['id', 'id_zing', 'title'])
->whereIn('id_zing', $track_id)
->get();
$this->privateUpdateTrackSinger($src, $track_db, $track_id_fullname);
}
// Lấy bài mới xuất bản --------------------------------------------------------------------------------
$track_id = [];
$track_id_fullname = [];
$url_new_release = config('api.zing.new_release');
$curl_new_release = curlZingMp3($url_new_release);
$data_new_release = json_decode($curl_new_release);
$tracks_new_release = $data_new_release->data->items;
if ($tracks_new_release) {
foreach ($tracks_new_release as $key => $track) {
if (isset($track->title) && isset($track->artists_names) && !empty($track->artists_names)) {
$track_id[] = $track->id;
$track_id_fullname[$track->id] = $track->artists_names;
}
}
}
// Xử lý cập nhật
if (count($track_id_fullname) > 0) {
$track_db = Track::select(['id', 'id_zing', 'title'])
->whereIn('id_zing', $track_id)
->get();
$this->privateUpdateTrackSinger($src, $track_db, $track_id_fullname);
}
break;
case 'nct':
break;
case 'keeng':
break;
case 'all':
break;
default:
}
echo "\n Kết thúc cập nhật ca sĩ cho bài hát. \n";
}
private function privateUpdateTrackSinger($src, $track_db, $track_id_fullname)
{
switch ($src) {
case 'zing':
echo "\n Zing: Cập nhật cho tổng số bài: " . $track_db->count();
foreach ($track_db as $key => $track) {
$user = User::updateOrCreate(
['full_name' => $track_id_fullname[$track->id_zing]],
['user_type' => 2, 'updated_at' => Carbon::now()]
);
TrackUser::updateOrCreate(
[
'track_id' => $track->id,
'user_id' => $user->id
],
['name' => $track_id_fullname[$track->id_zing]]
);
echo "\n - Cập nhật xong Ca sĩ (ID: $user->id) cho bài hát (ID: $track->id)";
}
break;
case 'nct': break;
case 'keeng': break;
}
}
}
......@@ -11,6 +11,7 @@ use App\Console\Commands\Keeng\KeengCrawlerNewRealeaseCommand;
use App\Console\Commands\Keeng\KeengCrawlerTrackCommand;
use App\Console\Commands\Nct\NctCrawlerTrackCommand;
use App\Console\Commands\NhacVn\NhacVnCrawlerTrackCommand;
use App\Console\Commands\Update\UpdateTrackArtistCommand;
use App\Console\Commands\Zing\ZingCrawlerNewRealeaseCommand;
use App\Console\Commands\Zing\ZingCrawlerTrackCommand;
use App\Console\Commands\CrawlerTrackImageCommand;
......@@ -42,6 +43,8 @@ class Kernel extends ConsoleKernel
CalculateReportCommand::class,
UpdateTrackArtistCommand::class,
];
......
......@@ -9,5 +9,9 @@ class TrackArtist extends Model
{
protected $table = Constants::TABLE_TRACK_ARTIST;
public $timestamps = false;
protected $fillable = ['track_id', 'user_id', 'name', 'status'];
}
<?php
namespace App\Models;
use App\Helpers\Constants;
use Illuminate\Database\Eloquent\Model;
class TrackUser extends Model
{
protected $table = Constants::TABLE_TRACK_USER;
public $timestamps = false;
protected $fillable = ['track_id', 'user_id', 'name', 'status'];
}
......@@ -2,6 +2,7 @@
namespace App;
use App\Helpers\Constants;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
......@@ -9,13 +10,18 @@ class User extends Authenticatable
{
use Notifiable;
protected $table = Constants::TABLE_USERS;
public $timestamps = true;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'username', 'password', 'email', 'full_name', 'avatar', 'cover', 'active', 'user_type',
'link', 'link_nct', 'link_keeng', 'zing_id', 'nct_id', 'keeng_id'
];
/**
......
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