Commit bb3d023e by Phạm Văn Đoan

test tải ảnh bài hát

parent 1860cae1
<?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;
class ZingCrawlerTrackImageCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'zing:get-track-image';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Lấy ảnh đại diện của bài hát từ nguồn zing';
protected $trackRepository;
/**
* Create a new command instance.
*
* ZingCrawlerTrackImageCommand constructor.
* @param TrackRepository $trackRepository
*/
public function __construct(TrackRepository $trackRepository)
{
parent::__construct();
$this->trackRepository = $trackRepository;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$tracks = $this->trackRepository->getTrackImageEmpty();
echo "\nTổng số bài hát cần lấy ảnh về: " . count($tracks);
if (is_array($tracks) && count($tracks) > 0) {
foreach ($tracks as $key => $track) {
if ($track['src_thumbnail_medium']) {
$file_name = md5($track['id']) . '.png';
$file_path = Constants::VMUSICCHART_IMG_TRACKS . $file_name;
@copy(Constants::VMUSICCHART_IMG_EMPTY_PNG, $file_path);
@file_put_contents($file_path, @file_get_contents($track['src_thumbnail_medium']));
$track = Track::find($track['id']);
if ($track) {
$track->art = Constants::VMUSICCHART_IMG_TRACKS_DB . $file_name;
$track->save();
}
}
}
}
echo "\nTải ảnh kết thúc.";
// Ghi log
Crontjob::create(['message' => 'ZingCrawlerTrackImageCommand is called at ' . Carbon::now()]);
}
}
......@@ -4,6 +4,7 @@ namespace App\Console;
use App\Console\Commands\CrontjobCommand;
use App\Console\Commands\Zing\ZingCrawlerTrackCommand;
use App\Console\Commands\Zing\ZingCrawlerTrackImageCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
......@@ -17,6 +18,7 @@ class Kernel extends ConsoleKernel
protected $commands = [
CrontjobCommand::class,
ZingCrawlerTrackCommand::class,
ZingCrawlerTrackImageCommand::class,
];
......
......@@ -110,4 +110,11 @@ class Constants
const STORAGE_THUMBNAIL_ARTISTS_DB = 'storage/artists/thumbnails/';
const STORAGE_THUMBNAIL_ARTISTS = 'storage/app/public/artists/thumbnails/';
const VMUSICCHART_IMG_TRACKS_DB = 'uploads/tracks/arts/';
const VMUSICCHART_IMG_TRACKS = '/home/aseanvn/public_html/vmusicchart.dcv.vn/uploads/tracks/arts/';
const VMUSICCHART_IMG_EMPTY_PNG = '/home/aseanvn/public_html/vmusicchart.dcv.vn/uploads/empty-1x1-png.png';
const VMUSICCHART_IMG_EMPTY_JPG = '/home/aseanvn/public_html/vmusicchart.dcv.vn/uploads/empty-1x1-jpg.jpg';
const VMUSICCHART_IMG_EMPTY_GIF = '/home/aseanvn/public_html/vmusicchart.dcv.vn/uploads/empty-1x1-gif.gif';
}
\ No newline at end of file
......@@ -11,6 +11,17 @@ class TrackRepository extends BaseRepository
}
/**
* Lấy ds bài hát đã insert
*
* doanpv - 24/03/2020
*
* @param string $pluck_by_field
* @param string $type
* @param array $select_fields
*
* @return array
*/
public function getTrackInserted($pluck_by_field='all', $type='original', array $select_fields)
{
if (count($select_fields) == 0) $select_fields = ['id', 'title', 'slug'];
......@@ -37,9 +48,19 @@ class TrackRepository extends BaseRepository
}
}
/**
* Lấy ds bài hát chưa có ảnh đại diện (trường art: null)
*
* doanpv - 24/03/2020
*
* @return array
*/
public function getTrackImageEmpty()
{
$query = Track::select(['id', 'art', 'id_zing', 'id_nct', 'id_keeng', 'src_thumbnail_medium'])->whereNull('art');
$query = Track::select(['id', 'art', 'id_zing', 'id_nct', 'id_keeng', 'src_thumbnail_medium'])
->whereNull('art')
->whereNotNull('src_thumbnail_medium');
return $query->get()->toArray();
}
......
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