Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,7 +64,7 @@ def authenticate(pwd):
|
|
| 64 |
return True
|
| 65 |
|
| 66 |
# =====================================================
|
| 67 |
-
# DATA FETCH (FIXED)
|
| 68 |
# =====================================================
|
| 69 |
def fetch_company_data(start_date, end_date):
|
| 70 |
page = 1
|
|
@@ -79,7 +79,8 @@ def fetch_company_data(start_date, end_date):
|
|
| 79 |
"start_date": start_date,
|
| 80 |
"end_date": end_date,
|
| 81 |
"page[number]": page,
|
| 82 |
-
"page[size]": PAGE_SIZE
|
|
|
|
| 83 |
},
|
| 84 |
timeout=30
|
| 85 |
)
|
|
@@ -90,17 +91,43 @@ def fetch_company_data(start_date, end_date):
|
|
| 90 |
if not data:
|
| 91 |
break
|
| 92 |
|
| 93 |
-
#
|
|
|
|
|
|
|
|
|
|
| 94 |
loc_map = {
|
| 95 |
i["id"]: i["attributes"]
|
| 96 |
-
for i in
|
| 97 |
if i["type"] == "locations"
|
| 98 |
}
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
for lead in data:
|
| 101 |
a = lead["attributes"]
|
| 102 |
|
| 103 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
loc_id = lead.get("relationships", {}).get("location", {}).get("data", {}).get("id")
|
| 105 |
loc = loc_map.get(loc_id, {})
|
| 106 |
|
|
@@ -133,11 +160,10 @@ def fetch_company_data(start_date, end_date):
|
|
| 133 |
"twitter_handle": a.get("twitter_handle"),
|
| 134 |
"facebook_url": a.get("facebook_url"),
|
| 135 |
|
| 136 |
-
# ---
|
| 137 |
-
|
| 138 |
-
"
|
| 139 |
-
|
| 140 |
-
# --- FIXED SECTION END ---
|
| 141 |
|
| 142 |
"country": loc.get("country"),
|
| 143 |
"region": loc.get("region"),
|
|
|
|
| 64 |
return True
|
| 65 |
|
| 66 |
# =====================================================
|
| 67 |
+
# DATA FETCH (FIXED FOR VISITS)
|
| 68 |
# =====================================================
|
| 69 |
def fetch_company_data(start_date, end_date):
|
| 70 |
page = 1
|
|
|
|
| 79 |
"start_date": start_date,
|
| 80 |
"end_date": end_date,
|
| 81 |
"page[number]": page,
|
| 82 |
+
"page[size]": PAGE_SIZE,
|
| 83 |
+
"include": "location,visits" # <--- CRITICAL: Request visits data
|
| 84 |
},
|
| 85 |
timeout=30
|
| 86 |
)
|
|
|
|
| 91 |
if not data:
|
| 92 |
break
|
| 93 |
|
| 94 |
+
# --- MAP INCLUDED DATA ---
|
| 95 |
+
included = js.get("included", [])
|
| 96 |
+
|
| 97 |
+
# 1. Map Locations by ID
|
| 98 |
loc_map = {
|
| 99 |
i["id"]: i["attributes"]
|
| 100 |
+
for i in included
|
| 101 |
if i["type"] == "locations"
|
| 102 |
}
|
| 103 |
|
| 104 |
+
# 2. Map Visits by ID (to lookup landing pages)
|
| 105 |
+
visit_map = {
|
| 106 |
+
i["id"]: i["attributes"]
|
| 107 |
+
for i in included
|
| 108 |
+
if i["type"] == "visits"
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
for lead in data:
|
| 112 |
a = lead["attributes"]
|
| 113 |
|
| 114 |
+
# --- LOGIC: GET LATEST VISIT ---
|
| 115 |
+
landing = None
|
| 116 |
+
exit_page = None
|
| 117 |
+
|
| 118 |
+
# The 'relationships' key contains a list of visit IDs for this company
|
| 119 |
+
visit_refs = lead.get("relationships", {}).get("visits", {}).get("data", [])
|
| 120 |
+
|
| 121 |
+
if visit_refs:
|
| 122 |
+
# We take the last visit in the list, assuming it is the most recent
|
| 123 |
+
latest_visit_id = visit_refs[-1]["id"]
|
| 124 |
+
|
| 125 |
+
# Look up the details in our map
|
| 126 |
+
visit_details = visit_map.get(latest_visit_id, {})
|
| 127 |
+
landing = visit_details.get("landing_page_path")
|
| 128 |
+
exit_page = visit_details.get("exit_page_path")
|
| 129 |
+
|
| 130 |
+
# --- LOGIC: GET LOCATION ---
|
| 131 |
loc_id = lead.get("relationships", {}).get("location", {}).get("data", {}).get("id")
|
| 132 |
loc = loc_map.get(loc_id, {})
|
| 133 |
|
|
|
|
| 160 |
"twitter_handle": a.get("twitter_handle"),
|
| 161 |
"facebook_url": a.get("facebook_url"),
|
| 162 |
|
| 163 |
+
# --- NEW FIELDS ---
|
| 164 |
+
"landing_page_path": landing,
|
| 165 |
+
"exit_page_path": exit_page,
|
| 166 |
+
# ------------------
|
|
|
|
| 167 |
|
| 168 |
"country": loc.get("country"),
|
| 169 |
"region": loc.get("region"),
|