462 lines
11 KiB
PHP
462 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Sofascore;
|
|
|
|
use App\Mail\SimpleMail;
|
|
use App\Models\SofaScoreEventGrapped;
|
|
use App\Models\SofaScoreTaskDay;
|
|
use App\Utilities\Notice;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
use function Laravel\Prompts\select;
|
|
use function Laravel\Prompts\text;
|
|
|
|
trait GrappingSofascore
|
|
{
|
|
use GrappingEvent;
|
|
use GrappingHelper;
|
|
use GrappingMail;
|
|
use GrappingTeams;
|
|
use GrappingToExcel;
|
|
use GrappingTournaments;
|
|
|
|
protected int $debug;
|
|
|
|
protected int $restart;
|
|
|
|
protected int $speed;
|
|
|
|
protected int $force;
|
|
|
|
protected int $task_id;
|
|
|
|
protected int $days_back = 0;
|
|
|
|
protected string $base_dir = 'sofascore_data';
|
|
|
|
protected string $data_dir = '';
|
|
|
|
protected string $base_url = 'https://www.sofascore.com/';
|
|
|
|
protected array $tournaments_requested = [];
|
|
|
|
protected array $grapped_files = [];
|
|
|
|
protected Collection $day_tournaments;
|
|
|
|
protected array $days = [];
|
|
|
|
public function handle(): void
|
|
{
|
|
ini_set('memory_limit', '2048M');
|
|
|
|
set_time_limit(0);
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
ini_set('display_errors', 1);
|
|
|
|
DB::connection()->disableQueryLog();
|
|
|
|
$this->debug = $this->options()['d'] ?? 0;
|
|
|
|
$this->restart = $this->options()['r'] ?? 0;
|
|
|
|
$this->force = $this->options()['f'] ?? 0;
|
|
|
|
$grapperSelected = $this->select_grapper();
|
|
|
|
$this->set_base_dir($grapperSelected);
|
|
|
|
switch ($grapperSelected) {
|
|
case 'daily':
|
|
$this->daily_grapper_nobreak();
|
|
break;
|
|
|
|
case 'back':
|
|
$this->task_id = 1;
|
|
$this->days_back = (int) $this->select_day();
|
|
$this->run_days_back($grapperSelected);
|
|
break;
|
|
|
|
case 'range':
|
|
$this->task_id = 3;
|
|
$this->range_grapper_nobreak();
|
|
break;
|
|
|
|
case 'rose':
|
|
$this->team_rose();
|
|
break;
|
|
|
|
case 'settings':
|
|
$this->settings_to_local();
|
|
break;
|
|
|
|
case 'excel':
|
|
$folder = $this->select_folder();
|
|
|
|
$this->import_to_excel($folder);
|
|
break;
|
|
|
|
case 'mail':
|
|
$folder = $this->select_folder();
|
|
|
|
$this->send($folder);
|
|
break;
|
|
|
|
case 'clear':
|
|
$folder = $this->select_folder();
|
|
|
|
$this->clear_folder($folder);
|
|
break;
|
|
|
|
case 'delete_orphants':
|
|
$this->delete_imported_files_orphants();
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Summary of daily_grapper_nobreak
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function daily_grapper_nobreak()
|
|
{
|
|
$folder = 'daily';
|
|
|
|
$this->task_id = 1;
|
|
|
|
$this->set_base_dir($folder);
|
|
|
|
$this->load_tournaments_requested($folder);
|
|
|
|
$restart = $this->ask_restart();
|
|
|
|
$this->speed = $this->ask_speed();
|
|
|
|
$this->debug = $this->ask_debug() == 'yes' ? 1 : 0;
|
|
|
|
$daily_way = $this->select_daily_days();
|
|
|
|
if ($this->ask_settings_download() == 'yes') {
|
|
$this->settings_to_local();
|
|
}
|
|
|
|
switch ($restart) {
|
|
case 'restart':
|
|
$this->restart = 1;
|
|
|
|
SofaScoreEventGrapped::truncate();
|
|
break;
|
|
case 'clear':
|
|
$this->restart = 1;
|
|
|
|
SofaScoreEventGrapped::truncate();
|
|
|
|
$this->clear_folder($folder, 'all');
|
|
break;
|
|
}
|
|
|
|
/**
|
|
* day 1
|
|
*/
|
|
if (in_array($daily_way, [1, 2])) {
|
|
$day = Carbon::now()->subDay()->format('Y-m-d');
|
|
|
|
$this->grapped_files = $this->get_tree($this->data_dir.'/'.$day);
|
|
|
|
$this->grap_tournaments_for_day($day);
|
|
|
|
foreach ($this->tournaments_requested as $tournament) {
|
|
$this->get_event($day, $tournament);
|
|
}
|
|
SofaScoreTaskDay::updateOrCreate(['grapper_id' => (int) $this->task_id, 'day' => $day], ['done' => 1]);
|
|
}
|
|
/**
|
|
* day 5
|
|
*/
|
|
if (in_array($daily_way, [1, 3])) {
|
|
$day = Carbon::now()->subDays(5)->format('Y-m-d');
|
|
|
|
$this->grapped_files = $this->get_tree($this->data_dir.'/'.$day);
|
|
|
|
$this->grap_tournaments_for_day($day);
|
|
|
|
foreach ($this->tournaments_requested as $tournament) {
|
|
$this->get_event($day, $tournament);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* to excel day by day
|
|
*/
|
|
$this->import_to_excel($folder);
|
|
|
|
/**
|
|
* mail it
|
|
*/
|
|
$this->send($folder);
|
|
}
|
|
|
|
/**
|
|
* Summary of run_days_back
|
|
*/
|
|
protected function run_days_back(string $grapperSelected)
|
|
{
|
|
$this->load_tournaments_requested($grapperSelected);
|
|
|
|
$this->load_days($grapperSelected);
|
|
|
|
$this->speed = $this->ask_speed();
|
|
|
|
if ($this->confirm($this->days[0], true)) {
|
|
|
|
foreach ($this->days as $day) {
|
|
|
|
$this->grap_tournaments_for_day($day);
|
|
|
|
foreach ($this->tournaments_requested as $tournament) {
|
|
$this->get_event($day, $tournament);
|
|
}
|
|
|
|
SofaScoreTaskDay::updateOrCreate(['grapper_id' => (int) $this->task_id, 'day' => $day], ['done' => 1]);
|
|
}
|
|
|
|
if ($this->debug) {
|
|
Notice::note('Day grapping compleated', 'g');
|
|
}
|
|
} else {
|
|
Notice::note('day grapping cancelled', 'r');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Summary of run_range
|
|
*/
|
|
protected function range_grapper_nobreak()
|
|
{
|
|
$folder = 'range';
|
|
|
|
$this->task_id = 3;
|
|
|
|
$this->set_base_dir($folder);
|
|
|
|
$restart = $this->ask_restart();
|
|
switch ($restart) {
|
|
case 'restart':
|
|
$this->restart = 1;
|
|
|
|
SofaScoreTaskDay::where('grapper_id', (int) $this->task_id)->delete();
|
|
|
|
SofaScoreEventGrapped::truncate();
|
|
|
|
break;
|
|
case 'clear':
|
|
if ($this->confirm_delete() == 'Yes') {
|
|
$this->restart = 1;
|
|
$this->clear_folder($folder, 'all');
|
|
|
|
SofaScoreEventGrapped::truncate();
|
|
|
|
SofaScoreTaskDay::where('grapper_id', (int) $this->task_id)->delete();
|
|
}
|
|
break;
|
|
}
|
|
|
|
$this->speed = $this->ask_speed();
|
|
|
|
$day_done = $this->days_done();
|
|
|
|
$this->load_tournaments_requested($folder);
|
|
|
|
$this->load_days($folder);
|
|
|
|
$count = \count($this->days);
|
|
foreach ($this->days as $key => $day) {
|
|
if ($this->debug) {
|
|
$key++;
|
|
Notice::note("$key/$count", 'br');
|
|
}
|
|
|
|
if (in_array($day, $day_done)) {
|
|
if ($this->debug) {
|
|
Notice::note("$day Events allready grapped", 'br');
|
|
}
|
|
} else {
|
|
|
|
$this->grapped_files = $this->get_tree($this->data_dir.'/'.$day);
|
|
|
|
$this->grap_tournaments_for_day($day);
|
|
|
|
foreach ($this->tournaments_requested as $tournament) {
|
|
$this->get_event($day, $tournament);
|
|
}
|
|
|
|
SofaScoreTaskDay::updateOrCreate(['grapper_id' => (int) $this->task_id, 'day' => $day], ['done' => 1]);
|
|
}
|
|
}
|
|
|
|
$this->import_to_excel($folder);
|
|
|
|
Notice::note('Done', 'g');
|
|
|
|
Mail::to('hermann.telser@gmail.com')->send(new SimpleMail('STATISTIKO', 'Grapping compleated'));
|
|
}
|
|
|
|
protected function days_done(): array
|
|
{
|
|
$directories = File::directories($this->data_dir);
|
|
$arr = [];
|
|
foreach ($directories as $directory) {
|
|
$arr[] = substr(strrchr($directory, '/'), 1);
|
|
}
|
|
|
|
return $arr;
|
|
}
|
|
|
|
/**
|
|
* Summary of select_grapper
|
|
*/
|
|
protected function select_grapper(): string
|
|
{
|
|
return select(
|
|
label: 'What task should run?',
|
|
scroll: 10,
|
|
options: [
|
|
'daily' => 'Daily',
|
|
'back' => 'Day back',
|
|
'range' => 'Range',
|
|
'rose' => 'Rose',
|
|
'player' => 'Player',
|
|
'settings' => 'Settings',
|
|
'excel' => 'Excel',
|
|
'mail' => 'Mail',
|
|
'clear' => 'Clear Folder',
|
|
'delete_orphants' => 'Delete Imported',
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Summary of select_folder
|
|
*/
|
|
protected function select_folder(): string
|
|
{
|
|
return select(
|
|
label: 'What folder should be used?',
|
|
scroll: 10,
|
|
options: [
|
|
'daily' => 'Daily',
|
|
'range' => 'Range',
|
|
'rose' => 'Rose',
|
|
'player' => 'Player',
|
|
]
|
|
);
|
|
}
|
|
|
|
protected function select_daily_days(): string
|
|
{
|
|
$yesterday = Carbon::yesterday()->format('Y-m-d');
|
|
$moredays = Carbon::now()->subDays(5)->format('Y-m-d');
|
|
|
|
return select(
|
|
label: 'What days should be grapped?',
|
|
scroll: 10,
|
|
default: 1,
|
|
options: [
|
|
1 => "$yesterday & $moredays",
|
|
2 => $yesterday,
|
|
3 => $moredays,
|
|
]
|
|
);
|
|
}
|
|
|
|
protected function select_day(): string
|
|
{
|
|
return text(
|
|
label: 'How many days back?',
|
|
placeholder: 'z.B. 1',
|
|
default: '1',
|
|
);
|
|
}
|
|
|
|
protected function ask_restart(): string
|
|
{
|
|
return select(
|
|
label: 'Restart Grapper?',
|
|
default: 'no',
|
|
options: [
|
|
'no' => 'No',
|
|
'restart' => 'Restart',
|
|
'clear' => 'Clear folder and restart',
|
|
]
|
|
);
|
|
}
|
|
|
|
protected function ask_speed(): int
|
|
{
|
|
return select(
|
|
label: 'How fast you want to go?',
|
|
default: 1,
|
|
options: [
|
|
1 => 'Fast (5 -10 sec)',
|
|
2 => 'Middle (10 - 20 sec)',
|
|
3 => 'Slow (30 - 50 sec)',
|
|
]
|
|
);
|
|
}
|
|
|
|
protected function ask_debug(): string
|
|
{
|
|
return select(
|
|
label: 'Show debubing?',
|
|
default: 'yes',
|
|
options: [
|
|
'no' => 'No',
|
|
'yes' => 'Yes',
|
|
]
|
|
);
|
|
}
|
|
|
|
protected function select_what_delete(): string
|
|
{
|
|
return select(
|
|
label: 'What files?',
|
|
scroll: 10,
|
|
options: [
|
|
'all' => 'All',
|
|
'data' => 'Data',
|
|
]
|
|
);
|
|
}
|
|
|
|
protected function ask_settings_download(): string
|
|
{
|
|
return select(
|
|
label: 'Download setting?',
|
|
default: 'no',
|
|
options: [
|
|
'no' => 'No',
|
|
'yes' => 'Yes',
|
|
]
|
|
);
|
|
}
|
|
|
|
protected function confirm_delete(): string
|
|
{
|
|
return select(
|
|
label: 'Confirm delete!',
|
|
default: 'no',
|
|
options: [
|
|
'no' => 'No',
|
|
'yes' => 'Yes',
|
|
]
|
|
);
|
|
}
|
|
}
|