From 034a2c875454d76758decd94ae5df421d80b08c3 Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Tue, 3 Jun 2025 20:21:34 +0200 Subject: [PATCH] Added jobArtefacts to InstagramRepostJob --- .../InstagramRepost/InstagramRepostJob.php | 145 ++++++++++-------- 1 file changed, 85 insertions(+), 60 deletions(-) diff --git a/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php b/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php index 1211e06..9757f78 100644 --- a/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php +++ b/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php @@ -7,6 +7,7 @@ use App\Browser\JobDebugScreenshot; use App\Models\InstagramRepost; use App\Models\InstagramAccount; use App\Models\Job; +use App\Models\JobArtifact; use App\Models\JobRun; use App\Notification\Notifications\JobDebugNotification; use App\Notification\Providers\AllNotification; @@ -141,6 +142,11 @@ class InstagramRepostJob extends BrowserJob implements ShouldBeUniqueUntilProces shuffle($toDownloadReels); $toDownloadReels = array_slice($toDownloadReels, 0, self::MAX_REPOSTS_PER_JOB); + $this->jobRun->addArtifact(new JobArtifact([ + "name" => count($toDownloadReels) . " reels sélectionnés pour être repost", + "content" => "" + ])); + // Download the reels $downloadedReels = []; foreach ($toDownloadReels as $repost) { @@ -153,19 +159,30 @@ class InstagramRepostJob extends BrowserJob implements ShouldBeUniqueUntilProces ]; } + $this->jobRun->addArtifact(new JobArtifact([ + "name" => count($downloadedReels) . " reels sélectionnés qui ont été téléchargés", + "content" => "" + ])); + // Now repost all downloaded reels + $repostedReelsCounter = 0; foreach ($downloadedReels as $infos) { $reel = $infos[0]; $videoInfo = $infos[1]; try { // TODO : Avoid getting the reel from the db again, store it in the downloadedReels array - $this->repostReel($browser, InstagramRepost::where('reel_id', $reel->reel_id)->first(), $videoInfo); + $repostedReelsCounter += $this->repostReel($browser, InstagramRepost::where('reel_id', $reel->reel_id)->first(), $videoInfo); } catch (\Exception $e) { Log::error("Failed to repost reel: {$videoInfo->getTitle()} - " . $e->getMessage()); AllNotification::send(new JobDebugNotification($this->jobId, "Failed to repost reel: {$videoInfo->getTitle()} - " . $e->getMessage())); } } + + $this->jobRun->addArtifact(new JobArtifact([ + "name" => $repostedReelsCounter . " reels sélectionnés qui ont été repostés", + "content" => "" + ])); } catch (\Exception $e) { dump($e->getMessage()); } finally { @@ -260,73 +277,81 @@ class InstagramRepostJob extends BrowserJob implements ShouldBeUniqueUntilProces return $videoInfo; } - protected function repostReel(Browser $browser, InstagramRepost $reel, IInstagramVideo $videoInfo) + protected function repostReel(Browser $browser, InstagramRepost $reel, IInstagramVideo $videoInfo): bool { - Log::info("Reposting reel: {$reel->reel_id} - {$videoInfo->getTitle()}"); - - // Increment the repost tries - $reel->repost_tries++; - $reel->save(); - - // TODO Reset if a problem occurs and try again with a limit of 3 attempts - $browser->visit('https://instagram.com'); - sleep(2); - - // Navigate to the reel upload page - $createButton = $browser->driver->findElement(WebDriverBy::xpath('//a[./div//span[contains(text(), "Create")]]')); - $createButton->click(); - sleep(2); - $newPostButton = $browser->driver->findElement(WebDriverBy::xpath('//a[./div//span[contains(text(), "Post")]][@href="#"]')); - $newPostButton->click(); - sleep(3); - - // Upload the video file - $selectFileButton = $browser->driver->findElement(WebDriverBy::xpath('//button[contains(text(), "Select from computer")]')); - $selectFileButton->click(); - sleep(2); - $browser->attach('input[type="file"]._ac69', $this->downloadFolder . $reel->reel_id . ".mp4"); - - sleep(5); // TODO : Wait for the file to be uploaded - - $this->removePopups($browser); - sleep(2); - - // Put original resolution - $this->putOriginalResolution($browser); - - $this->clickNext($browser); - $this->clickNext($browser); // Skip cover photo and trim - - // Add a caption - $captionInput = $browser->driver->findElement(WebDriverBy::xpath('//div[@contenteditable]')); - $captionInput->sendKeys($videoInfo->getDescription()); - - sleep(2); // Wait for the caption to be added - - $this->clickNext($browser); // Share the post - - sleep(5); // Wait for the post to be completed - - // Check if the post was successful try { - $browser->waitForText("Your reel has been shared.", 60, true); - Log::info("Reel reposted successfully: {$reel->reel_id}"); + Log::info("Reposting reel: {$reel->reel_id} - {$videoInfo->getTitle()}"); - // Mark the reel as reposted in the database - $reel->reposted = true; + // Increment the repost tries + $reel->repost_tries++; $reel->save(); - } catch (\Exception $e) { + // TODO Reset if a problem occurs and try again with a limit of 3 attempts + $browser->visit('https://instagram.com'); + sleep(2); + + // Navigate to the reel upload page + $createButton = $browser->driver->findElement(WebDriverBy::xpath('//a[./div//span[contains(text(), "Create")]]')); + $createButton->click(); + sleep(2); + $newPostButton = $browser->driver->findElement(WebDriverBy::xpath('//a[./div//span[contains(text(), "Post")]][@href="#"]')); + $newPostButton->click(); + sleep(3); + + // Upload the video file + $selectFileButton = $browser->driver->findElement(WebDriverBy::xpath('//button[contains(text(), "Select from computer")]')); + $selectFileButton->click(); + sleep(2); + $browser->attach('input[type="file"]._ac69', $this->downloadFolder . $reel->reel_id . ".mp4"); + + sleep(5); // TODO : Wait for the file to be uploaded + + $this->removePopups($browser); + sleep(2); + + // Put original resolution + $this->putOriginalResolution($browser); + + $this->clickNext($browser); + $this->clickNext($browser); // Skip cover photo and trim + + // Add a caption + $captionInput = $browser->driver->findElement(WebDriverBy::xpath('//div[@contenteditable]')); + $captionInput->sendKeys($videoInfo->getDescription()); + + sleep(2); // Wait for the caption to be added + + $this->clickNext($browser); // Share the post + + sleep(5); // Wait for the post to be completed + + // Check if the post was successful try { - $browser->waitForText("Your post was shared", 60, true); - $closeButton = $browser->driver->findElement(WebDriverBy::xpath('//div[./div/*[local-name() = "svg"][@aria-label="Close"]]')); - $closeButton->click(); + $browser->waitForText("Your reel has been shared.", 60, true); + Log::info("Reel reposted successfully: {$reel->reel_id}"); + + // Mark the reel as reposted in the database + $reel->reposted = true; + $reel->save(); + + return true; + } catch (\Exception $e) { - // Do nothing + try { + $browser->waitForText("Your post was shared", 60, true); + $closeButton = $browser->driver->findElement(WebDriverBy::xpath('//div[./div/*[local-name() = "svg"][@aria-label="Close"]]')); + $closeButton->click(); + } catch (\Exception $e) { + // Do nothing + } + Log::error("Failed to repost reel: {$reel->reel_id} - " . $e->getMessage()); + $browser->screenshot(JobDebugScreenshot::getFileName($this->jobId)); + AllNotification::send(new JobDebugNotification($this->jobId, "Failed to repost reel: {$reel->reel_id} - " . $e->getMessage())); + + return false; } - Log::error("Failed to repost reel: {$reel->reel_id} - " . $e->getMessage()); - $browser->screenshot(JobDebugScreenshot::getFileName($this->jobId)); - AllNotification::send(new JobDebugNotification($this->jobId, "Failed to repost reel: {$reel->reel_id} - " . $e->getMessage())); + } catch (\Exception $e) { + return false; } }