From 7079206658b1155e3528ad6461c715dc826ea74a Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Sat, 1 Mar 2025 10:03:40 +0100 Subject: [PATCH] Added migration, schdule and signing in --- .../Components/Hellcase/EpicGamesLogin.php | 47 +++++++ app/Browser/Jobs/EpicGames/EpicGamesJob.php | 133 ++++++++++++++++++ app/Browser/Jobs/Hellcase/HellcaseJob.php | 11 +- .../2025_02_27_180246_add_epic_games_job.php | 44 ++++++ routes/console.php | 4 + 5 files changed, 234 insertions(+), 5 deletions(-) create mode 100644 app/Browser/Components/Hellcase/EpicGamesLogin.php create mode 100644 app/Browser/Jobs/EpicGames/EpicGamesJob.php create mode 100644 database/migrations/2025_02_27_180246_add_epic_games_job.php diff --git a/app/Browser/Components/Hellcase/EpicGamesLogin.php b/app/Browser/Components/Hellcase/EpicGamesLogin.php new file mode 100644 index 0000000..e3a4ecd --- /dev/null +++ b/app/Browser/Components/Hellcase/EpicGamesLogin.php @@ -0,0 +1,47 @@ +assertVisible($this->selector()); + } + + /** + * Get the element shortcuts for the component. + * + * @return array + */ + public function elements(): array + { + return [ + '@email' => 'input#email', + '@password' => 'input#password', + '@signin-button' => 'button[type="submit"]', + ]; + } + + public function fillForm(Browser $browser, $email, $password) { + $browser->type('@email', $email); + sleep(1); + $browser->type('@password', $password); + sleep(1); + $browser->click('@signin-button'); + } +} diff --git a/app/Browser/Jobs/EpicGames/EpicGamesJob.php b/app/Browser/Jobs/EpicGames/EpicGamesJob.php new file mode 100644 index 0000000..a1a810f --- /dev/null +++ b/app/Browser/Jobs/EpicGames/EpicGamesJob.php @@ -0,0 +1,133 @@ +jobRun = new JobRun([ + "job_id" => $this->jobId, + "success" => false, + ]); + $this->jobRun->save(); + + $this->goToEpicGamesWebsite($browser); + $this->removePopups($browser); + sleep(5); + $this->signin($browser); + $this->getFreeGames($browser); + + $this->jobRun->success = true; + $this->jobRun->save(); + + Log::info( self::class . " run ended"); + + return $this->jobRun; + } + + /** + * @inheritDoc + */ + public function runTest(Browser $browser): ?JobRun + { + try { + $this->goToEpicGamesWebsite($browser); + sleep(2); + $this->removePopups($browser); + sleep(2); + $this->signin($browser); + return $this->makeSimpleJobRun( + true, + "Connexion réussie", + "Datboi a réussi à se connecter sur EpicGames" + ); + } catch (Exception $e) { + return $this->makeSimpleJobRun( + true, + "Connexion échouée", + "Datboi n'a pas réussi à se connecter sur EpicGames :\n" . $e->getMessage() + ); + } + } + + private function goToEpicGamesWebsite(Browser $browser) + { + sleep(3); + $browser->visit(self::WEBSITE_URL); + sleep(3); + $this->assertNotDetected($browser); + $browser->waitForText("Store", 30, true); + } + + private function signin(Browser $browser) + { + $browser->visit("https://store.epicgames.com/login?state=%2Fen-US%2F"); + sleep(5); + $this->assertNotDetected($browser); + $browser->waitForText("Sign In", 30, true); + sleep(3); + + $jobInfos = JobInfo::where("job_id", $this->jobId)->get(); + $email = $jobInfos->where("key", "email")->first()->value; + $password = $jobInfos->where("key", "password")->first()->value; + $browser->within(new EpicGamesLogin, function (Browser $browser) use ($email, $password) { + $browser->fillForm($email, $password); + }); + + sleep(40); + } + + private function getFreeGames(Browser $browser) + { + $browser->visit('https://www.epicgames.com/store/en-US/free-games'); + } + + private function removePopups(Browser $browser) + { + // $browser->script('document.querySelector("div.app-modal")[0].remove();'); + // $browser->driver->executeScript('document.querySelector("div.app-modal")[0].remove();'); + } + + private function assertNotDetected(Browser $browser) + { + try { + $browser->waitForText("One more step", 10, true); + } catch (Exception $_) { + return; + } + throw new Exception("Détecté par cloudflare"); + } +} diff --git a/app/Browser/Jobs/Hellcase/HellcaseJob.php b/app/Browser/Jobs/Hellcase/HellcaseJob.php index 7650bbc..8badaa3 100644 --- a/app/Browser/Jobs/Hellcase/HellcaseJob.php +++ b/app/Browser/Jobs/Hellcase/HellcaseJob.php @@ -21,25 +21,26 @@ class HellcaseJob extends BrowserJob implements ShouldBeUniqueUntilProcessing { private const STEAM_LOGIN_THRESHOLD = 5 * 60; // 5 minutes private const APPROXIMATIVE_RUNNING_MINUTES = 2; + private const WEBSITE_URL = "https://hellcase.com"; private JobRun $jobRun; public function __construct() { - Log::info("Constructing HellcaseJob"); + Log::info("Constructing " . self::class); parent::__construct(2); } public function run(Browser $browser): ?JobRun { - Log::info("Running HellcaseJob"); + Log::info("Running " . self::class); $this->jobRun = new JobRun([ "job_id" => $this->jobId, "success" => false, ]); $this->jobRun->save(); - $browser->visit('https://hellcase.com'); + $browser->visit(self::WEBSITE_URL); $browser->waitForText("CASES", 30, true); $this->removePopups($browser); sleep(5); @@ -50,7 +51,7 @@ class HellcaseJob extends BrowserJob implements ShouldBeUniqueUntilProcessing $this->jobRun->success = true; $this->jobRun->save(); - Log::info("HellcaseJob run ended"); + Log::info(self::class . " run ended"); return $this->jobRun; } @@ -61,7 +62,7 @@ class HellcaseJob extends BrowserJob implements ShouldBeUniqueUntilProcessing public function runTest(Browser $browser): ?JobRun { try { - $browser->visit('https://hellcase.com'); + $browser->visit(self::WEBSITE_URL); $browser->waitForText("CASES", 30, true); $this->removePopups($browser); sleep(2); diff --git a/database/migrations/2025_02_27_180246_add_epic_games_job.php b/database/migrations/2025_02_27_180246_add_epic_games_job.php new file mode 100644 index 0000000..43a705b --- /dev/null +++ b/database/migrations/2025_02_27_180246_add_epic_games_job.php @@ -0,0 +1,44 @@ + $jobId, + 'name' => 'Jeu gratuit Epic Games', + 'description' => 'Prends le jeu gratuit Epic games. Tourne tous les jours.', + ]); + \App\Models\JobInfo::forceCreate([ + "key" => "epicgames_account_email", + "name" => "E-mail", + "description" => "L'adresse e-mail utilisée pour votre compte Epic Games.", + "job_info_type_id" => 2, + "job_id" => $jobId, + ], ); + \App\Models\JobInfo::forceCreate([ + "key" => "epicgames_account_password", + "name" => "Mot de passe", + "description" => "Le mot de passe utilisé pour votre compte Epic Games.", + "job_info_type_id" => 3, + "job_id" => $jobId, + ]); + + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + \App\Models\Job::find(3)->delete(); + \App\Models\JobInfo::where('job_id', 3)->delete(); + } +}; diff --git a/routes/console.php b/routes/console.php index 3d242da..34d063d 100644 --- a/routes/console.php +++ b/routes/console.php @@ -1,5 +1,6 @@ monthly()->onOneServer()->withoutOverlapping // Jobs Schedule::job(new HellcaseJob)->daily()->onOneServer()->withoutOverlapping()->name('hellcase')->description('Hellcase job'); // Schedule::job(new HellcaseJob)->everyMinute()->onOneServer()->withoutOverlapping()->name('hellcase')->description('Hellcase job'); + +Schedule::job(new EpicGamesJob())->daily()->onOneServer()->withoutOverlapping()->name('epic-games')->description('Epic Games job'); +