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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?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;
}
}