From c5781197cc502d39c912d450d74b41ad14aaa9af Mon Sep 17 00:00:00 2001 From: Todd Date: Mon, 30 Mar 2026 08:41:42 -0400 Subject: [PATCH] Fix Instagram authenticated API cookie loading and status reporting - Handle both cookie formats: flat array and {"cookies": [...]} object (scrapers page saves the latter, code only accepted the former) - Update scraper status to 'ok' after successful authenticated API call (previously only set to 'failed' on 401, never cleared on success) Co-Authored-By: Claude Opus 4.6 --- modules/paid_content/instagram_adapter.py | 29 +++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/modules/paid_content/instagram_adapter.py b/modules/paid_content/instagram_adapter.py index ef92e4c..c748916 100644 --- a/modules/paid_content/instagram_adapter.py +++ b/modules/paid_content/instagram_adapter.py @@ -526,8 +526,15 @@ class InstagramAdapter(LoggingMixin): row = cursor.fetchone() if not row or not row[0]: return None - cookie_list = _json.loads(row[0]) - if not isinstance(cookie_list, list): + cookie_data = _json.loads(row[0]) + # Handle both formats: flat array or {"cookies": [...]} + if isinstance(cookie_data, dict): + cookie_list = cookie_data.get('cookies', []) + elif isinstance(cookie_data, list): + cookie_list = cookie_data + else: + return None + if not cookie_list: return None # Must have sessionid has_session = any(c.get('name') == 'sessionid' and c.get('value') @@ -603,6 +610,7 @@ class InstagramAdapter(LoggingMixin): data = resp.json() nodes = data.get('items', []) + self._notify_cookie_ok() if not nodes: return [] @@ -688,6 +696,8 @@ class InstagramAdapter(LoggingMixin): continue consecutive_errors = 0 + if page == 1: + self._notify_cookie_ok() data = resp.json() items = data.get('items', []) more = data.get('more_available', False) @@ -743,6 +753,21 @@ class InstagramAdapter(LoggingMixin): self.log(f"Authenticated API failed: {e}", 'warning') return None + def _notify_cookie_ok(self): + """Mark instagram_browser cookies as OK after a successful API call.""" + try: + if self.unified_db: + with self.unified_db.get_connection() as conn: + cursor = conn.cursor() + cursor.execute( + "UPDATE scrapers SET last_test_status = 'ok', " + "last_test_message = 'Authenticated API working' " + "WHERE id = 'instagram_browser'" + ) + conn.commit() + except Exception as e: + self.log(f"Failed to update scraper status: {e}", 'debug') + def _notify_cookie_expired(self): """Mark instagram_browser cookies as failed and broadcast a health alert.""" import json as _json