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:
@@ -526,8 +526,15 @@ class InstagramAdapter(LoggingMixin):
|
|||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
if not row or not row[0]:
|
if not row or not row[0]:
|
||||||
return None
|
return None
|
||||||
cookie_list = _json.loads(row[0])
|
cookie_data = _json.loads(row[0])
|
||||||
if not isinstance(cookie_list, list):
|
# 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
|
return None
|
||||||
# Must have sessionid
|
# Must have sessionid
|
||||||
has_session = any(c.get('name') == 'sessionid' and c.get('value')
|
has_session = any(c.get('name') == 'sessionid' and c.get('value')
|
||||||
@@ -603,6 +610,7 @@ class InstagramAdapter(LoggingMixin):
|
|||||||
|
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
nodes = data.get('items', [])
|
nodes = data.get('items', [])
|
||||||
|
self._notify_cookie_ok()
|
||||||
if not nodes:
|
if not nodes:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -688,6 +696,8 @@ class InstagramAdapter(LoggingMixin):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
consecutive_errors = 0
|
consecutive_errors = 0
|
||||||
|
if page == 1:
|
||||||
|
self._notify_cookie_ok()
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
items = data.get('items', [])
|
items = data.get('items', [])
|
||||||
more = data.get('more_available', False)
|
more = data.get('more_available', False)
|
||||||
@@ -743,6 +753,21 @@ class InstagramAdapter(LoggingMixin):
|
|||||||
self.log(f"Authenticated API failed: {e}", 'warning')
|
self.log(f"Authenticated API failed: {e}", 'warning')
|
||||||
return None
|
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):
|
def _notify_cookie_expired(self):
|
||||||
"""Mark instagram_browser cookies as failed and broadcast a health alert."""
|
"""Mark instagram_browser cookies as failed and broadcast a health alert."""
|
||||||
import json as _json
|
import json as _json
|
||||||
|
|||||||
Reference in New Issue
Block a user