294 lines
6.9 KiB
PHP
294 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Sofascore;
|
|
|
|
use App\Utilities\Notice;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use function Laravel\Prompts\select;
|
|
use function Laravel\Prompts\text;
|
|
|
|
trait GrapSofascore
|
|
{
|
|
use GrapperEvent;
|
|
use GrappingHelper;
|
|
use GrappingMail;
|
|
use GrappingToExcel;
|
|
|
|
protected int $debug;
|
|
|
|
protected int $restart;
|
|
|
|
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 $days = [];
|
|
|
|
public function handle(): void
|
|
{
|
|
error_reporting(E_ALL);
|
|
|
|
ini_set('display_errors', 1);
|
|
|
|
$this->debug = $this->options()['d'] ?? 0;
|
|
|
|
$this->restart = $this->options()['r'] ?? 0;
|
|
|
|
$this->force = $this->options()['f'] ?? 0;
|
|
|
|
switch ($this->getName()) {
|
|
case 'grp:daily':
|
|
$this->daily_grapper_nobreak();
|
|
break;
|
|
|
|
default:
|
|
// code...
|
|
|
|
$grapperSelected = $this->select_grapper();
|
|
|
|
$this->set_base_dir($grapperSelected);
|
|
|
|
switch ($grapperSelected) {
|
|
case 'daily':
|
|
$this->task_id = 1;
|
|
|
|
$this->days_back = (int) $this->select_day();
|
|
|
|
$this->run_daily($grapperSelected);
|
|
break;
|
|
|
|
case 'range':
|
|
$this->task_id = 3;
|
|
$this->range_grapper_nobreak();
|
|
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;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Summary of daily_grapper_nobreak
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function daily_grapper_nobreak()
|
|
{
|
|
$folder = 'daily';
|
|
|
|
$this->task_id = 1;
|
|
|
|
$this->restart = 1;
|
|
|
|
$this->set_base_dir($folder);
|
|
|
|
$this->load_tournaments_requested($folder);
|
|
|
|
/**
|
|
* day 1
|
|
*/
|
|
$this->clear_folder($folder, 'all');
|
|
|
|
$day = Carbon::now()->subDay()->format('Y-m-d');
|
|
|
|
$this->grap_tournaments_for_day($day);
|
|
|
|
foreach ($this->tournaments_requested as $tournament) {
|
|
$this->get_event($day, $tournament);
|
|
}
|
|
|
|
$this->import_to_excel($folder, $day);
|
|
|
|
/**
|
|
* day 5
|
|
*/
|
|
$this->clear_folder($folder, 'data');
|
|
|
|
$day = Carbon::now()->subDays(5)->format('Y-m-d');
|
|
|
|
$this->grap_tournaments_for_day($day);
|
|
|
|
foreach ($this->tournaments_requested as $tournament) {
|
|
$this->get_event($day, $tournament);
|
|
}
|
|
|
|
$this->import_to_excel($folder, $day);
|
|
|
|
$this->send($folder);
|
|
}
|
|
|
|
/**
|
|
* Summary of run_daily
|
|
*/
|
|
protected function run_daily(string $grapperSelected)
|
|
{
|
|
$this->load_tournaments_requested($grapperSelected);
|
|
|
|
$this->load_days($grapperSelected);
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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();
|
|
|
|
if ($restart == 'yes') {
|
|
$this->restart = 1;
|
|
$this->clear_folder($folder, 'all');
|
|
}
|
|
|
|
$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');
|
|
}
|
|
|
|
$this->grap_tournaments_for_day($day);
|
|
|
|
foreach ($this->tournaments_requested as $tournament) {
|
|
$this->get_event($day, $tournament);
|
|
}
|
|
}
|
|
|
|
$this->import_to_excel($folder);
|
|
|
|
Notice::note('Done', 'g');
|
|
}
|
|
|
|
/**
|
|
* Summary of select_grapper
|
|
*/
|
|
protected function select_grapper(): string
|
|
{
|
|
return select(
|
|
label: 'What task should run?',
|
|
scroll: 10,
|
|
options: [
|
|
'daily' => 'Select day back',
|
|
'range' => 'Range',
|
|
'rose' => 'Rose',
|
|
'player' => 'Player',
|
|
'settings' => 'Settings',
|
|
'excel' => 'Excel',
|
|
'mail' => 'Send Mail',
|
|
'clear' => 'Empty Folder',
|
|
'delete_orphants' => 'Delete Imported Orphants',
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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_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: [
|
|
'yes' => 'Yes',
|
|
'no' => 'No',
|
|
]
|
|
);
|
|
}
|
|
|
|
protected function select_what_delete(): string
|
|
{
|
|
return select(
|
|
label: 'What files?',
|
|
scroll: 10,
|
|
options: [
|
|
'all' => 'All',
|
|
'data' => 'Data',
|
|
]
|
|
);
|
|
}
|
|
}
|