Spaces:
Sleeping
Sleeping
Update leads_fetcher.py
Browse files- leads_fetcher.py +12 -5
leads_fetcher.py
CHANGED
|
@@ -3,6 +3,7 @@ import os
|
|
| 3 |
import time
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
from supabase import create_client
|
|
|
|
| 6 |
|
| 7 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 8 |
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY")
|
|
@@ -49,21 +50,21 @@ def fetch_leads_and_poll_status():
|
|
| 49 |
if res.status_code == 200:
|
| 50 |
report_run_id = res.json()['async_stats_reports'][0]['async_stats_report']['report_run_id']
|
| 51 |
print("π€ Report requested. Report ID:", report_run_id)
|
| 52 |
-
|
| 53 |
# Step 2: Poll until report is ready
|
| 54 |
status_url = f"{url}?report_run_id={report_run_id}"
|
| 55 |
max_attempts = 10
|
| 56 |
-
|
| 57 |
for attempt in range(max_attempts):
|
| 58 |
status_res = requests.get(status_url, headers=headers)
|
| 59 |
-
|
| 60 |
if status_res.status_code == 200:
|
| 61 |
try:
|
| 62 |
report = status_res.json()['async_stats_reports'][0]['async_stats_report']
|
| 63 |
status = report.get("async_status")
|
| 64 |
-
|
| 65 |
print(f"π Attempt {attempt + 1}: Status = {status}")
|
| 66 |
-
|
| 67 |
if status == "COMPLETED":
|
| 68 |
csv_url = report["result"]
|
| 69 |
print("β
Report Completed. Download:", csv_url)
|
|
@@ -79,5 +80,11 @@ def fetch_leads_and_poll_status():
|
|
| 79 |
else:
|
| 80 |
print("β Failed to check report status:", status_res.text)
|
| 81 |
break
|
|
|
|
| 82 |
else:
|
| 83 |
print("β Failed to request report:", res.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import time
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
from supabase import create_client
|
| 6 |
+
from token_manager import refresh_snapchat_token # make sure it's at the top
|
| 7 |
|
| 8 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 9 |
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY")
|
|
|
|
| 50 |
if res.status_code == 200:
|
| 51 |
report_run_id = res.json()['async_stats_reports'][0]['async_stats_report']['report_run_id']
|
| 52 |
print("π€ Report requested. Report ID:", report_run_id)
|
| 53 |
+
|
| 54 |
# Step 2: Poll until report is ready
|
| 55 |
status_url = f"{url}?report_run_id={report_run_id}"
|
| 56 |
max_attempts = 10
|
| 57 |
+
|
| 58 |
for attempt in range(max_attempts):
|
| 59 |
status_res = requests.get(status_url, headers=headers)
|
| 60 |
+
|
| 61 |
if status_res.status_code == 200:
|
| 62 |
try:
|
| 63 |
report = status_res.json()['async_stats_reports'][0]['async_stats_report']
|
| 64 |
status = report.get("async_status")
|
| 65 |
+
|
| 66 |
print(f"π Attempt {attempt + 1}: Status = {status}")
|
| 67 |
+
|
| 68 |
if status == "COMPLETED":
|
| 69 |
csv_url = report["result"]
|
| 70 |
print("β
Report Completed. Download:", csv_url)
|
|
|
|
| 80 |
else:
|
| 81 |
print("β Failed to check report status:", status_res.text)
|
| 82 |
break
|
| 83 |
+
|
| 84 |
else:
|
| 85 |
print("β Failed to request report:", res.text)
|
| 86 |
+
|
| 87 |
+
# π Auto-refresh token if the error is due to expiration
|
| 88 |
+
if "unauthorized" in res.text.lower() or "access token" in res.text.lower():
|
| 89 |
+
print("π Access token may be expired. Refreshing token now...")
|
| 90 |
+
refresh_snapchat_token()
|