Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -94,7 +94,7 @@ def authenticate(pwd):
|
|
| 94 |
return True
|
| 95 |
|
| 96 |
# =====================================================
|
| 97 |
-
# DATA FETCH
|
| 98 |
# =====================================================
|
| 99 |
def fetch_company_data(start_date, end_date):
|
| 100 |
page, rows = 1, []
|
|
@@ -111,50 +111,66 @@ def fetch_company_data(start_date, end_date):
|
|
| 111 |
}
|
| 112 |
)
|
| 113 |
r.raise_for_status()
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
if not payload.get("data"):
|
| 117 |
break
|
| 118 |
|
| 119 |
-
|
| 120 |
i["id"]: i["attributes"]
|
| 121 |
-
for i in
|
| 122 |
if i["type"] == "locations"
|
| 123 |
}
|
| 124 |
|
| 125 |
-
for lead in
|
| 126 |
a = lead["attributes"]
|
| 127 |
loc_id = lead.get("relationships", {}).get("location", {}).get("data", {}).get("id")
|
| 128 |
-
loc =
|
| 129 |
|
| 130 |
rows.append({
|
|
|
|
| 131 |
"company_name": a.get("name"),
|
| 132 |
"website_url": a.get("website_url"),
|
|
|
|
| 133 |
"business_id": a.get("business_id"),
|
| 134 |
-
|
| 135 |
"primary_industry": a.get("industry"),
|
| 136 |
-
"all_industries": ", ".join([i
|
|
|
|
|
|
|
| 137 |
"first_visit_date": a.get("first_visit_date"),
|
| 138 |
"last_visit_date": a.get("last_visit_date"),
|
| 139 |
"total_visits": a.get("visits"),
|
| 140 |
"lead_quality_score": a.get("quality"),
|
|
|
|
| 141 |
"employee_count": a.get("employee_count"),
|
|
|
|
|
|
|
| 142 |
"revenue": a.get("revenue"),
|
|
|
|
| 143 |
"assignee": a.get("assignee"),
|
|
|
|
| 144 |
"crm_lead_id": a.get("crm_lead_id"),
|
| 145 |
"crm_organization_id": a.get("crm_organization_id"),
|
| 146 |
"tags": ", ".join(a.get("tags", [])) if a.get("tags") else None,
|
|
|
|
| 147 |
"linkedin_url": a.get("linkedin_url"),
|
| 148 |
"twitter_handle": a.get("twitter_handle"),
|
| 149 |
"facebook_url": a.get("facebook_url"),
|
|
|
|
|
|
|
| 150 |
"country": loc.get("country"),
|
|
|
|
| 151 |
"region": loc.get("region"),
|
| 152 |
"city": loc.get("city"),
|
|
|
|
|
|
|
| 153 |
})
|
| 154 |
|
| 155 |
page += 1
|
| 156 |
|
| 157 |
-
|
|
|
|
|
|
|
| 158 |
|
| 159 |
# =====================================================
|
| 160 |
# KPI CARDS
|
|
|
|
| 94 |
return True
|
| 95 |
|
| 96 |
# =====================================================
|
| 97 |
+
# DATA FETCH (ALL FIELDS, COMPANY LEVEL)
|
| 98 |
# =====================================================
|
| 99 |
def fetch_company_data(start_date, end_date):
|
| 100 |
page, rows = 1, []
|
|
|
|
| 111 |
}
|
| 112 |
)
|
| 113 |
r.raise_for_status()
|
| 114 |
+
js = r.json()
|
| 115 |
+
if not js.get("data"):
|
|
|
|
| 116 |
break
|
| 117 |
|
| 118 |
+
loc_map = {
|
| 119 |
i["id"]: i["attributes"]
|
| 120 |
+
for i in js.get("included", [])
|
| 121 |
if i["type"] == "locations"
|
| 122 |
}
|
| 123 |
|
| 124 |
+
for lead in js["data"]:
|
| 125 |
a = lead["attributes"]
|
| 126 |
loc_id = lead.get("relationships", {}).get("location", {}).get("data", {}).get("id")
|
| 127 |
+
loc = loc_map.get(loc_id, {})
|
| 128 |
|
| 129 |
rows.append({
|
| 130 |
+
"lead_id": lead.get("id"),
|
| 131 |
"company_name": a.get("name"),
|
| 132 |
"website_url": a.get("website_url"),
|
| 133 |
+
"phone": a.get("phone"),
|
| 134 |
"business_id": a.get("business_id"),
|
| 135 |
+
|
| 136 |
"primary_industry": a.get("industry"),
|
| 137 |
+
"all_industries": ", ".join([i.get("name") for i in a.get("industries", [])])
|
| 138 |
+
if a.get("industries") else None,
|
| 139 |
+
|
| 140 |
"first_visit_date": a.get("first_visit_date"),
|
| 141 |
"last_visit_date": a.get("last_visit_date"),
|
| 142 |
"total_visits": a.get("visits"),
|
| 143 |
"lead_quality_score": a.get("quality"),
|
| 144 |
+
|
| 145 |
"employee_count": a.get("employee_count"),
|
| 146 |
+
"employees_min": a.get("employees_range", {}).get("min") if a.get("employees_range") else None,
|
| 147 |
+
"employees_max": a.get("employees_range", {}).get("max") if a.get("employees_range") else None,
|
| 148 |
"revenue": a.get("revenue"),
|
| 149 |
+
|
| 150 |
"assignee": a.get("assignee"),
|
| 151 |
+
"emailed_to": a.get("emailed_to"),
|
| 152 |
"crm_lead_id": a.get("crm_lead_id"),
|
| 153 |
"crm_organization_id": a.get("crm_organization_id"),
|
| 154 |
"tags": ", ".join(a.get("tags", [])) if a.get("tags") else None,
|
| 155 |
+
|
| 156 |
"linkedin_url": a.get("linkedin_url"),
|
| 157 |
"twitter_handle": a.get("twitter_handle"),
|
| 158 |
"facebook_url": a.get("facebook_url"),
|
| 159 |
+
"logo_url": a.get("logo_url"),
|
| 160 |
+
|
| 161 |
"country": loc.get("country"),
|
| 162 |
+
"country_code": loc.get("country_code"),
|
| 163 |
"region": loc.get("region"),
|
| 164 |
"city": loc.get("city"),
|
| 165 |
+
|
| 166 |
+
"leadfeeder_url": a.get("view_in_leadfeeder"),
|
| 167 |
})
|
| 168 |
|
| 169 |
page += 1
|
| 170 |
|
| 171 |
+
df = pd.DataFrame(rows)
|
| 172 |
+
df["last_visit_date"] = pd.to_datetime(df["last_visit_date"], errors="coerce")
|
| 173 |
+
return df
|
| 174 |
|
| 175 |
# =====================================================
|
| 176 |
# KPI CARDS
|