| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Models\LhcLottery as LhcLotteryModel;
- use Illuminate\Support\Facades\Log;
- class LhcLottery extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'lhc:lottery';
-
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '六合彩开奖数据抓取,每日21:36后开始执行一次';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $num = 0;
- while ($num < 10) {
- $num ++;
- $res = $this->spider();
- echo date('Y-m-d H:i:s').' 第'.$num.'次尝试,结果:'.$res.PHP_EOL;
- if ($res === false) {
- sleep(60);
- } else {
- break;
- }
- }
- }
-
- public function spider()
- {
- $url = 'https://api.bjjfnet.com/data/opencode/2032';
- try {
- $json = file_get_contents($url);
- $data = json_decode($json, true);
- if (empty($data)) {
- throw new \Exception('六合彩 Empty data');
- }
- if ($data['code'] != 0) {
- throw new \Exception('六合彩 code 不为 0');
- }
- $lotteries = array_reverse($data['data']);
- foreach ($lotteries as $lottery) {
- $exists = LhcLotteryModel::where('issue', $lottery['issue'])->first();
- if ($exists) {
- continue;
- }
- $mode = new LhcLotteryModel;
- $mode->issue = $lottery['issue'];
- $mode->open_code = $lottery['openCode'];
- $mode->open_time = strtotime($lottery['openTime']);
- $mode->save();
- }
- } catch (\Exception $e) {
- Log::error("六合彩开奖数据抓取失败:".$e->getMessage());
- return false;
- }
- return true;
- }
- }
|