Files
nuhodds_fi4/app/Console/Commands/Grapper/Betsapi/Fixtures.php
T

113 lines
4.0 KiB
PHP

<?php
namespace App\Console\Commands\Grapper\Betsapi;
use App\Console\Commands\Curl;
use App\Console\Commands\Grapper\Fixtures\FindFixture;
use App\Console\Commands\Nuh;
use App\Models\Country;
use App\Models\Mapper;
use App\Models\Sport;
use App\Utilities\Notice;
use Illuminate\Support\Carbon;
class Fixtures
{
public static function grap()
{
$debug = Nuh::$options['d'] ?? 0;
$today = Nuh::$options['t'] ?? 0;
FindFixture::start();
/**
* Delete Started
*/
$info = Mapper::whereDate('starttime', '<', Carbon::now())->delete();
if ($debug) {
Notice::note("Deleted: $info");
}
$country_map = Country::pluck('id', 'iso2');
$sportmap = Sport::map('bapi', 0, [1, 2, 3, 5]);
$now = Carbon::now();
for ($i = 0; $i < 30; $i++) {
if ($debug) {
Notice::note('Day: '.$now->format('d. M Y'), 'g');
}
$base_url = 'https://api.b365api.com/v3/events/upcoming?sport_id=###sport_id###&page=###page###&day='.$now->format('Ymd').'&skip_esports=1&token='.config('env.betsapi_token');
$now->addDays(1);
$total = 1;
foreach ($sportmap as $sport_id => $betsapi_sport_id) {
$sport_url = str_replace('###sport_id###', $betsapi_sport_id, $base_url);
$page = 1;
while ($page <= $total) {
$url = str_replace('###page###', $page, $sport_url);
$data = Curl::load($url);
if (! isset($data['pager'])) {
return;
}
$page++;
if (isset($data['pager']['total'])) {
$total = round($data['pager']['total'] / $data['pager']['per_page']);
if ($debug) {
Notice::note("Page: $page \nTotal: $total");
}
if (isset($data['results']) && \count($data['results'])) {
foreach ($data['results'] as $row) {
if (isset($row['home']['id']) && isset($row['away']['id']) && strpos($row['league']['name'], 'Esoccer') === false) {
$cc = strtoupper($row['league']['cc']);
$event = [
'event_id' => (int) $row['id'],
'bet365_id' => isset($row['bet365_id']) ? $row['bet365_id'] : 0,
'sport_id' => (int) $sport_id,
'provider_id' => (int) Nuh::$options['provider_id'],
'starttime' => Carbon::createFromTimestamp((string) $row['time'])->toDateTimeString(),
'country_id' => $country_map[$cc] ?? 0,
'home' => $row['home']['name'] ?? '',
'home_id' => (int) $row['home']['id'],
'home_cc' => strtoupper($row['home']['cc']) ?? '',
'away' => $row['away']['name'] ?? '',
'away_id' => (int) $row['away']['id'],
'away_cc' => strtoupper($row['away']['cc']) ?? '',
'group' => $row['league']['name'] ?? '',
'group_id' => (int) $row['league']['id'] ?? 0,
'group_cc' => strtoupper($row['league']['cc']) ?? '',
];
FindFixture::find($event);
}
}
}
}
}
}
if ($today) {
break;
}
}
}
}