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 <noreply@anthropic.com>
This commit is contained in:
Todd
2026-03-30 08:41:42 -04:00
parent 49e72207bf
commit c5781197cc

View File

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