wip fix
This commit is contained in:
1 parent
9bcd4432c6
commit
6f92f9e1d5
1209 files changed
+220940
No files matched your search
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits\Sofascore;
|
||||
|
||||
trait CurlSofa
|
||||
{
|
||||
protected function curl(string $url)
|
||||
{
|
||||
$headers = [
|
||||
'User-Agent: '.$this->useragent(),
|
||||
'Accept: */*',
|
||||
'Accept-Language: de,en-US;q=0.7,en;q=0.8',
|
||||
'Accept-Encoding: gzip, deflate, br, zstd',
|
||||
'Referer: https://www.sofascore.com/',
|
||||
'DNT: 1',
|
||||
'Sec-GPC: 1',
|
||||
'Connection: keep-alive',
|
||||
'Sec-Fetch-Dest: empty',
|
||||
'Sec-Fetch-Mode: cors',
|
||||
'Sec-Fetch-Site: same-origin',
|
||||
'Priority: u=4',
|
||||
'Pragma: no-cache',
|
||||
'Cache-Control: no-cache',
|
||||
'TE: trailers',
|
||||
];
|
||||
|
||||
// 10.208.40.145
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
// This line magically handles all decompression out-of-the-box:
|
||||
curl_setopt($ch, CURLOPT_ENCODING, '');
|
||||
$data = curl_exec($ch);
|
||||
|
||||
$data = json_decode($data, true);
|
||||
|
||||
return $data;
|
||||
|
||||
}
|
||||
|
||||
protected function useragent()
|
||||
{
|
||||
$array = [];
|
||||
$array[] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36';
|
||||
$array[] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36';
|
||||
$array[] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15';
|
||||
$array[] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0';
|
||||
$array[] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0';
|
||||
$array[] = 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Mobile Safari/537.36';
|
||||
$array[] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15';
|
||||
$array[] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Safari/605.1.15';
|
||||
$array[] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
|
||||
$array[] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.1 Safari/605.1.15';
|
||||
$array[] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0';
|
||||
$array[] = 'Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36';
|
||||
|
||||
return $array[array_rand($array)];
|
||||
}
|
||||
|
||||
protected function sleep($speed = false)
|
||||
{
|
||||
switch ($speed) {
|
||||
case 1:
|
||||
// 5 -10 sec
|
||||
$sleep = rand(5000000, 10000000);
|
||||
break;
|
||||
case 2:
|
||||
// 10 - 20 sec
|
||||
$sleep = rand(10000000, 20000000);
|
||||
break;
|
||||
case 3:
|
||||
// 30 - 50 sec
|
||||
$sleep = rand(30000000, 50000000);
|
||||
break;
|
||||
|
||||
default:
|
||||
// 1 -1.5 sec
|
||||
$sleep = rand(100000, 150000);
|
||||
break;
|
||||
}
|
||||
|
||||
usleep($sleep);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
<?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 GrappingEvent
|
||||
{
|
||||
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');
|
||||
}
|
||||
|
||||
$url = $this->base_url . 'api/v1/unique-tournament/' . $tournament['sofa_score_tournament_id'] . "/scheduled-events/$day";
|
||||
|
||||
if ($this->debug) {
|
||||
Notice::note($url, 'p');
|
||||
}
|
||||
|
||||
$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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($this->debug) {
|
||||
Notice::note($day . ' - ' . $tournament['tournament']['name'] . " no events", 'y');
|
||||
}
|
||||
}
|
||||
|
||||
SofaScoreTaskDay::updateOrCreate([
|
||||
'grapper_id' => (int) $this->task_id,
|
||||
'day' => $day,
|
||||
], [
|
||||
'done' => 1,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits\Sofascore;
|
||||
|
||||
use App\Mail\SofaScoreDataMail;
|
||||
use App\Utilities\Notice;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
trait GrappingMail
|
||||
{
|
||||
public function send(string $folder)
|
||||
{
|
||||
ini_set('memory_limit', -1);
|
||||
|
||||
if ($this->debug) {
|
||||
Notice::note("Mail Excel: $folder", 'b');
|
||||
}
|
||||
|
||||
$files = $this->get_tree($this->data_dir);
|
||||
|
||||
$sendfiles = $this->filter($files, ['xlsx']);
|
||||
|
||||
$header = ucfirst($folder).' '.Carbon::now()->format('Y-m-d');
|
||||
|
||||
Mail::to('hermann.telser@gmail.com')
|
||||
->cc('ubaldo.perrucci@gmail.com')
|
||||
->send(
|
||||
new SofaScoreDataMail($header, $sendfiles)
|
||||
);
|
||||
if ($this->debug) {
|
||||
Notice::note('✉ Mail Send', 'bg');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits\Sofascore;
|
||||
|
||||
use App\Mail\SimpleMail;
|
||||
use App\Utilities\Notice;
|
||||
use Illuminate\Support\Carbon;
|
||||
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 GrappingToExcel;
|
||||
use GrappingTournaments;
|
||||
|
||||
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);
|
||||
|
||||
$restart = $this->ask_restart();
|
||||
switch ($restart) {
|
||||
case 'restart':
|
||||
$this->restart = 1;
|
||||
break;
|
||||
case 'clear':
|
||||
$this->restart = 1;
|
||||
$this->clear_folder($folder, 'all');
|
||||
break;
|
||||
}
|
||||
|
||||
/**
|
||||
* day 1
|
||||
*/
|
||||
|
||||
// $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);
|
||||
// }
|
||||
|
||||
/**
|
||||
* day 5
|
||||
*/
|
||||
|
||||
// $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);
|
||||
// }
|
||||
|
||||
/**
|
||||
* to excel day by day
|
||||
*/
|
||||
|
||||
$this->import_to_excel($folder);
|
||||
|
||||
/**
|
||||
* mail it
|
||||
*/
|
||||
|
||||
$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();
|
||||
switch ($restart) {
|
||||
case 'restart':
|
||||
$this->restart = 1;
|
||||
break;
|
||||
case 'clear':
|
||||
$this->restart = 1;
|
||||
$this->clear_folder($folder, 'all');
|
||||
break;
|
||||
}
|
||||
|
||||
$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->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');
|
||||
|
||||
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' => '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: [
|
||||
'no' => 'No',
|
||||
'restart' => 'Restart',
|
||||
'clear' => 'Clear folder and restart',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
protected function select_what_delete(): string
|
||||
{
|
||||
return select(
|
||||
label: 'What files?',
|
||||
scroll: 10,
|
||||
options: [
|
||||
'all' => 'All',
|
||||
'data' => 'Data',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits\Sofascore;
|
||||
|
||||
use App\Models\SofaScoreTask;
|
||||
use App\Utilities\Notice;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use Illuminate\Support\Facades\File;
|
||||
|
||||
trait GrappingToExcel
|
||||
{
|
||||
protected int $max = 2000;
|
||||
|
||||
protected string $titel = '';
|
||||
|
||||
public function import_to_excel(string $folder)
|
||||
{
|
||||
ini_set('memory_limit', -1);
|
||||
|
||||
if ($this->debug) {
|
||||
Notice::note("Import $folder to Excel", 'bb');
|
||||
}
|
||||
|
||||
switch ($folder) {
|
||||
case 'daily':
|
||||
$directories = File::directories($this->data_dir);
|
||||
foreach ($directories as $directory) {
|
||||
$day = substr(strrchr($directory, "/"), 1);
|
||||
$this->titel = ucfirst($folder) . '-' . $day;
|
||||
$this->make_file($this->data_dir . '/' . $day);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'range':
|
||||
$task = SofaScoreTask::with('sport')->where('id', 3)->first();
|
||||
$this->titel = ucfirst($folder) . '-' . $this->clean_string($task->name);
|
||||
$this->make_file($this->data_dir);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected function make_file(string $directory)
|
||||
{
|
||||
$files = $this->get_tree($directory);
|
||||
|
||||
// only json max 2000
|
||||
$files = array_chunk($this->filter($files), $this->max);
|
||||
|
||||
foreach ($files as $key => $value) {
|
||||
|
||||
$has = count($value);
|
||||
if (! $key && $has < $this->max) {
|
||||
$count_from = 1;
|
||||
$count_to = $has;
|
||||
} elseif (! $key) {
|
||||
$count_from = 1;
|
||||
$key++;
|
||||
$count_to = $key * $this->max;
|
||||
} elseif ($has == $this->max) {
|
||||
$count_from = $key * $this->max + 1;
|
||||
$key++;
|
||||
$count_to = $key * $this->max;
|
||||
} else {
|
||||
$count_from = $key * $this->max + 1;
|
||||
$count_to = $key * $this->max + $has;
|
||||
}
|
||||
|
||||
$spreadsheet = new Spreadsheet;
|
||||
$spreadsheet->getProperties()->setTitle($this->titel . '-' . $count_from . '-' . $count_to);
|
||||
$activeWorksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$column = 1;
|
||||
foreach ($value as $file) {
|
||||
$data = file_get_contents($file);
|
||||
$data = json_decode($data, true);
|
||||
$json_string = json_encode($data, JSON_PRETTY_PRINT);
|
||||
|
||||
$arr = explode(PHP_EOL, $json_string);
|
||||
$row = 0;
|
||||
foreach ($arr as $string) {
|
||||
$row++;
|
||||
$replacedString = preg_replace('/\\\\u([0-9abcdef]{4})/', '&#x$1;', $string);
|
||||
$utf8string = mb_convert_encoding($replacedString, 'UTF-8', 'HTML-ENTITIES');
|
||||
$activeWorksheet->setCellValue([$column, $row], $utf8string);
|
||||
}
|
||||
$column++;
|
||||
}
|
||||
|
||||
$spreadsheet->setActiveSheetIndex(0);
|
||||
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
||||
$writer->save("$this->data_dir/$this->titel-$count_from-$count_to.xlsx");
|
||||
}
|
||||
}
|
||||
|
||||
protected function filter(array $files, array $types = ['json'])
|
||||
{
|
||||
$filtered = [];
|
||||
foreach ($files as $file) {
|
||||
$path_parts = pathinfo($file);
|
||||
if (isset($path_parts['extension']) && in_array($path_parts['extension'], $types)) {
|
||||
$filtered[] = $file;
|
||||
}
|
||||
}
|
||||
|
||||
return $filtered;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits\Sofascore;
|
||||
|
||||
use App\Models\SofaScoreCategory;
|
||||
use App\Models\SofaScoreDayTournament;
|
||||
use App\Models\SofaScoreTournament;
|
||||
use App\Services\CloudflareSolverService;
|
||||
use App\Utilities\Notice;
|
||||
|
||||
trait GrappingTournaments
|
||||
{
|
||||
/**
|
||||
* Summary of grap_tournaments_for_day
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function grap_tournaments_for_day(string $day)
|
||||
{
|
||||
$sport_id = 1;
|
||||
|
||||
$has = SofaScoreDayTournament::where('day', $day)->first();
|
||||
|
||||
$slug = 'football';
|
||||
|
||||
if (! $this->force && $has) {
|
||||
if ($this->debug) {
|
||||
Notice::note("$day Tournaments allready grapped", 'bb');
|
||||
}
|
||||
} else {
|
||||
|
||||
$page = 1;
|
||||
$tournaments_counter = 0;
|
||||
|
||||
while ($page) {
|
||||
// $data = $this->curl($url);
|
||||
|
||||
$this->sleep();
|
||||
|
||||
$url = $this->base_url . "api/v1/sport/$slug/scheduled-tournaments/$day/page/$page";
|
||||
|
||||
$cloudflareService = new CloudflareSolverService;
|
||||
|
||||
$data = $cloudflareService->getJsonFromUrl($url);
|
||||
|
||||
if ($data['hasNextPage']) {
|
||||
$page++;
|
||||
} else {
|
||||
$page = false;
|
||||
}
|
||||
|
||||
foreach ($data['scheduled'] as $row) {
|
||||
|
||||
if (isset($row['tournament']['uniqueTournament'])) {
|
||||
$tournament = $row['tournament']['uniqueTournament'];
|
||||
} else {
|
||||
$tournament = $row['tournament'];
|
||||
}
|
||||
|
||||
$category = $tournament['category'];
|
||||
|
||||
if ($sport_id == 5) {
|
||||
$country_id = 470;
|
||||
} else {
|
||||
|
||||
/**
|
||||
* CATEGORY
|
||||
*/
|
||||
$find = [
|
||||
'Amateur',
|
||||
'Bosnia & Herzegovina',
|
||||
'Czechia',
|
||||
'DR Congo',
|
||||
'Netherlands',
|
||||
'North & Central America',
|
||||
'Bonaire',
|
||||
'Sao Tome and Principe',
|
||||
'Cape Verde',
|
||||
'Macao',
|
||||
'Saint Kitts and Nevis',
|
||||
'Eswatini',
|
||||
'Timor Leste',
|
||||
'Laos',
|
||||
'Congo Republic',
|
||||
|
||||
];
|
||||
$replace = [
|
||||
'',
|
||||
'Bosnia Herzegovina',
|
||||
'Czech Republic',
|
||||
'Congo DR',
|
||||
'Holland',
|
||||
'North America',
|
||||
'Bonaire, Sint Eustatius and Saba',
|
||||
'Sao Tome e Principe',
|
||||
'Cape Verde Islands',
|
||||
'Macau',
|
||||
'St. Kitts and Nevis',
|
||||
'Swaziland',
|
||||
'East Timor',
|
||||
'Lao Peoples\'s Democratic Republic',
|
||||
'Congo DR',
|
||||
];
|
||||
$country_id = Country::where('name', trim(str_replace($find, $replace, $category['name'])))->value('id');
|
||||
}
|
||||
|
||||
if (! $country_id) {
|
||||
// platzhalter fix later sofa_score_categories => txodds countries
|
||||
// CATEGORY find <-> replace
|
||||
// Log::channel('sofascore')->info($tournament);
|
||||
|
||||
$sofa_score_category_id = 10000000;
|
||||
} else {
|
||||
$call = SofaScoreCategory::updateOrCreate([
|
||||
'id' => (int) $category['id'],
|
||||
'country_id' => (int) $country_id,
|
||||
'slug' => $category['slug'],
|
||||
], [
|
||||
'name' => $category['name'],
|
||||
]);
|
||||
|
||||
$sofa_score_category_id = $call->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* TOURNAMENT
|
||||
*/
|
||||
SofaScoreTournament::updateOrCreate([
|
||||
'sport_id' => (int) $sport_id,
|
||||
'tournament_id' => (int) $tournament['id'],
|
||||
'sofa_score_category_id' => (int) $sofa_score_category_id,
|
||||
], [
|
||||
'name' => $tournament['name'],
|
||||
'slug' => $tournament['slug'],
|
||||
]);
|
||||
$tournaments_counter++;
|
||||
|
||||
// if ($debug) {
|
||||
// Notice::note('tournament: ' . $tournament['name'], 'p');
|
||||
// }
|
||||
|
||||
/**
|
||||
* DAY TOURNAMENT
|
||||
*/
|
||||
SofaScoreDayTournament::updateOrCreate([
|
||||
'tournament_id' => (int) $tournament['id'],
|
||||
'sport_id' => (int) $sport_id,
|
||||
'day' => $day,
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->debug) {
|
||||
Notice::note("Tournaments: $tournaments_counter", 'p');
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->debug) {
|
||||
Notice::note("$day Tournaments loaded", 'bb');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user