281 lines
8.7 KiB
PHP
281 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Sofascore;
|
|
|
|
use App\Models\Country;
|
|
use App\Models\SofaScoreCategory;
|
|
use App\Models\SofaScoreDayTournament;
|
|
use App\Models\SofaScoreEventGrapped;
|
|
use App\Models\SofaScoreRosa;
|
|
use App\Models\SofaScoreTask;
|
|
use App\Models\SofaScoreTaskDay;
|
|
use App\Models\SofaScoreTaskTournament;
|
|
use App\Models\SofaScoreTournament;
|
|
use App\Models\StatistikoImport;
|
|
use App\Services\CloudflareSolverService;
|
|
use App\Utilities\Notice;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
trait GrappingHelper
|
|
{
|
|
use CurlSofa;
|
|
|
|
/**
|
|
* Summary of load_days
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function load_days(string $grapperSelected)
|
|
{
|
|
$this->days = [];
|
|
|
|
$task = SofaScoreTask::with('sport')->where('id', $this->task_id)->first();
|
|
|
|
switch ($grapperSelected) {
|
|
case 'daily':
|
|
|
|
$this->days[] = Carbon::now()->subDays($this->days_back)->format('Y-m-d');
|
|
|
|
break;
|
|
case 'range':
|
|
$task = SofaScoreTask::with('sport')->where('id', $this->task_id)->first();
|
|
|
|
$from = $this->validate_date($task->day_from);
|
|
$from = Carbon::createFromDate($from);
|
|
|
|
$to = $this->validate_date($task->day_to);
|
|
$to = Carbon::createFromDate($to);
|
|
|
|
$lengthdates = $from->diffInDays($to);
|
|
if ($lengthdates < 1) {
|
|
Notice::note('start must be before end', 'r');
|
|
}
|
|
|
|
if ($this->restart) {
|
|
while ($from <= $to) {
|
|
$this->days[] = $from->format('Y-m-d');
|
|
$from->addDay();
|
|
}
|
|
} else {
|
|
while ($from <= $to) {
|
|
$res = SofaScoreTaskDay::firstOrNew(['grapper_id' => (int) 3, 'day' => $from->format('Y-m-d')]);
|
|
if (! $res->done) {
|
|
$this->days[] = $from->format('Y-m-d');
|
|
}
|
|
$from->addDay();
|
|
}
|
|
}
|
|
shuffle($this->days);
|
|
|
|
break;
|
|
}
|
|
|
|
// if ($this->restart) {
|
|
// SofaScoreEventGrapped::where('grapper_id', $this->task_id)->delete();
|
|
|
|
// foreach ($this->days as $day) {
|
|
// // if restarting find position
|
|
// SofaScoreTaskDay::where('grapper_id', 1)
|
|
// ->where('day', $day)
|
|
// ->update(['done' => 0]);
|
|
// }
|
|
// if ($this->debug) {
|
|
// Notice::note('Grapper restarted', 'r', 1);
|
|
// }
|
|
// }
|
|
|
|
$this->days = array_unique($this->days, SORT_REGULAR);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
protected function set_base_dir(string $grapperSelected)
|
|
{
|
|
switch ($grapperSelected) {
|
|
case 'daily':
|
|
$this->data_dir = storage_path($this->base_dir) . '/daily';
|
|
break;
|
|
case 'range':
|
|
$this->data_dir = storage_path($this->base_dir) . '/range';
|
|
break;
|
|
case 'rose':
|
|
$this->data_dir = storage_path($this->base_dir) . '/rose';
|
|
break;
|
|
case 'player':
|
|
$this->data_dir = storage_path($this->base_dir) . '/player';
|
|
break;
|
|
case 'excel':
|
|
case 'mail':
|
|
case 'clear':
|
|
$this->data_dir = storage_path($this->base_dir);
|
|
break;
|
|
}
|
|
|
|
File::makeDirectory($this->data_dir, 0775, true, true);
|
|
}
|
|
|
|
/**
|
|
* Summary of load_tournaments_requested
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function load_tournaments_requested(string $grapperSelected)
|
|
{
|
|
switch ($grapperSelected) {
|
|
case 'daily':
|
|
$this->tournaments_requested = SofaScoreTaskTournament::with('tournament')->where('sofa_score_task_id', 1)->where('disabled', false)->get()->toArray();
|
|
break;
|
|
case 'range':
|
|
$this->tournaments_requested = SofaScoreTaskTournament::with('tournament')->where('sofa_score_task_id', 3)->where('disabled', false)->get()->toArray();
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Summary of selectGrapper
|
|
*/
|
|
public function settings_to_local(): void
|
|
{
|
|
if ($this->debug) {
|
|
Notice::note('⚽ settings to local');
|
|
Notice::note('⚽ close all vpn');
|
|
}
|
|
exec('killall ssh');
|
|
|
|
if ($this->debug) {
|
|
Notice::note('⚽ open vpn');
|
|
}
|
|
exec('ssh -f -N -L 13306:127.0.0.1:3306 -p 58371 root@162.55.242.190');
|
|
/**
|
|
* SofaScoreRosa
|
|
*/
|
|
if ($this->debug) {
|
|
Notice::note('SofaScoreRosa', 'g');
|
|
}
|
|
SofaScoreRosa::truncate();
|
|
$rows = DB::connection('remote_mariadb')->table('sofa_score_rosas')->get();
|
|
foreach ($rows as $row) {
|
|
SofaScoreRosa::insert((array) $row);
|
|
}
|
|
/**
|
|
* SofaScoreTask
|
|
*/
|
|
if ($this->debug) {
|
|
Notice::note('SofaScoreTask', 'g');
|
|
}
|
|
SofaScoreTask::truncate();
|
|
$rows = DB::connection('remote_mariadb')->table('sofa_score_tasks')->get();
|
|
foreach ($rows as $row) {
|
|
SofaScoreTask::insert((array) $row);
|
|
}
|
|
/**
|
|
* SofaScoreTaskTournament
|
|
*/
|
|
if ($this->debug) {
|
|
Notice::note('SofaScoreTaskTournament', 'g');
|
|
}
|
|
SofaScoreTaskTournament::truncate();
|
|
$rows = DB::connection('remote_mariadb')->table('sofa_score_task_tournaments')->get();
|
|
foreach ($rows as $row) {
|
|
SofaScoreTaskTournament::insert((array) $row);
|
|
}
|
|
/**
|
|
* SofaScoreTournament
|
|
*/
|
|
if ($this->debug) {
|
|
Notice::note('SofaScoreTournament', 'g');
|
|
}
|
|
SofaScoreTournament::truncate();
|
|
$rows = DB::connection('remote_mariadb')->table('sofa_score_tournaments')->get();
|
|
foreach ($rows as $row) {
|
|
SofaScoreTournament::insert((array) $row);
|
|
}
|
|
|
|
if ($this->debug) {
|
|
Notice::note('⚽ close all vpn');
|
|
}
|
|
exec('killall ssh');
|
|
}
|
|
|
|
/**
|
|
* Summary of validate_date
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function validate_date(string $date)
|
|
{
|
|
if (Carbon::createFromDate($date) !== false) {
|
|
return $date;
|
|
} else {
|
|
Notice::note('Wrong date format', 'r', 1);
|
|
}
|
|
}
|
|
|
|
protected function get_tree(string $dir, &$results = [])
|
|
{
|
|
$files = scandir($dir);
|
|
foreach ($files as $value) {
|
|
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
|
|
if (! is_dir($path)) {
|
|
$results[] = $path;
|
|
} elseif ($value != '.' && $value != '..') {
|
|
self::get_tree($path, $results);
|
|
$results[] = $path;
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
protected function clean_string(string $string)
|
|
{
|
|
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
|
|
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
|
|
$string = strtolower($string);
|
|
|
|
return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
|
|
}
|
|
|
|
protected function clear_folder(string $folder, $what = '')
|
|
{
|
|
$mainFolder = storage_path($this->base_dir . '/' . $folder);
|
|
|
|
if (! $what) {
|
|
$what = $this->select_what_delete();
|
|
}
|
|
switch ($what) {
|
|
case 'all':
|
|
if (File::exists($mainFolder)) {
|
|
File::deleteDirectory($mainFolder);
|
|
File::makeDirectory($mainFolder, 0755, true);
|
|
Notice::note("folder $folder empty", 'bb');
|
|
}
|
|
break;
|
|
|
|
case 'data':
|
|
if (File::exists($mainFolder)) {
|
|
$subFolders = File::directories($mainFolder);
|
|
foreach ($subFolders as $subfolder) {
|
|
File::deleteDirectory($subfolder);
|
|
}
|
|
Notice::note("data folders from $folder removed", 'bb');
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
protected function delete_imported_files_orphants(): void
|
|
{
|
|
$tasks = StatistikoImport::pluck('file_name')->toArray();
|
|
|
|
collect(Storage::disk('public')->allFiles('statistiko'))
|
|
->reject(fn(string $file) => $file === '.gitignore')
|
|
->reject(fn(string $file) => in_array($file, $tasks))
|
|
->each(fn($file) => Storage::disk('public')->delete($file));
|
|
}
|
|
}
|