Mr-Help commited on
Commit
6d5e0f9
Β·
verified Β·
1 Parent(s): 28fbac9

Update leads_fetcher.py

Browse files
Files changed (1) hide show
  1. 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()