NctCrawlerTrackCommand.php 4.99 KB
<?php

namespace App\Console\Commands\Nct;

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

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

    protected $trackRepository;

    /**
     * Create a new command instance.
     *
     * NctCrawlerTrackCommand constructor.
     * @param TrackRepository $trackRepository
     */
    public function __construct(TrackRepository $trackRepository)
    {
        parent::__construct();
        $this->trackRepository = $trackRepository;
    }

    /**
     * Execute the console command.
     * Top 100 NCT: https://www.nhaccuatui.com/playlist/top-100-nhac-tre-hay-nhat-va.m3liaiy6vVsF.html
     *
     * @return mixed
     */
    public function handle()
    {
        echo "\n Bắt đầu xử lý lấy top 100 bài hát từ NCT.";

        // 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.nct.domain');
        $url = config('api.nct.top100');
        $url_playlist_top100 = config('api.nct.playlist_top100');

        $tracks = $this->trackRepository->processNctGetTrackInfoFromTop100($url);
        $tracks_playlist = $this->trackRepository->processNctGetTrackInfoFromTop100Playlist($url_playlist_top100);

        if (count($tracks) == 0) return false;

        $key_insert = 0;
        $key_update = 0;
        $bulk_track_insert = [];

        foreach ($tracks as $key => $track) {
            //$link = $this->privateGeTrackLink($tracks_playlist, $track['id_nct']);
            $link = $tracks_playlist[$key]['link'];
            $duration = $tracks_playlist[$key]['duration'];
            // 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)) {
                // Cập nhật
                $track_db = Track::where('title', $track['title'])->take(1)->get();
                if (isset($track_db[0])) {
                    $track_db[0]->track_duration = $duration;
                    $track_db[0]->id_nct = $track['id_nct'];
                    $track_db[0]->id_nct_str = $track['id_nct_str'];
                    $track_db[0]->link_nct_top100 = $track['link'];
                    $track_db[0]->link_nct = $link;
                    $track_db[0]->crawler_at = Carbon::now();
                    $track_db[0]->save();
                }
                $key_update++;
            } else {
                $bulk_track_insert[$key_insert]['title'] = $track['title'];
                $bulk_track_insert[$key_insert]['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_nct']);
                $bulk_track_insert[$key_insert]['time'] = time();
                $bulk_track_insert[$key_insert]['track_duration'] = $duration;
                $bulk_track_insert[$key_insert]['id_nct'] = $track['id_nct'];
                $bulk_track_insert[$key_insert]['id_nct_str'] = $track['id_nct_str'];
                $bulk_track_insert[$key_insert]['src_thumbnail_medium'] = $track['img'];
                $bulk_track_insert[$key_insert]['link_nct'] = $link;
                $bulk_track_insert[$key_insert]['link_nct_top100'] = $track['link'];
                $bulk_track_insert[$key_insert]['crawler_at'] = Carbon::now();

                $key_insert++;
            }
        }

        echo "\n Số bài hát insert: " . $key_insert;
        echo "\n Số bài hát update: " . $key_update;

        if ($key_insert > 0) {
            DB::table(Constants::TABLE_TRACKS)->insert($bulk_track_insert);
            echo "\n Insert thành công.";
        } else {
            echo "\n Không lấy được bài hát nào.";
        }

        // Ghi log
        Crontjob::create(['message' => 'NctCrawlerTrackCommand is called at ' . Carbon::now()]);

    }

    /**
     * Hàm lấy link tương ứng của bài hát với ID dạng số, không tìm thấy trả về null
     * @param $tracks_playlist
     * @param $track_id
     * @return string
     */
    private function privateGeTrackLink($tracks_playlist, $track_id)
    {
        if (is_array($tracks_playlist) && count($tracks_playlist) > 0) {
            foreach ($tracks_playlist as $key => $val) {
                if ($val['id_nct'] == $track_id) {
                    return $val['link'];
                }
            }
        }

        return null;
    }

}