162 lines
5.6 KiB
PHP
162 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Sofascore;
|
|
|
|
use App\Models\SofaScoreDayTournament;
|
|
use App\Models\SofaScoreEventGrapped;
|
|
use App\Models\SofaScoreTaskDay;
|
|
use App\Services\CloudflareSolverService;
|
|
use App\Utilities\Notice;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
trait GrapperEvent
|
|
{
|
|
protected function get_event(string $day, array $tournament)
|
|
{
|
|
$res = SofaScoreDayTournament::whereDate('day', '=', $day)->where('tournament_id', $tournament['sofa_score_tournament_id'])->first();
|
|
|
|
if ($res) {
|
|
|
|
if ($this->debug) {
|
|
Notice::note("$day - " . $tournament['tournament']['name'] . ' - ' . $tournament['sofa_score_tournament_id'], 'bb');
|
|
}
|
|
|
|
$this->sleep(1);
|
|
|
|
$url = $this->base_url . 'api/v1/unique-tournament/' . $tournament['sofa_score_tournament_id'] . "/scheduled-events/$day";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'b');
|
|
}
|
|
|
|
$this->sleep(1);
|
|
|
|
$cloudflareService = new CloudflareSolverService;
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
if (isset($data['events'])) {
|
|
|
|
foreach ($data['events'] as $event) {
|
|
|
|
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'];
|
|
|
|
$url = $this->base_url . "api/v1/event/$event_id/statistics";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'b');
|
|
}
|
|
|
|
$this->sleep();
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
$data['id'] = $event_id;
|
|
|
|
File::put("$folder/statistics.json", json_encode($data));
|
|
|
|
/*********************************
|
|
* EVENT lineups
|
|
*********************************/
|
|
|
|
$url = $this->base_url . "api/v1/event/$event_id/lineups";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'b');
|
|
}
|
|
|
|
$this->sleep();
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
$data['id'] = $event_id;
|
|
|
|
File::put("$folder/lineups.json", json_encode($data));
|
|
|
|
/*********************************
|
|
* EVENT odds
|
|
*********************************/
|
|
|
|
$url = $this->base_url . "api/v1/event/$event_id/odds/1/all";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'b');
|
|
}
|
|
|
|
$this->sleep();
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
$data['id'] = $event_id;
|
|
|
|
File::put("$folder/odds.json", json_encode($data));
|
|
|
|
/*********************************
|
|
* EVENT incidents
|
|
*********************************/
|
|
|
|
$url = $this->base_url . "api/v1/event/$event_id/incidents";
|
|
|
|
if ($this->debug) {
|
|
Notice::note($url, 'b');
|
|
}
|
|
|
|
$this->sleep();
|
|
|
|
$data = $cloudflareService->getJsonFromUrl($url);
|
|
|
|
$data['id'] = $event_id;
|
|
|
|
File::put("$folder/incidents.json", json_encode($data));
|
|
|
|
$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(),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
SofaScoreTaskDay::updateOrCreate([
|
|
'grapper_id' => (int) $this->task_id,
|
|
'day' => $day,
|
|
], [
|
|
'done' => 1,
|
|
]);
|
|
}
|
|
}
|