大家晚上好~
今天不再拖了!就把這個趕快做完吧!
前幾天我們做完了推送通知和簡易爬蟲,今天就把他們參在一起做撒尿牛丸!
請服用!
所以我們就直接來囉!
php artisan make:command PushAnimationNotification
<?php
namespace App\Console\Commands;
use App\Services\CrawlerService;
use App\Services\LineBotService;
use Illuminate\Console\Command;
class PushAnimationNotification extends Command
{
(...略)
private $path;
/** @var CrawlerService */
private $crawlerService;
/** @var LineBotService */
private $lineBotService;
/**
* PushAnimationNotification constructor.
* @param CrawlerService $crawlerService
* @param LineBotService $lineBotService
*/
public function __construct(CrawlerService $crawlerService, LineBotService $lineBotService)
{
parent::__construct();
$this->path = config('services.url.baHa');
$this->crawlerService = $crawlerService;
$this->lineBotService = $lineBotService;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$originalData = $this->crawlerService->getOriginalData($this->path);
$list = $this->crawlerService->getNewAnimationFromBaHa($originalData);
$today = today()->format('m/d');
$existedList = [];
$list = array_map(function ($d) use ($today, &$existedList) {
if (false === strpos($d['date'], $today) ||
in_array($d['label'], $existedList, true)
) {
return null;
}
$existedList[] = $d['label'];
if (mb_strlen($d['label'], 'UTF-8') > 12) {
$d['label'] = mb_substr($d['label'], 0, 9, 'UTF-8') . '...';
}
return $d;
}, $list);
$target = array_filter($list, function ($d) {
return null !== $d;
});
$message = "{$today} 最新動畫來囉!";
$messageBuilders = $this->lineBotService->buildTemplateMessageBuilder($target, $message);
foreach ($messageBuilders as $target) {
$this->lineBotService->pushMessage($target);
}
echo "Good luck!\n";
}
}
我們可以很快速的稍微調整一下爬取的資訊,接著就直接推送訊息!
但要怎樣加入排程在Heroku呢?
很簡單,來看圖說故事囉~
按一按之後,會到下圖!
沒有錯!就直接加入排程進去即可!最短可以設定為每十分鐘呼叫一次!
php artisan schedule:push-baha-animation
原本以為會需要設定Scheduler,但看來都不用做這些了,透過Heroku的WEB介面就可以達成了!
現在大家可以在收到最新的動漫後去洗彈幕啦~
原本的排程資訊有些不夠使用,有興趣可以去看一下連結,在推送通知上有做些小調整!
今天沒什麼重點,就專心實作吧~