Save reposted reel id for future use
Some checks failed
Push image to registry / build-image (push) Failing after 52s

- Changed the managed account login from phone number, username or email to only username so it can be used for other things
This commit is contained in:
2025-08-05 13:58:10 +02:00
parent aa936a2a11
commit e3713773b7
3 changed files with 81 additions and 7 deletions

View File

@ -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();

View File

@ -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)) {

View File

@ -0,0 +1,33 @@
<?php
use App\Models\JobInfo;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
JobInfo::where('key', 'instagram_repost_account_email')
->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."
]);
}
};