From 60dd646989cdb05f49c8e2696094b6851aa607f2 Mon Sep 17 00:00:00 2001 From: Maciej Pienczyn Date: Thu, 12 Mar 2026 12:44:20 +0100 Subject: [PATCH] feat: trigger Facebook Graph API sync during enrichment scan Instead of skipping OAuth-connected Facebook profiles, the enrichment scan now calls sync_facebook_to_social_media() to fetch fresh data via Graph API. Co-Authored-By: Claude Opus 4.6 --- blueprints/admin/routes_social.py | 20 ++++++++++++++++--- .../admin/social_audit_enrichment_review.html | 8 +++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/blueprints/admin/routes_social.py b/blueprints/admin/routes_social.py index ef7967c..0da1e1c 100644 --- a/blueprints/admin/routes_social.py +++ b/blueprints/admin/routes_social.py @@ -893,8 +893,22 @@ def _run_enrichment_background(company_ids): } if profile.source in ('facebook_api',): - profile_result['status'] = 'skipped' - profile_result['reason'] = 'Dane z API (wyższy priorytet)' + # Sync via Graph API instead of scraping + try: + from facebook_graph_service import sync_facebook_to_social_media + sync_result = sync_facebook_to_social_media(db, company_id) + if sync_result.get('success'): + data = sync_result.get('data', {}) + profile_result['status'] = 'synced_api' + profile_result['reason'] = f"Zsynchronizowano przez Graph API ({data.get('followers_count', 0)} obserwujących, engagement: {data.get('engagement_rate', 0):.1f}%)" + company_result['has_changes'] = True + else: + profile_result['status'] = 'error' + profile_result['reason'] = f"Graph API: {sync_result.get('error', 'nieznany błąd')}" + except Exception as e: + logger.warning(f"Facebook API sync failed for {company.name}: {e}") + profile_result['status'] = 'error' + profile_result['reason'] = f"Graph API sync error: {str(e)[:100]}" company_result['profiles'].append(profile_result) continue @@ -1142,7 +1156,7 @@ def admin_social_audit_enrichment_review(): # Summary stats total_profiles_scanned = sum(len(r.get('profiles', [])) for r in results) profiles_with_changes = len(pending) - profiles_skipped = sum(1 for r in results for p in r.get('profiles', []) if p.get('status') == 'skipped') + profiles_skipped = sum(1 for r in results for p in r.get('profiles', []) if p.get('status') in ('skipped', 'synced_api')) profiles_no_data = sum(1 for r in results for p in r.get('profiles', []) if p.get('status') in ('no_data', 'no_changes')) profiles_errors = sum(1 for r in results for p in r.get('profiles', []) if p.get('status') == 'error') companies_with_changes = len(set(c['company_id'] for c in pending)) diff --git a/templates/admin/social_audit_enrichment_review.html b/templates/admin/social_audit_enrichment_review.html index 1fc8ed1..606248c 100644 --- a/templates/admin/social_audit_enrichment_review.html +++ b/templates/admin/social_audit_enrichment_review.html @@ -178,8 +178,8 @@ Z nowymi danymi
- {{ summary.profiles_skipped }} - Pominiętych (API) + {{ summary.profiles_skipped }} + Sync API (OAuth)
{{ summary.profiles_no_data }} @@ -242,7 +242,7 @@