Spaces:
Sleeping
Sleeping
Complete data_enrichment.py - add missing methods and fix syntax
Browse files- data_enrichment.py +81 -1
data_enrichment.py
CHANGED
|
@@ -338,4 +338,84 @@ class DataEnrichmentManager:
|
|
| 338 |
if contact_data.get('domain'):
|
| 339 |
website_contacts = self.scrape_website_contacts(contact_data['domain'])
|
| 340 |
if website_contacts:
|
| 341 |
-
enriched['enrichments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
if contact_data.get('domain'):
|
| 339 |
website_contacts = self.scrape_website_contacts(contact_data['domain'])
|
| 340 |
if website_contacts:
|
| 341 |
+
enriched['enrichments']['website_contacts'] = website_contacts
|
| 342 |
+
enriched['sources'].append('website_contacts')
|
| 343 |
+
|
| 344 |
+
# Local business data (if company name and location provided)
|
| 345 |
+
if contact_data.get('company') and contact_data.get('location'):
|
| 346 |
+
business_data = self.scrape_local_business(
|
| 347 |
+
query=contact_data['company'],
|
| 348 |
+
location=contact_data['location']
|
| 349 |
+
)
|
| 350 |
+
if business_data:
|
| 351 |
+
enriched['enrichments']['local_business'] = business_data
|
| 352 |
+
enriched['sources'].append('local_business')
|
| 353 |
+
|
| 354 |
+
# Twitter enrichment
|
| 355 |
+
if contact_data.get('twitter_username'):
|
| 356 |
+
twitter_data = self.enrich_twitter_profile(contact_data['twitter_username'])
|
| 357 |
+
if twitter_data:
|
| 358 |
+
enriched['enrichments']['twitter'] = twitter_data
|
| 359 |
+
enriched['sources'].append('twitter')
|
| 360 |
+
|
| 361 |
+
# Facebook enrichment
|
| 362 |
+
if contact_data.get('facebook_id'):
|
| 363 |
+
facebook_data = self.enrich_facebook_profile(contact_data['facebook_id'])
|
| 364 |
+
if facebook_data:
|
| 365 |
+
enriched['enrichments']['facebook'] = facebook_data
|
| 366 |
+
enriched['sources'].append('facebook')
|
| 367 |
+
|
| 368 |
+
# Google search for additional info
|
| 369 |
+
if contact_data.get('name'):
|
| 370 |
+
search_query = f"{contact_data['name']}"
|
| 371 |
+
if contact_data.get('company'):
|
| 372 |
+
search_query += f" {contact_data['company']}"
|
| 373 |
+
google_results = self.search_google(search_query, num_results=5)
|
| 374 |
+
if google_results:
|
| 375 |
+
enriched['enrichments']['google_search'] = google_results
|
| 376 |
+
enriched['sources'].append('google')
|
| 377 |
+
|
| 378 |
+
enriched['enriched_at'] = datetime.now().isoformat()
|
| 379 |
+
enriched['total_sources'] = len(enriched['sources'])
|
| 380 |
+
|
| 381 |
+
return enriched
|
| 382 |
+
|
| 383 |
+
def get_enrichment_stats(self) -> Dict:
|
| 384 |
+
"""Get cache statistics and API usage"""
|
| 385 |
+
stats = {
|
| 386 |
+
'cache_directory': str(self.cache_dir),
|
| 387 |
+
'cached_profiles': 0,
|
| 388 |
+
'cache_types': {},
|
| 389 |
+
'total_api_calls': 0,
|
| 390 |
+
'cache_hits': 0,
|
| 391 |
+
'cache_misses': 0,
|
| 392 |
+
'total_enrichments': 0,
|
| 393 |
+
'successful_enrichments': 0,
|
| 394 |
+
'failed_enrichments': 0
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
if self.cache_dir.exists():
|
| 398 |
+
cache_files = list(self.cache_dir.glob('*.json'))
|
| 399 |
+
stats['cached_profiles'] = len(cache_files)
|
| 400 |
+
|
| 401 |
+
for cache_file in cache_files:
|
| 402 |
+
cache_type = cache_file.stem.split('_')[0]
|
| 403 |
+
stats['cache_types'][cache_type] = stats['cache_types'].get(cache_type, 0) + 1
|
| 404 |
+
|
| 405 |
+
stats['configured_apis'] = [k for k, v in self.api_keys.items() if v]
|
| 406 |
+
stats['total_apis'] = len(stats['configured_apis'])
|
| 407 |
+
|
| 408 |
+
return stats
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
# Convenience functions
|
| 412 |
+
def create_enrichment_manager(username: str) -> DataEnrichmentManager:
|
| 413 |
+
"""Create a new enrichment manager instance"""
|
| 414 |
+
return DataEnrichmentManager(username)
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
def enrich_contact(username: str, contact_data: Dict) -> Dict:
|
| 418 |
+
"""Quick enrichment for a single contact"""
|
| 419 |
+
manager = DataEnrichmentManager(username)
|
| 420 |
+
return manager.enrich_contact_full(contact_data)
|
| 421 |
+
|