KeengCrawlerTrackCommand.php 4.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?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();
40
        ini_set('max_execution_time', -1);
41 42 43 44 45 46 47 48 49 50
        $this->trackRepository = $trackRepository;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
51
        echo "\nBắt đầu xử lý lấy top bài hát từ Keeng.";
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

        // 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;
74
                        $track_db[0]->id_keeng = isset($track->identify) ? $track->identify : null;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
                        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;
                        }
                        $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;
95
                    $bulk_track_insert[$key_insert]['id_keeng'] = isset($track->identify) ? $track->identify : null;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                    $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]['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()]);
    }
}