134 lines
4.0 KiB
PHP
134 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Browser\Jobs\EpicGames;
|
|
|
|
use App\Browser\BrowserJob;
|
|
use App\Browser\Components\Hellcase\EpicGamesLogin;
|
|
use App\Browser\Components\Hellcase\MainNav;
|
|
use App\Browser\Jobs\Hellcase\HellcaseLoginQrCode;
|
|
use App\Models\JobArtifact;
|
|
use App\Models\JobInfo;
|
|
use App\Models\JobRun;
|
|
use App\Notification\NotificationBody\Hellcase\HellcaseNotificationDailyFreeBody;
|
|
use App\Notification\NotificationBody\Hellcase\HellcaseNotificationLoginBody;
|
|
use App\Notification\Notifications\Hellcase\HellcaseNotificationDailyFree;
|
|
use App\Notification\Notifications\Hellcase\HellcaseNotificationLogin;
|
|
use App\Notification\Providers\AllNotification;
|
|
use Exception;
|
|
use Facebook\WebDriver\WebDriverBy;
|
|
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Laravel\Dusk\Browser;
|
|
|
|
class EpicGamesJob extends BrowserJob implements ShouldBeUniqueUntilProcessing
|
|
{
|
|
private const APPROXIMATIVE_RUNNING_MINUTES = 2;
|
|
|
|
private const WEBSITE_URL = "https://www.epicgames.com/store/en-US/";
|
|
|
|
private JobRun $jobRun;
|
|
|
|
public function __construct()
|
|
{
|
|
Log::info("Constructing " . self::class);
|
|
parent::__construct(3);
|
|
}
|
|
|
|
public function run(Browser $browser): ?JobRun
|
|
{
|
|
sleep(40);
|
|
Log::info("Running " . self::class);
|
|
$this->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");
|
|
}
|
|
}
|