diff --git a/app/Browser/Jobs/Instagram/InstagramAbstractJob.php b/app/Browser/Jobs/Instagram/InstagramAbstractJob.php index 1d88cf7..dde54e0 100644 --- a/app/Browser/Jobs/Instagram/InstagramAbstractJob.php +++ b/app/Browser/Jobs/Instagram/InstagramAbstractJob.php @@ -62,7 +62,7 @@ abstract class InstagramAbstractJob extends BrowserJob implements ShouldBeUnique sleep(3); $emailButton = $browser->driver->findElement(WebDriverBy::xpath('//input[contains(@aria-label, "email")]')); $emailButton->click(); - $emailButton->sendKeys($this->jobInfos->get("instagram_repost_account_email")); + $emailButton->sendKeys($this->jobInfos->get("instagram_repost_account_username")); sleep(3); $passwordButton = $browser->driver->findElement(WebDriverBy::xpath('//input[contains(@aria-label, "Password")]')); $passwordButton->click(); diff --git a/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php b/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php index 90ce2ef..1663628 100644 --- a/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php +++ b/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php @@ -211,8 +211,12 @@ class InstagramRepostJob extends InstagramAbstractJob implements ShouldBeUniqueU } } - private function getLatestReelsFromAccount(Browser $browser, string $account): array + private function getLatestReelsFromAccount(Browser $browser, string $account, int $maxReels = null, bool $save = true): array { + if ($maxReels === null) { + $maxReels = config("jobs.instagramRepost.max_reposts_per_account"); + } + $accountReels = []; // Indexed array to store new reels from the account $browser->visit("https://instagram.com/{$account}/reels"); @@ -252,12 +256,26 @@ class InstagramRepostJob extends InstagramAbstractJob implements ShouldBeUniqueU break; } - $reelModel = InstagramRepost::firstOrCreate( - ["reel_id" => $postId, "account_id" => $accountModel->id], - ["reposted" => false, "repost_tries" => 0] - ); + // Get the model for the reel + $query = InstagramRepost::where("reel_id", $postId) + ->where("account_id", $accountModel->id) + ->first(); - if (count($accountReels) < config("jobs.instagramRepost.max_reposts_per_account")) { + if ($query) { + $reelModel = $query; + } else { + $reelModel = new InstagramRepost([ + "reel_id" => $postId, + "account_id" => $accountModel->id, + "reposted" => false, + "repost_tries" => 0 + ]); + if ($save) { + $reelModel = $reelModel->save(); + } + } + + if (count($accountReels) < $maxReels) { $accountReels[] = $reelModel; // Add it to the to be downloaded reels array } } @@ -361,6 +379,10 @@ class InstagramRepostJob extends InstagramAbstractJob implements ShouldBeUniqueU $reel->reposted = true; $reel->save(); + // set the repost ID to the lastest account reel ID + $reel->repost_reel_id = $this->getLatestPostId($browser, $reel); + $reel->save(); + return true; } catch (\Exception $e) { @@ -384,6 +406,25 @@ class InstagramRepostJob extends InstagramAbstractJob implements ShouldBeUniqueU } } + private function getLatestPostId(Browser $browser, InstagramRepost $reel): void + { + // Go to the profile page of the managed account + $newReel = $this->getLatestReelsFromAccount( + $browser, + $this->jobInfos->get('instagram_repost_account_username'), + 1, + false // Don't save the reel, we don't want to repost our own reposts + )[0] ?? null; + + if ($newReel === null) { + Log::error("No reels found for account: " . $this->jobInfos->get('instagram_repost_account_username')); + return; + } + + // Return the reel ID + return $newReel->reel_id; + } + private function getReelCaption(InstagramRepost $reel, IInstagramVideo $videoInfo): string { if (isset($reel->instagram_caption)) { diff --git a/database/migrations/2025_08_05_113401_changed_instagram_login_to_username_only.php b/database/migrations/2025_08_05_113401_changed_instagram_login_to_username_only.php new file mode 100644 index 0000000..9b6ebea --- /dev/null +++ b/database/migrations/2025_08_05_113401_changed_instagram_login_to_username_only.php @@ -0,0 +1,33 @@ +update([ + 'key' => 'instagram_repost_account_username', + 'description' => "Le nom d'utilisateur unique utilisé pour le compte Instagram de repost." + ]); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + JobInfo::where('key', 'instagram_repost_account_username') + ->update([ + 'key' => 'instagram_repost_account_email', + 'description' => "L'adresse e-mail/nom d'utilisateur/N° de téléphone utilisée pour le compte Instagram de repost." + ]); + } +};