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
143
144
145
146
<?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()]);
}
}