ZingCrawlerTrackCommand.php 6.16 KB
<?php

namespace App\Console\Commands\Zing;

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 ZingCrawlerTrackCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'zing:get-track';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Lấy thông tin bài hát từ ZingMp3';

    protected $trackRepository;

    /**
     * Create a new command instance.
     *
     * ZingCrawlerTrackCommand 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 100 bài hát từ zing mp3.";

        // 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.zing.domain');
        $url = config('api.zing.chart_realtime');
        $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)) {
                    $track_db = Track::where('title', $track->title)->take(1)->get();
                    if (isset($track_db[0])) {
                        if (empty($track_db[0]->slug)) {
                            $track_db[0]->slug = isset($track->alias) ? $track->alias : null;
                        }
                        if (empty($track_db[0]->download_hash)) {
                            $track_db[0]->download_hash = md5($track->alias);
                        }
                        if (empty($track_db[0]->time)) {
                            $track_db[0]->time =  time();
                        }
                        if (empty($track_db[0]->link)) {
                            $track_db[0]->link =  isset($track->link) ? $domain . $track->link : null;
                        }
                        if (empty($track_db[0]->id_zing)) {
                            $track_db[0]->id_zing =  isset($track->id) ? $track->id : null;
                        }
                        if (empty($track_db[0]->track_duration)) {
                            $track_db[0]->track_duration = isset($track->duration) ? $track->duration : 0;
                        }
                        if (empty($track_db[0]->link_mv)) {
                            $track_db[0]->link_mv = isset($track->mv_link) ? $domain . $track->mv_link : null;
                        }
                        if (empty($track_db[0]->src_singer)) {
                            $track_db[0]->src_singer = isset($track->artists_names) ? $track->artists_names : null;
                        }
                        $track_db[0]->crawler_at = Carbon::now();
                        $track_db[0]->save();
                    }

                    $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]['src_singer'] = isset($track->artists_names) ? $track->artists_names : null;
                    $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' => 'ZingCrawlerTrackCommand is called at ' . Carbon::now()]);
    }
}