Files
nuhodds_fi4/app/Traits/Sofascore/GrappingTeams.php
T
2026-10-13 08:06:07 +02:00

102 lines
2.8 KiB
PHP

<?php
namespace App\Traits\Sofascore;
use App\Models\SofaScoreRosa;
use App\Models\SofaScoreRosaTournament;
use App\Models\SofaScoreTournament;
use App\Services\CloudflareSolverService;
use App\Utilities\Notice;
use Illuminate\Support\Facades\File;
use function Laravel\Prompts\select;
trait GrappingTeams
{
protected int $max = 2000;
public function team_rose()
{
// LaLiga 8
// Ita A 23
// Ita B 53
// Eng Prem 17
// Bundesliga 35
// FRA Ligue 1 34
// ENG1, ESP1, ITA1, ITA2, FRA1 e GER1
$major = [8, 17, 23, 34, 35, 53];
$what = $this->select_what_to_grap();
if ($this->restart) {
SofaScoreRosa::query()->update(['grapped' => 0]);
File::cleanDirectory($this->data_dir);
}
switch ($what) {
case 'major':
$tournaments = SofaScoreTournament::where('enabled', 1)->whereIn('tournament_id', $major)->pluck('tournament_id')->toArray();
break;
default:
$tournaments = SofaScoreTournament::where('enabled', 1)->whereNotIn('tournament_id', $major)->pluck('tournament_id')->toArray();
break;
}
$tmp = SofaScoreRosaTournament::whereIn('ss_tournament_id', $tournaments)->pluck('ss_team_id')->toArray();
$teams = SofaScoreRosa::where('is_active', 1)->whereIn('ss_team_id', $tmp)->where('grapped', 0)->get();
$count = \count($teams);
$cloudflareService = new CloudflareSolverService;
foreach ($teams as $key => $value) {
if ($this->debug) {
$key++;
echo "\n$key/$count\n";
}
$url = $this->base_url . "api/v1/team/$value->ss_team_id/players";
if ($this->debug) {
Notice::note($url, 'b');
}
$data = $cloudflareService->getJsonFromUrl($url);
$data['ss_team_id'] = $value->ss_team_id;
$data['ss_team_name'] = $value->ss_team_name;
/*********************************
* add team to folder
*********************************/
File::put($this->data_dir . '/' . $this->clean_string($value->ss_team_name) . '.json', json_encode($data));
SofaScoreRosa::where('id', $value->id)->update(['grapped' => 1]);
$this->grapper_sleep($this->speed);
}
$this->import_to_excel("rose_$what");
if ($this->debug) {
Notice::note('DONE', 'bb');
}
}
protected function select_what_to_grap(): string
{
return select(
label: 'What teams should be grapped?',
options: [
'major' => 'Major Leagues',
'others' => 'Others Leagues',
]
);
}
}