Commit d1a50548 by Phạm Văn Đoan

lấy lịch sử lượt nghe từ NCT

parent 6ad20bc1
......@@ -2,7 +2,9 @@
namespace App\Console\Commands;
use App\Models\CrawlerListenNct;
use App\Repositories\TrackRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
class CrawlerNctTrackListenCommand extends Command
......@@ -46,7 +48,8 @@ class CrawlerNctTrackListenCommand extends Command
$from_page = 'nct';
$limit = 10;
$crawlered = $this->trackRepository->getCrawlerListenInserted($from_page, false);
//$crawlered = $this->trackRepository->getCrawlerListenInserted($from_page, false);
$crawlered = CrawlerListenNct::groupBy('track_id')->pluck('track_id')->all();
echo "\n Tổng số bài hát đã lấy lượt nghe hôm nay: " . count($crawlered);
......@@ -56,11 +59,57 @@ class CrawlerNctTrackListenCommand extends Command
echo "\n Tổng số bài hát cần lấy lượt nghe: " . $total;
foreach ($inserted as $key => $track) {
$listen[] = $this->privateGetListen($from_page, $track);
echo "\n " . ($key+1) . "/" . $total . ": Đã lấy lịch sử lượt nghe bài hát có ID= " . $track['id'];
}
if (count($listen) > 0) {
foreach ($listen as $key => $array_listen) {
if (count($array_listen) > 0) {
foreach ($array_listen as $val) {
CrawlerListenNct::updateOrCreate(
[
'track_id' => $val['track_id'],
'date_chart' => $val['date_chart']
],
[
'cumu_chart' => $val['cumu_chart'],
'daily_chart' => $val['daily_chart'],
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
]
);
}
}
}
}
echo "\n Kết thúc. \n";
}
$data = $this->trackRepository->getNctTrackListen($inserted);
print_r($data);die();
private function privateGetListen($src, $track)
{
$listen = [];
/* Xử lý mới sau khi bị chặn */
$url = getNctUrlGetCounterForChart($track['id_nct']);
$curl = cURL($url);
$response = json_decode($curl);
if (isset($response->status) && $response->status == 'Success') {
if (isset($response->dailyChart) && isset($response->dateChart) && isset($response->cumuChart) &&
is_array($response->dailyChart) && is_array($response->dateChart) && is_array($response->cumuChart))
{
foreach ($response->dailyChart as $key => $val) {
$listen[$key]['track_id'] = $track['id'];
$listen[$key]['daily_chart'] = $val;
$listen[$key]['date_chart'] = date('Y-m-d', strtotime(str_replace('/', '-', $response->dateChart[$key])));
$listen[$key]['cumu_chart'] = $response->cumuChart[$key];
}
}
}
return $listen;
}
}
......@@ -7,6 +7,7 @@ class Constants
const TABLE_ARTISTS = 'artists';
const TABLE_COUNT_VIEWS = 'count_views';
const TABLE_CRAWLER_LISTEN_HISTORIES = 'crawler_listen_histories';
const TABLE_CRAWLER_LISTEN_NCTS = 'crawler_listen_ncts';
const TABLE_CRAWLER_LISTENS = 'crawler_listens';
const TABLE_CRONTJOBS = 'crontjobs';
......
......@@ -245,6 +245,22 @@ if (!function_exists('getNctUrlGetCounter')) {
}
}
if (!function_exists('getNctUrlGetCounterForChart')) {
/**
* Hàm tạo URL cho từng bài hát dựa vào ID của nó để lấy lượt nghe
* https://www.nhaccuatui.com/ajax/chart?songId=6290700
*
* @param $id
* @return string
*/
function getNctUrlGetCounterForChart($id) {
$url = 'https://www.nhaccuatui.com/ajax/chart?';
$url .= 'songId=' . $id;
return $url;
}
}
if (!function_exists('getKeengUrlGetCounter')) {
/**
* Hàm tạo URL cho từng bài hát dựa vào ID của nó để lấy lượt nghe
......
<?php
namespace App\Models;
use App\Helpers\Constants;
use Illuminate\Database\Eloquent\Model;
class CrawlerListenNct extends Model
{
protected $table = Constants::TABLE_CRAWLER_LISTEN_NCTS;
public $timestamps = true;
protected $fillable = [
'track_id',
'date_chart',
'cumu_chart',
'daily_chart',
'is_get_reporting'
];
}
......@@ -437,7 +437,6 @@ class TrackRepository extends BaseRepository
public function getNctTrackListen($inserted)
{
return file_get_contents('https://www.nhaccuatui.com/interaction/api/counter?listSongIds=6217224');
$tracks = [];
//
......
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