199 lines
8.4 KiB
PHP
199 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Sofascore;
|
|
|
|
use App\Models\SofaScoreEventGrapped;
|
|
use App\Services\CloudflareSolverService;
|
|
use App\Utilities\Notice;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
trait GrappingEvent
|
|
{
|
|
protected function get_event(string $day, array $tournament)
|
|
{
|
|
foreach ($this->day_tournaments as $day_tournament) {
|
|
|
|
if ($day_tournament->tournament_id == $tournament['sofa_score_tournament_id']) {
|
|
|
|
if ($this->debug) {
|
|
Notice::note("$day - ".$tournament['tournament']['name'].' - '.$tournament['sofa_score_tournament_id'], 'bb');
|
|
}
|
|
|
|
$url = $this->base_url.'api/v1/unique-tournament/'.$tournament['sofa_score_tournament_id']."/scheduled-events/$day";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'p');
|
|
}
|
|
|
|
$this->grapper_sleep($this->speed);
|
|
|
|
$cloudflareService = new CloudflareSolverService;
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
if (isset($data['events'])) {
|
|
|
|
foreach ($data['events'] as $event) {
|
|
|
|
$base_check_string = $day.'-'.$event['tournament']['category']['slug'].'-'.$event['tournament']['slug'].'/'.$event['slug'];
|
|
|
|
$res = SofaScoreEventGrapped::where('id', (int) $event['id'])->where('grapper_id', $this->task_id)->first();
|
|
|
|
if (! $res) {
|
|
|
|
$base_check_string = $day.'-'.$event['tournament']['category']['slug'].'-'.$event['tournament']['slug'].'/'.$event['slug'];
|
|
|
|
// chinese-super-league
|
|
|
|
if (! $this->restart && SofaScoreEventGrapped::where('grapper_id', $this->task_id)->where('id', (int) $event['id'])->exists()) {
|
|
if ($this->debug) {
|
|
Notice::note('DB - Allready grapped at '.$day.' - '.$event['homeTeam']['name'].' vs '.$event['awayTeam']['name'], 'b');
|
|
}
|
|
} else {
|
|
|
|
if ($this->debug) {
|
|
Notice::note("$day - ".$event['homeTeam']['name'].' vs '.$event['awayTeam']['name'], 'g');
|
|
}
|
|
|
|
$folder = strtolower("$this->data_dir/$day/$day-".$event['tournament']['category']['slug'].'-'.$event['tournament']['slug'].'/'.$event['slug']);
|
|
|
|
File::makeDirectory($folder, 0775, true, true);
|
|
|
|
/*********************************
|
|
* EVENT event
|
|
*********************************/
|
|
|
|
File::put("$folder/event.json", json_encode($event));
|
|
|
|
/*********************************
|
|
* EVENT statistics
|
|
*********************************/
|
|
|
|
$event_id = $event['id'];
|
|
|
|
if (! $this->grapped($base_check_string.'/statistics.json')) {
|
|
|
|
$url = $this->base_url."api/v1/event/$event_id/statistics";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'b');
|
|
}
|
|
|
|
$this->grapper_sleep();
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
$data['id'] = $event_id;
|
|
|
|
File::put("$folder/statistics.json", json_encode($data));
|
|
} else {
|
|
if ($this->debug) {
|
|
Notice::note('already grapped statistics.json', 'y');
|
|
}
|
|
}
|
|
|
|
/*********************************
|
|
* EVENT lineups
|
|
*********************************/
|
|
|
|
if (! $this->grapped($base_check_string.'/lineups.json')) {
|
|
|
|
$url = $this->base_url."api/v1/event/$event_id/lineups";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'b');
|
|
}
|
|
|
|
$this->grapper_sleep();
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
$data['id'] = $event_id;
|
|
|
|
File::put("$folder/lineups.json", json_encode($data));
|
|
} else {
|
|
if ($this->debug) {
|
|
Notice::note('already grapped lineups.json', 'y');
|
|
}
|
|
}
|
|
|
|
/*********************************
|
|
* EVENT odds
|
|
*********************************/
|
|
|
|
if (! $this->grapped($base_check_string.'/odds.json')) {
|
|
|
|
$url = $this->base_url."api/v1/event/$event_id/odds/1/all";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'b');
|
|
}
|
|
|
|
$this->grapper_sleep();
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
$data['id'] = $event_id;
|
|
|
|
File::put("$folder/odds.json", json_encode($data));
|
|
} else {
|
|
if ($this->debug) {
|
|
Notice::note('already grapped odds.json', 'y');
|
|
}
|
|
}
|
|
/*********************************
|
|
* EVENT incidents
|
|
*********************************/
|
|
|
|
if (! $this->grapped($base_check_string.'/incidents.json')) {
|
|
|
|
$url = $this->base_url."api/v1/event/$event_id/incidents";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'b');
|
|
}
|
|
|
|
$this->grapper_sleep();
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
$data['id'] = $event_id;
|
|
|
|
File::put("$folder/incidents.json", json_encode($data));
|
|
} else {
|
|
if ($this->debug) {
|
|
Notice::note('already grapped incidents.json', 'y');
|
|
}
|
|
}
|
|
|
|
$starttime = Carbon::createFromTimestamp($event['startTimestamp']);
|
|
|
|
SofaScoreEventGrapped::updateOrCreate([
|
|
'id' => (int) $event['id'],
|
|
], [
|
|
'grapper_id' => $this->task_id,
|
|
'day' => $day,
|
|
'liga' => $event['tournament']['name'],
|
|
'name' => $event['homeTeam']['name'].' vs '.$event['awayTeam']['name'],
|
|
'url' => $url,
|
|
'starttime' => $starttime->toDateTimeString(),
|
|
]);
|
|
}
|
|
} else {
|
|
if ($this->debug) {
|
|
Notice::note('allready in db event grapped: '.$base_check_string, 'p');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
if ($this->debug) {
|
|
Notice::note($day.' - '.$tournament['tournament']['name'].' no events', 'y');
|
|
}
|
|
}
|
|
}
|