Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
crawler.vmusicchart.vn
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Phạm Văn Đoan
crawler.vmusicchart.vn
Commits
8c349671
Commit
8c349671
authored
Mar 29, 2020
by
Phạm Văn Đoan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cập nhật lấy lượt view ở các trang zing, nct, keeng
parent
4131a4ba
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
204 additions
and
13 deletions
+204
-13
CalculateDailyListenCommand.php
app/Console/Commands/CalculateDailyListenCommand.php
+139
-0
CrawlerTrackListenCommand.php
app/Console/Commands/CrawlerTrackListenCommand.php
+14
-5
Kernel.php
app/Console/Kernel.php
+28
-5
CrawlerListen.php
app/Models/CrawlerListen.php
+2
-1
TrackRepository.php
app/Repositories/TrackRepository.php
+21
-2
No files found.
app/Console/Commands/CalculateDailyListenCommand.php
0 → 100644
View file @
8c349671
<?php
namespace
App\Console\Commands
;
use
App\Models\Crontjob
;
use
App\Repositories\TrackRepository
;
use
Carbon\Carbon
;
use
Illuminate\Console\Command
;
class
CalculateDailyListenCommand
extends
Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected
$signature
=
'calculate:daily-listen'
;
/**
* The console command description.
*
* @var string
*/
protected
$description
=
'Di chuyển lượt nghe sang ngày hôm qua trước khi cập nhật lượt nghe ngày mới'
;
protected
$trackRepository
;
/**
* Create a new command instance.
*
* CalculateDailyListenCommand constructor.
* @param TrackRepository $trackRepository
*/
public
function
__construct
(
TrackRepository
$trackRepository
)
{
parent
::
__construct
();
ini_set
(
'max_execution_time'
,
-
1
);
$this
->
trackRepository
=
$trackRepository
;
}
/**
* Xử lý di chuyển dựa vào cờ date_moved_at
* Mỗi lần di chuyển 1k bản ghi trong crawler_listens
* Di chuyển xong thì cập nhật ngày trong crawler_listens.date_moved_at và cho lượt nghe hôm nay=0
* Trong lần sau sẽ ko lấy những bản ghi có date_moved_at = ngày hiện tại
* Execute the console command.
*
* @return mixed
*/
public
function
handle
()
{
// Ghi log xem có gọi không
Crontjob
::
create
([
'message'
=>
'CalculateDailyListenCommand is called at '
.
Carbon
::
now
()]);
// Xử lý di chuyển
echo
"
\n
Bắt đầu xử lý lấy lượt nghe ngày hôm qua"
;
$crawlered
=
$this
->
trackRepository
->
getCrawlerListenNotYetMoved
();
if
(
$crawlered
)
{
echo
"
\n
Số lượng cần tính toán lượt nghe: "
.
$crawlered
->
count
();
foreach
(
$crawlered
as
$key
=>
$listen
)
{
// Xử lý lượt nghe từ zing -----------------------------------------------------------------------------
if
(
$listen
->
yesterday_zing
==
0
)
{
if
(
$listen
->
today_zing
>
0
)
{
$listen
->
yesterday_zing
=
$listen
->
today_zing
;
$listen
->
today_zing
=
0
;
}
}
else
if
(
$listen
->
yesterday_zing
>
0
)
{
if
(
$listen
->
today_zing
>
0
)
{
$difference
=
$listen
->
today_zing
-
$listen
->
yesterday_zing
;
if
(
$difference
>=
0
)
{
$listen
->
yesterday_zing
=
$listen
->
today_zing
;
$listen
->
today_zing
=
0
;
$listen
->
date_zing
=
$difference
;
$listen
->
increment
(
'week_zing'
,
$difference
);
$listen
->
increment
(
'month_zing'
,
$difference
);
$listen
->
increment
(
'year_zing'
,
$difference
);
$listen
->
increment
(
'alltime_zing'
,
$difference
);
}
}
}
// Xử lý lượt nghe từ NCT ------------------------------------------------------------------------------
if
(
$listen
->
yesterday_nct
==
0
)
{
if
(
$listen
->
today_nct
>
0
)
{
$listen
->
yesterday_nct
=
$listen
->
today_nct
;
$listen
->
today_nct
=
0
;
}
}
else
if
(
$listen
->
yesterday_nct
>
0
)
{
if
(
$listen
->
today_nct
>
0
)
{
$difference
=
$listen
->
today_nct
-
$listen
->
yesterday_nct
;
if
(
$difference
>=
0
)
{
$listen
->
yesterday_nct
=
$listen
->
today_nct
;
$listen
->
today_nct
=
0
;
$listen
->
date_nct
=
$difference
;
$listen
->
increment
(
'week_nct'
,
$difference
);
$listen
->
increment
(
'month_nct'
,
$difference
);
$listen
->
increment
(
'year_nct'
,
$difference
);
$listen
->
increment
(
'alltime_nct'
,
$difference
);
}
}
}
// Xử lý lượt nghe từ nhacvn ---------------------------------------------------------------------------
if
(
$listen
->
yesterday_keeng
==
0
)
{
if
(
$listen
->
today_keeng
>
0
)
{
$listen
->
yesterday_keeng
=
$listen
->
today_keeng
;
$listen
->
today_keeng
=
0
;
}
}
else
if
(
$listen
->
yesterday_keeng
>
0
)
{
if
(
$listen
->
today_keeng
>
0
)
{
$difference
=
$listen
->
today_keeng
-
$listen
->
yesterday_keeng
;
if
(
$difference
>=
0
)
{
$listen
->
yesterday_keeng
=
$listen
->
today_keeng
;
$listen
->
today_keeng
=
0
;
$listen
->
date_keeng
=
$difference
;
$listen
->
increment
(
'week_keeng'
,
$difference
);
$listen
->
increment
(
'month_keeng'
,
$difference
);
$listen
->
increment
(
'year_keeng'
,
$difference
);
$listen
->
increment
(
'alltime_keeng'
,
$difference
);
}
}
}
// Lưu lại giá trị -------------------------------------------------------------------------------------
$listen
->
date_moved_at
=
Carbon
::
now
();
$listen
->
save
();
echo
"
\n
Đã tính toán lượt nghe bản ghi có ID= "
.
$listen
->
id
.
' (Track ID: '
.
$listen
->
track_id
.
')'
;
}
}
echo
"
\n
Tính toán kết thúc kết thúc: "
.
$crawlered
->
count
();
}
}
app/Console/Commands/CrawlerTrackListenCommand.php
View file @
8c349671
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
namespace
App\Console\Commands
;
namespace
App\Console\Commands
;
use
App\Helpers\Constants
;
use
App\Models\CrawlerListen
;
use
App\Models\CrawlerListen
;
use
App\Models\Crontjob
;
use
App\Models\Crontjob
;
use
App\Repositories\TrackRepository
;
use
App\Repositories\TrackRepository
;
...
@@ -15,7 +16,7 @@ class CrawlerTrackListenCommand extends Command
...
@@ -15,7 +16,7 @@ class CrawlerTrackListenCommand extends Command
*
*
* @var string
* @var string
*/
*/
protected
$signature
=
'crawler:get-listen {from_page?}'
;
protected
$signature
=
'crawler:get-listen {from_page?}
{--limit=}
'
;
/**
/**
* The console command description.
* The console command description.
...
@@ -50,11 +51,18 @@ class CrawlerTrackListenCommand extends Command
...
@@ -50,11 +51,18 @@ class CrawlerTrackListenCommand extends Command
Crontjob
::
create
([
'message'
=>
'CrawlerTrackListenCommand is called at '
.
Carbon
::
now
()]);
Crontjob
::
create
([
'message'
=>
'CrawlerTrackListenCommand is called at '
.
Carbon
::
now
()]);
$from_page
=
$this
->
argument
(
'from_page'
);
$from_page
=
$this
->
argument
(
'from_page'
);
$limit
=
$this
->
option
(
'limit'
);
if
(
empty
(
$from_page
)
||
!
in_array
(
$from_page
,
[
'zing'
,
'nct'
,
'nhacvn'
,
'keeng'
]))
{
if
(
empty
(
$from_page
)
||
!
in_array
(
$from_page
,
[
'zing'
,
'nct'
,
'nhacvn'
,
'keeng'
]))
{
$from_page
=
$this
->
choice
(
'Chọn trang nhạc '
,
[
'zing'
,
'nct'
,
'nhacvn'
,
'keeng'
]);
$from_page
=
$this
->
choice
(
'Chọn trang nhạc '
,
[
'zing'
,
'nct'
,
'nhacvn'
,
'keeng'
]);
}
}
if
(
empty
(
$limit
))
{
$limit
=
Constants
::
CRAWLER_LISTEN_LIMIT
;
}
else
{
$limit
=
-
1
;
}
echo
"
\n
Bắt đầu xử lý lấy lượt nghe từ: "
.
$from_page
;
echo
"
\n
Bắt đầu xử lý lấy lượt nghe từ: "
.
$from_page
;
$crawlered
=
$this
->
trackRepository
->
getCrawlerListenInserted
(
$from_page
,
false
);
$crawlered
=
$this
->
trackRepository
->
getCrawlerListenInserted
(
$from_page
,
false
);
...
@@ -62,18 +70,19 @@ class CrawlerTrackListenCommand extends Command
...
@@ -62,18 +70,19 @@ class CrawlerTrackListenCommand extends Command
echo
"
\n
Tổng số bài hát đã lấy lượt nghe hôm nay: "
.
count
(
$crawlered
);
echo
"
\n
Tổng số bài hát đã lấy lượt nghe hôm nay: "
.
count
(
$crawlered
);
// Lấy mảng bài hát theo từng hệ thống nhạc
// Lấy mảng bài hát theo từng hệ thống nhạc
$inserted
=
$this
->
trackRepository
->
getTrackBySource
(
$from_page
,
$crawlered
);
$inserted
=
$this
->
trackRepository
->
getTrackBySource
(
$from_page
,
$crawlered
,
$limit
);
$total
=
count
(
$inserted
);
echo
"
\n
Tổng số bài hát cần lấy lượt nghe: "
.
count
(
$inserted
)
;
echo
"
\n
Tổng số bài hát cần lấy lượt nghe: "
.
$total
;
if
(
count
(
$inserted
)
>
0
)
{
if
(
$total
>
0
)
{
$data_listen
=
[];
$data_listen
=
[];
foreach
(
$inserted
as
$key
=>
$track
)
{
foreach
(
$inserted
as
$key
=>
$track
)
{
$listen
=
$this
->
privateGetListen
(
$from_page
,
$track
);
$listen
=
$this
->
privateGetListen
(
$from_page
,
$track
);
if
(
$listen
>
0
)
{
if
(
$listen
>
0
)
{
$data_listen
[
$track
[
'id'
]]
=
$listen
;
$data_listen
[
$track
[
'id'
]]
=
$listen
;
}
}
echo
"
\n
"
.
(
$key
+
1
)
.
". "
.
$track
[
'title'
]
.
" (Track ID= "
.
$track
[
'id'
]
.
"): "
.
$listen
;
echo
"
\n
"
.
(
$key
+
1
)
.
"
/"
.
$total
.
"
. "
.
$track
[
'title'
]
.
" (Track ID= "
.
$track
[
'id'
]
.
"): "
.
$listen
;
}
}
if
(
count
(
$data_listen
)
>
0
)
{
if
(
count
(
$data_listen
)
>
0
)
{
...
...
app/Console/Kernel.php
View file @
8c349671
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
namespace
App\Console
;
namespace
App\Console
;
use
App\Console\Commands\CalculateDailyListenCommand
;
use
App\Console\Commands\CrawlerTrackListenCommand
;
use
App\Console\Commands\CrawlerTrackListenCommand
;
use
App\Console\Commands\CrontjobCommand
;
use
App\Console\Commands\CrontjobCommand
;
use
App\Console\Commands\Keeng\KeengCrawlerByCateCommand
;
use
App\Console\Commands\Keeng\KeengCrawlerByCateCommand
;
...
@@ -9,7 +10,6 @@ use App\Console\Commands\Keeng\KeengCrawlerNewRealeaseCommand;
...
@@ -9,7 +10,6 @@ use App\Console\Commands\Keeng\KeengCrawlerNewRealeaseCommand;
use
App\Console\Commands\Keeng\KeengCrawlerTrackCommand
;
use
App\Console\Commands\Keeng\KeengCrawlerTrackCommand
;
use
App\Console\Commands\Nct\NctCrawlerTrackCommand
;
use
App\Console\Commands\Nct\NctCrawlerTrackCommand
;
use
App\Console\Commands\NhacVn\NhacVnCrawlerTrackCommand
;
use
App\Console\Commands\NhacVn\NhacVnCrawlerTrackCommand
;
use
App\Console\Commands\Zing\ZingCrawlerListenCommand
;
use
App\Console\Commands\Zing\ZingCrawlerNewRealeaseCommand
;
use
App\Console\Commands\Zing\ZingCrawlerNewRealeaseCommand
;
use
App\Console\Commands\Zing\ZingCrawlerTrackCommand
;
use
App\Console\Commands\Zing\ZingCrawlerTrackCommand
;
use
App\Console\Commands\CrawlerTrackImageCommand
;
use
App\Console\Commands\CrawlerTrackImageCommand
;
...
@@ -25,18 +25,20 @@ class Kernel extends ConsoleKernel
...
@@ -25,18 +25,20 @@ class Kernel extends ConsoleKernel
*/
*/
protected
$commands
=
[
protected
$commands
=
[
CrontjobCommand
::
class
,
CrontjobCommand
::
class
,
ZingCrawlerTrackCommand
::
class
,
ZingCrawlerTrackCommand
::
class
,
ZingCrawlerNewRealeaseCommand
::
class
,
ZingCrawlerNewRealeaseCommand
::
class
,
NctCrawlerTrackCommand
::
class
,
NctCrawlerTrackCommand
::
class
,
NhacVnCrawlerTrackCommand
::
class
,
NhacVnCrawlerTrackCommand
::
class
,
KeengCrawlerTrackCommand
::
class
,
KeengCrawlerTrackCommand
::
class
,
CrawlerTrackImageCommand
::
class
,
//ZingCrawlerListenCommand::class,
CrawlerTrackListenCommand
::
class
,
KeengCrawlerByCateCommand
::
class
,
KeengCrawlerByCateCommand
::
class
,
KeengCrawlerNewRealeaseCommand
::
class
,
KeengCrawlerNewRealeaseCommand
::
class
,
CrawlerTrackImageCommand
::
class
,
CrawlerTrackListenCommand
::
class
,
CalculateDailyListenCommand
::
class
,
];
];
...
@@ -61,6 +63,27 @@ class Kernel extends ConsoleKernel
...
@@ -61,6 +63,27 @@ class Kernel extends ConsoleKernel
$schedule
->
command
(
'crawler:get-track-image'
)
->
hourlyAt
(
35
);
$schedule
->
command
(
'crawler:get-track-image'
)
->
hourlyAt
(
35
);
// 22h bắt đầu quét. Quét 3 lần, lần cuối quét hết
$schedule
->
command
(
'crawler:get-listen keeng'
)
->
dailyAt
(
'22:00'
);
$schedule
->
command
(
'crawler:get-listen keeng'
)
->
dailyAt
(
'22:05'
);
$schedule
->
command
(
'crawler:get-listen keeng'
)
->
dailyAt
(
'22:10'
);
$schedule
->
command
(
'crawler:get-listen keeng --limit=-1'
)
->
dailyAt
(
'22:15'
);
// 22h30 bắt đầu quét. Quét 3 lần, lần cuối quét hết
$schedule
->
command
(
'crawler:get-listen nct'
)
->
dailyAt
(
'22:30'
);
$schedule
->
command
(
'crawler:get-listen nct'
)
->
dailyAt
(
'22:35'
);
$schedule
->
command
(
'crawler:get-listen nct'
)
->
dailyAt
(
'22:40'
);
$schedule
->
command
(
'crawler:get-listen nct --limit=-1'
)
->
dailyAt
(
'22:45'
);
// 23h bắt đầu quét. Quét 3 lần, lần cuối quét hết
$schedule
->
command
(
'crawler:get-listen zing'
)
->
dailyAt
(
'23:00'
);
$schedule
->
command
(
'crawler:get-listen zing'
)
->
dailyAt
(
'23:05'
);
$schedule
->
command
(
'crawler:get-listen zing'
)
->
dailyAt
(
'23:10'
);
$schedule
->
command
(
'crawler:get-listen zing --limit=-1'
)
->
dailyAt
(
'23:15'
);
$schedule
->
command
(
'calculate:daily-listen'
)
->
dailyAt
(
'1:00'
);
$schedule
->
command
(
'calculate:daily-listen'
)
->
dailyAt
(
'1:10'
);
$schedule
->
command
(
'queue:work --once --timeout=120'
)
->
everyMinute
()
->
withoutOverlapping
();
$schedule
->
command
(
'queue:work --once --timeout=120'
)
->
everyMinute
()
->
withoutOverlapping
();
}
}
...
...
app/Models/CrawlerListen.php
View file @
8c349671
...
@@ -20,7 +20,8 @@ class CrawlerListen extends Model
...
@@ -20,7 +20,8 @@ class CrawlerListen extends Model
'month_zing'
,
'month_nct'
,
'week_nhacvn'
,
'month_keeng'
,
'month_zing'
,
'month_nct'
,
'week_nhacvn'
,
'month_keeng'
,
'year_zing'
,
'year_nct'
,
'year_nhacvn'
,
'year_keeng'
,
'year_zing'
,
'year_nct'
,
'year_nhacvn'
,
'year_keeng'
,
'alltime_zing'
,
'alltime_nct'
,
'alltime_nhacvn'
,
'alltime_keeng'
,
'alltime_zing'
,
'alltime_nct'
,
'alltime_nhacvn'
,
'alltime_keeng'
,
'zing_crawler_at'
,
'nct_crawler_at'
,
'nhacvn_crawler_at'
,
'keeng_crawler_at'
'zing_crawler_at'
,
'nct_crawler_at'
,
'nhacvn_crawler_at'
,
'keeng_crawler_at'
,
'date_moved_at'
];
];
}
}
app/Repositories/TrackRepository.php
View file @
8c349671
...
@@ -63,7 +63,8 @@ class TrackRepository extends BaseRepository
...
@@ -63,7 +63,8 @@ class TrackRepository extends BaseRepository
{
{
$query
=
Track
::
select
([
'id'
,
'art'
,
'id_zing'
,
'id_nct'
,
'id_keeng'
,
'src_thumbnail_medium'
])
$query
=
Track
::
select
([
'id'
,
'art'
,
'id_zing'
,
'id_nct'
,
'id_keeng'
,
'src_thumbnail_medium'
])
->
whereNull
(
'art'
)
->
whereNull
(
'art'
)
->
whereNotNull
(
'src_thumbnail_medium'
)
->
take
(
$limit
);
->
whereNotNull
(
'src_thumbnail_medium'
)
->
take
(
$limit
);
return
$query
->
get
()
->
toArray
();
return
$query
->
get
()
->
toArray
();
}
}
...
@@ -307,7 +308,9 @@ class TrackRepository extends BaseRepository
...
@@ -307,7 +308,9 @@ class TrackRepository extends BaseRepository
case
'keeng'
:
$query
->
whereNotNull
(
'id_keeng'
)
->
where
(
'id_keeng'
,
'<>'
,
''
);
break
;
case
'keeng'
:
$query
->
whereNotNull
(
'id_keeng'
)
->
where
(
'id_keeng'
,
'<>'
,
''
);
break
;
}
}
$query
->
take
((
$limit
<
1
)
?
Constants
::
CRAWLER_LISTEN_LIMIT
:
$limit
);
if
(
$limit
>
0
)
{
$query
->
take
(
$limit
);
}
return
$query
->
get
()
->
toArray
();
return
$query
->
get
()
->
toArray
();
...
@@ -364,5 +367,20 @@ class TrackRepository extends BaseRepository
...
@@ -364,5 +367,20 @@ class TrackRepository extends BaseRepository
return
collect
(
$result
)
->
map
(
function
(
$item
)
{
return
$item
->
track_id
;
})
->
all
();
return
collect
(
$result
)
->
map
(
function
(
$item
)
{
return
$item
->
track_id
;
})
->
all
();
}
}
public
function
getCrawlerListenNotYetMoved
(
$max_date
=
null
)
{
if
(
empty
(
$max_date
))
{
$max_date
=
date
(
'Y-m-d 00:00:00'
);
}
else
{
$max_date
=
date
(
'Y-m-d 23:59:59'
,
strtotime
(
$max_date
));
}
return
CrawlerListen
::
select
(
'*'
)
->
where
(
'date_moved_at'
,
'<='
,
$max_date
)
->
orWhereNull
(
'date_moved_at'
)
->
take
(
5000
)
->
get
();
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment