Commit dc5ef86a by Phạm Văn Đoan

cập nhât task lấy bài hát từ zing mp3

parent 77e062bc
......@@ -2,7 +2,12 @@
namespace App\Console\Commands\Zing;
use App\Helpers\Constants;
use App\Models\Crontjob;
use App\Repositories\TrackRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ZingCrawlerTrackCommand extends Command
{
......@@ -20,14 +25,18 @@ class ZingCrawlerTrackCommand extends Command
*/
protected $description = 'Lấy thông tin bài hát từ ZingMp3';
protected $trackRepository;
/**
* Create a new command instance.
*
* @return void
* ZingCrawlerTrackCommand constructor.
* @param TrackRepository $trackRepository
*/
public function __construct()
public function __construct(TrackRepository $trackRepository)
{
parent::__construct();
$this->trackRepository = $trackRepository;
}
/**
......@@ -37,6 +46,82 @@ class ZingCrawlerTrackCommand extends Command
*/
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)) {
$bulk_track_update[$key_update]['title'] = isset($track->title) ? $track->title : 'unknown-title';
$bulk_track_update[$key_update]['slug'] = isset($track->alias) ? $track->alias : null;
$bulk_track_update[$key_update]['userid'] = 0;
$bulk_track_update[$key_update]['tag'] = 'vmusic';
$bulk_track_update[$key_update]['genre'] = 1;
$bulk_track_update[$key_update]['download_hash'] = md5($track->alias);
$bulk_track_update[$key_update]['time'] = time();
$bulk_track_update[$key_update]['link'] = isset($track->link) ? $domain . $track->link : null;
$bulk_track_update[$key_update]['id_zing'] = isset($track->id) ? $track->id : null;
$bulk_track_update[$key_update]['singer_list'] = $singer_list;
$bulk_track_update[$key_update]['track_duration'] = isset($track->duration) ? $track->duration : 0;
$bulk_track_update[$key_update]['src_thumbnail'] = isset($track->thumbnail) ? $track->thumbnail : null;
$bulk_track_update[$key_update]['src_thumbnail_medium'] = isset($track->thumbnail_medium) ? $track->thumbnail_medium : null;
$bulk_track_update[$key_update]['link_mv'] = isset($track->mv_link) ? $domain . $track->mv_link : null;
$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;
$key_insert++;
}
}
}
echo "\nSố bài hát insert: " . count($bulk_track_insert);
echo "\nSố bài hát update: " . count($bulk_track_update);
if (count($bulk_track_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()]);
}
}
......@@ -11,29 +11,35 @@ class TrackRepository extends BaseRepository
}
/**
* Lấy mảng ID do bên nguồn quản lý đã insert trong tracks
* @param string $src
* @return null
*/
public function getTrackSrcIdInserted($src='all')
public function getTrackInserted($pluck_by_field='all', $type='original', array $select_fields)
{
$query = Track::select(['id', 'source', 'id_zing', 'id_nct', 'id_keeng']);
if ($src != 'all') {
$query->where('source', $src);
switch ($src) {
case 'zing': return $query->pluck('id_zing')->toArray(); break;
case 'nct': return $query->pluck('id_nct')->toArray(); break;
case 'keeng': return $query->pluck('id_keeng')->toArray(); break;
if (count($select_fields) == 0) $select_fields = ['id', 'title', 'slug'];
$query = Track::select($select_fields);
if ($pluck_by_field == 'all') {
return $query->get()->toArray();
}
return null;
switch ($type) {
case 'upper':
return $query->pluck($pluck_by_field)->map(function ($item){
return strtoupper($item);
})->all();
break;
case 'lower':
return $query->pluck($pluck_by_field)->map(function ($item){
return strtolower($item);
})->all();
break;
default: return $query->pluck($pluck_by_field)->toArray();
}
return $query->get();
}
public function getTrackImageEmpty()
{
$query = Track::select(['id', 'image', 'source', 'id_zing', 'id_nct', 'id_keeng', 'thumbnail_medium'])->whereNull('image');
$query = Track::select(['id', 'art', 'id_zing', 'id_nct', 'id_keeng', 'src_thumbnail_medium'])->whereNull('art');
return $query->get()->toArray();
}
......
<?php
return [
/*
|--------------------------------------------------------------------------
| SMS API Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'sms' => [
'esms' => [
'api_key' => env('ESMS_API_KEY', 'ESMS_API_KEY'),
'secret_key' => env('ESMS_SECRET_KEY', 'ESMS_SECRET_KEY')
],
],
'weather' => [
'openweathermap' => [
'api_key_default' => 'd3cef33ba9791e4609a122642d8494a5',
'url_weather' => 'https://api.openweathermap.org/data/2.5/weather',
'url_forecast' => 'https://api.openweathermap.org/data/2.5/weather',
]
],
'smart_job' => [
'location_all' => 2146,
'jobs_url' => 'https://smartjob.vn/wp-content/themes/SMARTJOB/search_ajax.php',
'feed_url' => 'https://smartjob.vn/feed/',
],
'thongtindoanhnghiep' => [
'company' => 'https://thongtindoanhnghiep.co/api/company/'
],
'ahoadon' => [
'url_price' => 'https://ahoadon.com/bang-gia/',
'url_template' => 'https://ahoadon.com/mau-hoa-don/'
],
'http_exception' => [
'403' => [
'code' => '403',
'class' => 'AccessDeniedHttpException',
'message' => 'ACCESS_DENIED_HTTP_EXCEPTION'
],
'400' => [
'code' => '400',
'class' => 'BadRequestHttpException',
'message' => 'BAD_REQUEST_HTTP_EXCEPTION'
]
],
'zing' => [
'domain' => 'https://zingmp3.vn',
'chart_realtime' => 'https://zingmp3.vn/api/chart-realtime/get-detail?type=song&time=-1&count=100&ctime=1578533534&sig=0d0094bf842afb29153fbf281406eec4d64709de8ca280d540878b264b9ab0119058c9c6c9d0912fd0d02c24c8031da02f8bda9e573480704a7c1c62f3ec49be&api_key=38e8643fb0dc04e8d65b99994d3dafff',
'playlist' => 'https://zingmp3.vn/api/playlist/get-playlist-detail?id=ZWZB969E&ctime=1584213996&sig=e295c92bed3ce58a29a2fb8c69f9e1934bab807ae6f43c546eeb7ca4242be599171c9566f23296fdc137ee156fd307db4cfa1a4b528e5d99773065ee10c6a603&api_key=38e8643fb0dc04e8d65b99994d3dafff',
'new_release' => 'https://zingmp3.vn/api/chart/get-chart-new-release?ctime=1584214467&sig=66218d539436572b65c74d0c852550f7c7df0a3128aca9b5be13aa3dcd1d7c0bf49e3af459f75db87e3cc08f60c087684a0c92375ab3a8a784253502bbdf065f&api_key=38e8643fb0dc04e8d65b99994d3dafff',
'artist' => 'https://zingmp3.vn/api/artist/get-list?id=IWZ9Z08I&type=genre&sort=listen&start=1&count=1000&ctime=1584216597&sig=e2b8e1e48ccd12f560959913a6901fc76c9b93e1cafecba2d3517d8ab57eeb44717aa41eca70338bcf8bc8c84def1c43367ef44c887589db4ed81a1e2c042303&api_key=38e8643fb0dc04e8d65b99994d3dafff',
]
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment