JayBene1 commited on
Commit
b104d98
·
verified ·
1 Parent(s): 14348e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -25
app.py CHANGED
@@ -370,8 +370,42 @@ def parse_csv_file(file_obj):
370
  debug_info.append(f"Error parsing CSV: {e}")
371
  return [], debug_info
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  def search_csv_websites(csv_file, max_results=10):
374
- """Search for contacts from websites listed in CSV file"""
375
  if csv_file is None:
376
  return "Please upload a CSV file", ""
377
 
@@ -391,45 +425,78 @@ def search_csv_websites(csv_file, max_results=10):
391
  error_msg += "4. Verify the CSV file is not corrupted\n"
392
  return error_msg, ""
393
 
 
 
 
 
 
 
 
 
394
  all_contacts = []
395
  processed_websites = []
 
396
 
397
- # Search each website
 
 
 
 
 
 
 
 
398
  for website in websites[:20]: # Limit to first 20 websites
399
  print(f"Processing website: {website}")
400
- contacts = simulate_website_scraping(website)
401
- if contacts:
402
- all_contacts.extend(contacts)
 
 
403
  processed_websites.append(website)
404
- print(f"Found {len(contacts)} contacts for {website}")
405
  else:
406
  print(f"No contacts found for {website}")
407
 
408
- # Remove duplicates based on email
409
- unique_contacts = []
410
- seen_emails = set()
411
- for contact in all_contacts:
412
- if contact['email'] not in seen_emails:
413
- unique_contacts.append(contact)
414
- seen_emails.add(contact['email'])
415
-
416
- # Limit results
417
- unique_contacts = unique_contacts[:max_results]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
418
 
419
- if not unique_contacts:
420
  result_msg = f"No contacts found for the {len(websites)} websites from the CSV file.\n\n"
421
  result_msg += "DEBUG INFORMATION:\n" + debug_text + "\n\n"
422
  result_msg += f"Websites processed: {', '.join(websites[:10])}\n"
423
  result_msg += "This might be because the websites are not in our sample database."
424
  return result_msg, ""
425
 
426
- # Format results
427
  results_text = f"CONTACT DISCOVERY REPORT\n"
428
  results_text += f"CSV Processing Details:\n"
429
  results_text += f"Total Websites in CSV: {len(websites)}\n"
430
  results_text += f"Websites Processed: {len(processed_websites)}\n"
431
  results_text += f"Websites with Contacts: {len(processed_websites)}\n"
432
- results_text += f"Unique Contacts Found: {len(unique_contacts)}\n"
433
  results_text += f"Processed Websites: {', '.join(processed_websites)}\n"
434
  results_text += f"{'='*60}\n\n"
435
 
@@ -437,7 +504,17 @@ def search_csv_websites(csv_file, max_results=10):
437
  results_text += "DEBUG INFORMATION:\n" + debug_text + "\n\n"
438
  results_text += f"{'='*60}\n\n"
439
 
440
- for i, contact in enumerate(unique_contacts, 1):
 
 
 
 
 
 
 
 
 
 
441
  results_text += f"CONTACT #{i}\n"
442
  results_text += f"Name: {contact['first_name']} {contact['last_name']}\n"
443
  results_text += f"Position: {contact['job_title']}\n"
@@ -446,12 +523,17 @@ def search_csv_websites(csv_file, max_results=10):
446
  results_text += f"Company: {contact['company']}\n"
447
  results_text += f"Website: {contact['website']}\n\n"
448
 
449
- # Create CSV output
450
- csv_output = "First Name,Last Name,Job Title,Email,Phone,Company,Website\n"
451
- for contact in unique_contacts:
452
- csv_output += f"{contact['first_name']},{contact['last_name']},{contact['job_title']},{contact['email']},{contact['phone']},{contact['company']},{contact['website']}\n"
 
 
 
 
 
453
 
454
- return results_text, csv_output
455
 
456
  except Exception as e:
457
  return f"Error processing CSV file: {str(e)}", ""
 
370
  debug_info.append(f"Error parsing CSV: {e}")
371
  return [], debug_info
372
 
373
+ def rank_contact_by_title(job_title):
374
+ """Rank contacts by job title priority (lower number = higher priority)"""
375
+ title_lower = job_title.lower()
376
+
377
+ # Define ranking hierarchy
378
+ if 'president' in title_lower:
379
+ return 1
380
+ elif 'ceo' in title_lower or 'chief executive' in title_lower:
381
+ return 2
382
+ elif 'cfo' in title_lower or 'chief financial' in title_lower:
383
+ return 3
384
+ elif 'coo' in title_lower or 'chief operating' in title_lower:
385
+ return 4
386
+ elif 'vice president' in title_lower or 'vp' in title_lower or 'v.p.' in title_lower:
387
+ return 5
388
+ elif 'controller' in title_lower:
389
+ return 6
390
+ elif 'general manager' in title_lower or 'gm' in title_lower:
391
+ return 7
392
+ else:
393
+ return 100 # All other positions get lower priority
394
+
395
+ def get_best_contact_for_website(website_url):
396
+ """Get the highest-ranked contact for a specific website"""
397
+ contacts = simulate_website_scraping(website_url)
398
+ if not contacts:
399
+ return None
400
+
401
+ # Sort contacts by job title ranking
402
+ contacts_with_rank = [(contact, rank_contact_by_title(contact['job_title'])) for contact in contacts]
403
+ contacts_with_rank.sort(key=lambda x: x[1]) # Sort by rank (lower number = higher priority)
404
+
405
+ return contacts_with_rank[0][0] # Return the highest-ranked contact
406
+
407
  def search_csv_websites(csv_file, max_results=10):
408
+ """Search for contacts from websites listed in CSV file and populate the CSV"""
409
  if csv_file is None:
410
  return "Please upload a CSV file", ""
411
 
 
425
  error_msg += "4. Verify the CSV file is not corrupted\n"
426
  return error_msg, ""
427
 
428
+ # Read the original CSV file to preserve all data
429
+ with open(csv_file.name, 'r', encoding='utf-8') as f:
430
+ content = f.read()
431
+
432
+ csv_reader = csv.reader(io.StringIO(content))
433
+ original_rows = list(csv_reader)
434
+
435
+ # Process websites and find contacts
436
  all_contacts = []
437
  processed_websites = []
438
+ contacts_by_website = {}
439
 
440
+ # Create a mapping of websites to their row indices
441
+ website_to_row = {}
442
+ for row_idx, row in enumerate(original_rows):
443
+ if len(row) > 7: # Column H exists
444
+ website_url = row[7].strip()
445
+ if website_url and is_valid_url(website_url):
446
+ website_to_row[website_url] = row_idx
447
+
448
+ # Search each website and get the best contact
449
  for website in websites[:20]: # Limit to first 20 websites
450
  print(f"Processing website: {website}")
451
+ best_contact = get_best_contact_for_website(website)
452
+
453
+ if best_contact:
454
+ contacts_by_website[website] = best_contact
455
+ all_contacts.append(best_contact)
456
  processed_websites.append(website)
457
+ print(f"Found best contact for {website}: {best_contact['first_name']} {best_contact['last_name']} - {best_contact['job_title']}")
458
  else:
459
  print(f"No contacts found for {website}")
460
 
461
+ # Create updated CSV with contact information
462
+ updated_rows = []
463
+ for row_idx, row in enumerate(original_rows):
464
+ # Make a copy of the row and ensure it has enough columns
465
+ new_row = row[:]
466
+
467
+ # Extend row to have at least 13 columns (A-M)
468
+ while len(new_row) < 13:
469
+ new_row.append("")
470
+
471
+ # Check if this row has a website we found contacts for
472
+ if len(row) > 7:
473
+ website_url = row[7].strip()
474
+ if website_url in contacts_by_website:
475
+ contact = contacts_by_website[website_url]
476
+
477
+ # Populate contact information in specified columns
478
+ new_row[8] = contact['first_name'] # Column I (index 8)
479
+ new_row[9] = contact['last_name'] # Column J (index 9)
480
+ new_row[10] = contact['job_title'] # Column K (index 10)
481
+ new_row[11] = contact['phone'] # Column L (index 11)
482
+ new_row[12] = contact['email'] # Column M (index 12)
483
+
484
+ updated_rows.append(new_row)
485
 
486
+ if not all_contacts:
487
  result_msg = f"No contacts found for the {len(websites)} websites from the CSV file.\n\n"
488
  result_msg += "DEBUG INFORMATION:\n" + debug_text + "\n\n"
489
  result_msg += f"Websites processed: {', '.join(websites[:10])}\n"
490
  result_msg += "This might be because the websites are not in our sample database."
491
  return result_msg, ""
492
 
493
+ # Format results text
494
  results_text = f"CONTACT DISCOVERY REPORT\n"
495
  results_text += f"CSV Processing Details:\n"
496
  results_text += f"Total Websites in CSV: {len(websites)}\n"
497
  results_text += f"Websites Processed: {len(processed_websites)}\n"
498
  results_text += f"Websites with Contacts: {len(processed_websites)}\n"
499
+ results_text += f"Contacts Found: {len(all_contacts)}\n"
500
  results_text += f"Processed Websites: {', '.join(processed_websites)}\n"
501
  results_text += f"{'='*60}\n\n"
502
 
 
504
  results_text += "DEBUG INFORMATION:\n" + debug_text + "\n\n"
505
  results_text += f"{'='*60}\n\n"
506
 
507
+ # Show contact rankings
508
+ results_text += "CONTACT RANKINGS (by job title priority):\n"
509
+ for i, contact in enumerate(all_contacts, 1):
510
+ rank = rank_contact_by_title(contact['job_title'])
511
+ results_text += f"{i}. {contact['first_name']} {contact['last_name']} - {contact['job_title']} "
512
+ results_text += f"(Priority Rank: {rank}) - {contact['company']}\n"
513
+
514
+ results_text += f"\n{'='*60}\n\n"
515
+
516
+ # Show detailed contact information
517
+ for i, contact in enumerate(all_contacts, 1):
518
  results_text += f"CONTACT #{i}\n"
519
  results_text += f"Name: {contact['first_name']} {contact['last_name']}\n"
520
  results_text += f"Position: {contact['job_title']}\n"
 
523
  results_text += f"Company: {contact['company']}\n"
524
  results_text += f"Website: {contact['website']}\n\n"
525
 
526
+ # Create CSV output with updated data
527
+ csv_output = io.StringIO()
528
+ csv_writer = csv.writer(csv_output)
529
+
530
+ for row in updated_rows:
531
+ csv_writer.writerow(row)
532
+
533
+ csv_content = csv_output.getvalue()
534
+ csv_output.close()
535
 
536
+ return results_text, csv_content
537
 
538
  except Exception as e:
539
  return f"Error processing CSV file: {str(e)}", ""