[object Object]

← back to Goldleafwallpaper

ig-poster: poll media container status_code until FINISHED before publish (fixes 9007 media-not-ready)

9c92498ce9caafc774e10aaf944d08a45976b982 · 2026-08-10 11:51:46 -0700 · Steve Abrams

Files touched

Diff

commit 9c92498ce9caafc774e10aaf944d08a45976b982
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Mon Aug 10 11:51:46 2026 -0700

    ig-poster: poll media container status_code until FINISHED before publish (fixes 9007 media-not-ready)
---
 scripts/ig-poster/post-next.js | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/scripts/ig-poster/post-next.js b/scripts/ig-poster/post-next.js
index f2a2ef6..6da950c 100644
--- a/scripts/ig-poster/post-next.js
+++ b/scripts/ig-poster/post-next.js
@@ -77,6 +77,18 @@ async function publish(imageUrl, caption) {
   if (!cRes.ok || !cJson.id) throw new Error(`container failed: ${JSON.stringify(cJson)}`);
   const creationId = cJson.id;
 
+  // Step 1.5: poll the container until it finishes processing. Publishing a
+  // container that is still IN_PROGRESS returns code 9007 ("media is not
+  // ready"). Poll status_code up to ~30s before giving up.
+  for (let i = 0; i < 15; i++) {
+    const sUrl = `${GRAPH}/${creationId}?fields=status_code,status&access_token=${TOKEN}`;
+    const sJson = await (await fetch(sUrl)).json();
+    const code = sJson.status_code;
+    if (code === 'FINISHED') break;
+    if (code === 'ERROR' || code === 'EXPIRED') throw new Error(`container ${code}: ${JSON.stringify(sJson.status || sJson)}`);
+    await new Promise((r) => setTimeout(r, 2000));
+  }
+
   // Step 2: publish the container.
   const pubUrl = `${GRAPH}/${IG_USER}/media_publish`;
   const pubBody = new URLSearchParams({ creation_id: creationId, access_token: TOKEN });

← 8e0b68f Add @goldleafwallpaper 4h RML Instagram poster (token-gated  ·  back to Goldleafwallpaper  ·  ig-poster: loud CNCP alert on token/auth failure (no silent 018206f →