<?php namespace App\Console\Commands\Keeng; use App\Helpers\Constants; use App\Models\Crontjob; use App\Models\Track; use App\Repositories\TrackRepository; use Carbon\Carbon; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; class KeengCrawlerTrackCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'keeng:get-track'; /** * The console command description. * * @var string */ protected $description = 'Lấy thông tin bài hát từ Keeng'; protected $trackRepository; /** * Create a new command instance. * * KeengCrawlerTrackCommand constructor. * @param TrackRepository $trackRepository */ public function __construct(TrackRepository $trackRepository) { parent::__construct(); ini_set('max_execution_time', -1); $this->trackRepository = $trackRepository; } /** * Execute the console command. * * @return mixed */ public function handle() { echo "\nBắt đầu xử lý lấy top bài hát từ Keeng."; // Lấy mảng ID của Zing những bài hát đã insert $inserted = $this->trackRepository->getTrackInserted('title', 'original', []); // Lấy ds BXH về $domain = config('api.keeng.domain'); $url = config('api.keeng.api_get_rank'); $curl = cURL($url); $data = json_decode($curl); $tracks = $data->data; $bulk_track_insert = []; $bulk_track_update = []; $key_insert = 0; $key_update = 0; foreach ($tracks as $key => $track) { if (isset($track->name)) { // Xử lý insert/update dựa vào tên bài hát if (is_array($inserted) && count($inserted) > 0 && in_array($track->name, $inserted)) { $track_db = Track::where('title', $track->name)->take(1)->get(); if (isset($track_db[0])) { $track_db[0]->link_keeng = isset($track->url) ? $track->url : null; $track_db[0]->id_keeng = isset($track->identify) ? $track->identify : null; if (empty($track_db[0]->singer_list)) { $track_db[0]->singer_list = isset($track->singer) ? $track->singer : null; } if (empty($track_db[0]->composer_list)) { $track_db[0]->composer_list = isset($track->info_extra->author_name) ? $track->info_extra->author_name : null; } if (empty($track_db[0]->src_singer)) { $track_db[0]->src_singer = isset($track->artists_names) ? $track->artists_names : null; } if (empty($track_db[0]->src_composer)) { $track_db[0]->src_composer = isset($track->info_extra->author_name) ? $track->info_extra->author_name : null; } $track_db[0]->crawler_at = Carbon::now(); $track_db[0]->save(); } $key_update++; } else { $bulk_track_insert[$key_insert]['title'] = isset($track->name) ? $track->name : 'unknown-title'; $bulk_track_insert[$key_insert]['slug'] = isset($track->slug) ? $track->slug : 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->id); $bulk_track_insert[$key_insert]['time'] = time(); $bulk_track_insert[$key_insert]['link_keeng'] = isset($track->url) ? $track->url : null; $bulk_track_insert[$key_insert]['id_keeng'] = isset($track->identify) ? $track->identify : null; $bulk_track_insert[$key_insert]['singer_list'] = isset($track->singer) ? $track->singer : null; $bulk_track_insert[$key_insert]['composer_list'] = isset($track->info_extra->author_name) ? $track->info_extra->author_name : null; $bulk_track_insert[$key_insert]['src_thumbnail'] = isset($track->image) ? $track->image : null; $bulk_track_insert[$key_insert]['src_thumbnail_medium'] = isset($track->image310) ? $track->image310 : null; $bulk_track_insert[$key_insert]['src_singer'] = isset($track->singer) ? $track->singer : null; $composer = (isset($track->info_extra) && isset($track->info_extra->author_name)) ? $track->info_extra->author_name : null; $bulk_track_insert[$key_insert]['src_composer'] = $composer; $bulk_track_insert[$key_insert]['crawler_at'] = Carbon::now(); $key_insert++; } } } echo "\nSố bài hát insert: " . $key_insert; echo "\nSố bài hát update: " . $key_update; if ($key_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' => 'KeengCrawlerTrackCommand is called at ' . Carbon::now()]); } }