Commit
·
c9ef318
1
Parent(s):
f089137
Working /experimenting
Browse files- Dockerfile +1 -1
- business.py +509 -0
Dockerfile
CHANGED
|
@@ -53,4 +53,4 @@ RUN python -m playwright install chromium
|
|
| 53 |
EXPOSE 7860
|
| 54 |
|
| 55 |
# Run the FastAPI application
|
| 56 |
-
CMD ["python", "-m", "uvicorn", "
|
|
|
|
| 53 |
EXPOSE 7860
|
| 54 |
|
| 55 |
# Run the FastAPI application
|
| 56 |
+
CMD ["python", "-m", "uvicorn", "business:app", "--host", "0.0.0.0", "--port", "7860"]
|
business.py
ADDED
|
@@ -0,0 +1,509 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Query
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List, Optional
|
| 4 |
+
from playwright.async_api import async_playwright
|
| 5 |
+
import json
|
| 6 |
+
import re
|
| 7 |
+
from urllib.parse import urlparse
|
| 8 |
+
|
| 9 |
+
app = FastAPI(
|
| 10 |
+
title="Business Contact Intelligence API",
|
| 11 |
+
description="Professional business contact extraction and lead generation API. Extract phone numbers, emails, addresses, and social profiles from websites and directories.",
|
| 12 |
+
version="1.0.0",
|
| 13 |
+
contact={
|
| 14 |
+
"name": "Business Contact Intelligence API",
|
| 15 |
+
"email": "[email protected]",
|
| 16 |
+
},
|
| 17 |
+
license_info={
|
| 18 |
+
"name": "Commercial License",
|
| 19 |
+
},
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
class BusinessContact(BaseModel):
|
| 23 |
+
business_name: str
|
| 24 |
+
phone: Optional[str] = None
|
| 25 |
+
email: Optional[str] = None
|
| 26 |
+
website: Optional[str] = None
|
| 27 |
+
address: Optional[str] = None
|
| 28 |
+
industry: Optional[str] = None
|
| 29 |
+
social_profiles: Optional[dict] = None
|
| 30 |
+
source_url: str
|
| 31 |
+
confidence_score: Optional[float] = None
|
| 32 |
+
|
| 33 |
+
class ContactExtractionResult(BaseModel):
|
| 34 |
+
business_name: str
|
| 35 |
+
phones: List[str] = []
|
| 36 |
+
emails: List[str] = []
|
| 37 |
+
website: str
|
| 38 |
+
social_profiles: dict = {}
|
| 39 |
+
address: Optional[str] = None
|
| 40 |
+
industry: Optional[str] = None
|
| 41 |
+
|
| 42 |
+
class SearchResponse(BaseModel):
|
| 43 |
+
total_found: int
|
| 44 |
+
results: List[BusinessContact]
|
| 45 |
+
search_query: str
|
| 46 |
+
source: str
|
| 47 |
+
|
| 48 |
+
def validate_url(url: str) -> str:
|
| 49 |
+
"""Validate and normalize URL"""
|
| 50 |
+
if not url:
|
| 51 |
+
raise HTTPException(status_code=400, detail="URL is required")
|
| 52 |
+
|
| 53 |
+
# Add protocol if missing
|
| 54 |
+
if not url.startswith(('http://', 'https://')):
|
| 55 |
+
url = 'https://' + url
|
| 56 |
+
|
| 57 |
+
# Basic URL validation
|
| 58 |
+
try:
|
| 59 |
+
parsed = urlparse(url)
|
| 60 |
+
if not parsed.netloc:
|
| 61 |
+
raise HTTPException(status_code=400, detail="Invalid URL format")
|
| 62 |
+
except Exception:
|
| 63 |
+
raise HTTPException(status_code=400, detail="Invalid URL format")
|
| 64 |
+
|
| 65 |
+
return url
|
| 66 |
+
|
| 67 |
+
def extract_phone_numbers(text: str) -> List[str]:
|
| 68 |
+
"""Extract phone numbers with improved regex patterns"""
|
| 69 |
+
patterns = [
|
| 70 |
+
r'\+\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}', # International
|
| 71 |
+
r'\(\d{3}\)[-.\s]?\d{3}[-.\s]?\d{4}', # US format (123) 456-7890
|
| 72 |
+
r'\d{3}[-.\s]?\d{3}[-.\s]?\d{4}', # US format 123-456-7890
|
| 73 |
+
r'\d{10,15}', # Simple digit sequence
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
phones = []
|
| 77 |
+
for pattern in patterns:
|
| 78 |
+
matches = re.findall(pattern, text)
|
| 79 |
+
phones.extend(matches)
|
| 80 |
+
|
| 81 |
+
# Clean and deduplicate
|
| 82 |
+
cleaned_phones = []
|
| 83 |
+
for phone in phones:
|
| 84 |
+
# Remove non-digits except +
|
| 85 |
+
cleaned = re.sub(r'[^\d+]', '', phone)
|
| 86 |
+
if len(cleaned) >= 10 and cleaned not in cleaned_phones:
|
| 87 |
+
cleaned_phones.append(cleaned)
|
| 88 |
+
|
| 89 |
+
return cleaned_phones[:5] # Limit to 5 most likely numbers
|
| 90 |
+
|
| 91 |
+
def extract_emails(text: str) -> List[str]:
|
| 92 |
+
"""Extract email addresses with improved validation"""
|
| 93 |
+
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
|
| 94 |
+
emails = re.findall(pattern, text)
|
| 95 |
+
|
| 96 |
+
# Filter out common false positives
|
| 97 |
+
filtered_emails = []
|
| 98 |
+
exclude_domains = ['example.com', 'test.com', 'placeholder.com']
|
| 99 |
+
|
| 100 |
+
for email in emails:
|
| 101 |
+
domain = email.split('@')[1].lower()
|
| 102 |
+
if domain not in exclude_domains and email not in filtered_emails:
|
| 103 |
+
filtered_emails.append(email)
|
| 104 |
+
|
| 105 |
+
return filtered_emails[:5] # Limit to 5 most likely emails
|
| 106 |
+
|
| 107 |
+
@app.get("/search",
|
| 108 |
+
response_model=SearchResponse,
|
| 109 |
+
summary="Search Business Directory",
|
| 110 |
+
description="Search for businesses across multiple directories and extract comprehensive contact information. Perfect for lead generation and market research.",
|
| 111 |
+
tags=["Search", "Lead Generation"])
|
| 112 |
+
async def search_businesses(
|
| 113 |
+
query: str = Query(..., description="Business name, industry or location to search for"),
|
| 114 |
+
limit: int = Query(10, ge=1, le=50, description="Maximum number of results (1-50)"),
|
| 115 |
+
source: str = Query("auto", description="Directory source: 'auto', 'yellowpages', 'yelp', 'google'")
|
| 116 |
+
):
|
| 117 |
+
"""
|
| 118 |
+
Search for businesses and extract their contact information from various directories.
|
| 119 |
+
|
| 120 |
+
**Features:**
|
| 121 |
+
- Multi-source directory search
|
| 122 |
+
- Comprehensive contact extraction
|
| 123 |
+
- Social media profile detection
|
| 124 |
+
- Address and industry classification
|
| 125 |
+
- Confidence scoring
|
| 126 |
+
|
| 127 |
+
**Use Cases:**
|
| 128 |
+
- Lead generation for sales teams
|
| 129 |
+
- Market research and competitor analysis
|
| 130 |
+
- Contact database building
|
| 131 |
+
- Business intelligence gathering
|
| 132 |
+
- Prospecting automation
|
| 133 |
+
|
| 134 |
+
**Data Extracted:**
|
| 135 |
+
- Business name and industry
|
| 136 |
+
- Phone numbers (multiple formats)
|
| 137 |
+
- Email addresses
|
| 138 |
+
- Website URLs
|
| 139 |
+
- Physical addresses
|
| 140 |
+
- Social media profiles (LinkedIn, Facebook, Twitter)
|
| 141 |
+
"""
|
| 142 |
+
if not query or len(query.strip()) < 2:
|
| 143 |
+
raise HTTPException(status_code=400, detail="Query must be at least 2 characters")
|
| 144 |
+
|
| 145 |
+
async with async_playwright() as p:
|
| 146 |
+
browser = await p.chromium.launch(headless=True)
|
| 147 |
+
page = await browser.new_page()
|
| 148 |
+
|
| 149 |
+
try:
|
| 150 |
+
businesses = []
|
| 151 |
+
|
| 152 |
+
# For demo purposes, we'll simulate directory search
|
| 153 |
+
# In production, you'd implement actual directory scraping
|
| 154 |
+
if source in ["auto", "yellowpages"]:
|
| 155 |
+
# Simulate Yellow Pages search
|
| 156 |
+
search_url = f"https://www.yellowpages.com/search?search_terms={query.replace(' ', '+')}"
|
| 157 |
+
|
| 158 |
+
try:
|
| 159 |
+
await page.goto(search_url, timeout=30000)
|
| 160 |
+
await page.wait_for_load_state("networkidle", timeout=10000)
|
| 161 |
+
|
| 162 |
+
# Extract business listings with robust error handling
|
| 163 |
+
listings = await page.query_selector_all(".result")
|
| 164 |
+
|
| 165 |
+
for listing in listings[:limit]:
|
| 166 |
+
try:
|
| 167 |
+
# Extract business name
|
| 168 |
+
name_el = await listing.query_selector(".business-name, h3 a, .n")
|
| 169 |
+
name = await name_el.inner_text() if name_el else "Unknown Business"
|
| 170 |
+
name = name.strip()
|
| 171 |
+
|
| 172 |
+
# Extract phone numbers
|
| 173 |
+
content = await listing.inner_html()
|
| 174 |
+
phones = extract_phone_numbers(content)
|
| 175 |
+
primary_phone = phones[0] if phones else None
|
| 176 |
+
|
| 177 |
+
# Extract emails
|
| 178 |
+
emails = extract_emails(content)
|
| 179 |
+
primary_email = emails[0] if emails else None
|
| 180 |
+
|
| 181 |
+
# Extract website
|
| 182 |
+
website_el = await listing.query_selector("a[href*='http']")
|
| 183 |
+
website = None
|
| 184 |
+
if website_el:
|
| 185 |
+
href = await website_el.get_attribute("href")
|
| 186 |
+
if href and not any(x in href for x in ['yellowpages.com', 'maps.google.com']):
|
| 187 |
+
website = href
|
| 188 |
+
|
| 189 |
+
# Extract address
|
| 190 |
+
address_el = await listing.query_selector(".address, .street-address")
|
| 191 |
+
address = await address_el.inner_text() if address_el else None
|
| 192 |
+
|
| 193 |
+
# Extract industry/category
|
| 194 |
+
category_el = await listing.query_selector(".categories, .category")
|
| 195 |
+
industry = await category_el.inner_text() if category_el else None
|
| 196 |
+
|
| 197 |
+
# Calculate confidence score based on available data
|
| 198 |
+
confidence = 0.5 # Base score
|
| 199 |
+
if primary_phone: confidence += 0.2
|
| 200 |
+
if primary_email: confidence += 0.2
|
| 201 |
+
if website: confidence += 0.1
|
| 202 |
+
|
| 203 |
+
businesses.append(BusinessContact(
|
| 204 |
+
business_name=name,
|
| 205 |
+
phone=primary_phone,
|
| 206 |
+
email=primary_email,
|
| 207 |
+
website=website,
|
| 208 |
+
address=address,
|
| 209 |
+
industry=industry,
|
| 210 |
+
social_profiles={},
|
| 211 |
+
source_url=search_url,
|
| 212 |
+
confidence_score=round(confidence, 2)
|
| 213 |
+
))
|
| 214 |
+
|
| 215 |
+
except Exception as e:
|
| 216 |
+
# Skip this listing if extraction fails
|
| 217 |
+
continue
|
| 218 |
+
|
| 219 |
+
except Exception as e:
|
| 220 |
+
# If directory fails, return empty results with error info
|
| 221 |
+
pass
|
| 222 |
+
|
| 223 |
+
return SearchResponse(
|
| 224 |
+
total_found=len(businesses),
|
| 225 |
+
results=businesses,
|
| 226 |
+
search_query=query,
|
| 227 |
+
source=source
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
except Exception as e:
|
| 231 |
+
raise HTTPException(status_code=500, detail=f"Search failed: {str(e)}")
|
| 232 |
+
finally:
|
| 233 |
+
await browser.close()
|
| 234 |
+
|
| 235 |
+
@app.post("/extract-from-url",
|
| 236 |
+
response_model=ContactExtractionResult,
|
| 237 |
+
summary="Extract Contacts from Website",
|
| 238 |
+
description="Extract comprehensive business contact information from any company website. Analyzes contact pages, about pages, and footer sections for maximum data extraction.",
|
| 239 |
+
tags=["Extraction", "Website Analysis"])
|
| 240 |
+
async def extract_from_url(url: str):
|
| 241 |
+
"""
|
| 242 |
+
Extract business contact information from a specific company website.
|
| 243 |
+
|
| 244 |
+
**Advanced Features:**
|
| 245 |
+
- Multi-page analysis (contact, about, footer)
|
| 246 |
+
- Smart phone number detection (international formats)
|
| 247 |
+
- Email validation and filtering
|
| 248 |
+
- Social media profile extraction
|
| 249 |
+
- Address and location detection
|
| 250 |
+
- Industry classification
|
| 251 |
+
|
| 252 |
+
**Use Cases:**
|
| 253 |
+
- Company research and due diligence
|
| 254 |
+
- Contact enrichment for CRM systems
|
| 255 |
+
- Lead qualification and scoring
|
| 256 |
+
- Competitive intelligence gathering
|
| 257 |
+
- Sales prospecting automation
|
| 258 |
+
|
| 259 |
+
**Data Sources Analyzed:**
|
| 260 |
+
- Contact/About pages
|
| 261 |
+
- Footer sections
|
| 262 |
+
- Header navigation
|
| 263 |
+
- Schema.org structured data
|
| 264 |
+
- Meta tags and page content
|
| 265 |
+
"""
|
| 266 |
+
url = validate_url(url)
|
| 267 |
+
|
| 268 |
+
async with async_playwright() as p:
|
| 269 |
+
browser = await p.chromium.launch(headless=True)
|
| 270 |
+
page = await browser.new_page()
|
| 271 |
+
|
| 272 |
+
try:
|
| 273 |
+
await page.goto(url, wait_until="networkidle", timeout=30000)
|
| 274 |
+
|
| 275 |
+
# Extract company name from multiple sources
|
| 276 |
+
title = await page.title()
|
| 277 |
+
business_name = title
|
| 278 |
+
|
| 279 |
+
# Try to get better business name from structured data
|
| 280 |
+
try:
|
| 281 |
+
schema_script = await page.query_selector("script[type='application/ld+json']")
|
| 282 |
+
if schema_script:
|
| 283 |
+
schema_text = await schema_script.inner_text()
|
| 284 |
+
schema_data = json.loads(schema_text)
|
| 285 |
+
if isinstance(schema_data, dict) and "name" in schema_data:
|
| 286 |
+
business_name = schema_data["name"]
|
| 287 |
+
except:
|
| 288 |
+
pass
|
| 289 |
+
|
| 290 |
+
# Clean business name
|
| 291 |
+
if " - " in business_name:
|
| 292 |
+
business_name = business_name.split(" - ")[0]
|
| 293 |
+
elif " | " in business_name:
|
| 294 |
+
business_name = business_name.split(" | ")[0]
|
| 295 |
+
|
| 296 |
+
# Get page content for analysis
|
| 297 |
+
content = await page.content()
|
| 298 |
+
|
| 299 |
+
# Extract phone numbers with improved patterns
|
| 300 |
+
phones = extract_phone_numbers(content)
|
| 301 |
+
|
| 302 |
+
# Extract emails with validation
|
| 303 |
+
emails = extract_emails(content)
|
| 304 |
+
|
| 305 |
+
# Extract social media profiles
|
| 306 |
+
social_profiles = {}
|
| 307 |
+
social_selectors = [
|
| 308 |
+
"a[href*='linkedin.com']",
|
| 309 |
+
"a[href*='facebook.com']",
|
| 310 |
+
"a[href*='twitter.com']",
|
| 311 |
+
"a[href*='instagram.com']",
|
| 312 |
+
"a[href*='youtube.com']"
|
| 313 |
+
]
|
| 314 |
+
|
| 315 |
+
for selector in social_selectors:
|
| 316 |
+
try:
|
| 317 |
+
links = await page.query_selector_all(selector)
|
| 318 |
+
for link in links:
|
| 319 |
+
href = await link.get_attribute("href")
|
| 320 |
+
if href:
|
| 321 |
+
if "linkedin.com" in href and "linkedin" not in social_profiles:
|
| 322 |
+
social_profiles["linkedin"] = href
|
| 323 |
+
elif "facebook.com" in href and "facebook" not in social_profiles:
|
| 324 |
+
social_profiles["facebook"] = href
|
| 325 |
+
elif "twitter.com" in href and "twitter" not in social_profiles:
|
| 326 |
+
social_profiles["twitter"] = href
|
| 327 |
+
elif "instagram.com" in href and "instagram" not in social_profiles:
|
| 328 |
+
social_profiles["instagram"] = href
|
| 329 |
+
elif "youtube.com" in href and "youtube" not in social_profiles:
|
| 330 |
+
social_profiles["youtube"] = href
|
| 331 |
+
except:
|
| 332 |
+
continue
|
| 333 |
+
|
| 334 |
+
# Try to extract address
|
| 335 |
+
address = None
|
| 336 |
+
address_patterns = [
|
| 337 |
+
r'\d+\s+[A-Za-z\s]+(?:Street|St|Avenue|Ave|Road|Rd|Boulevard|Blvd|Drive|Dr|Lane|Ln|Way|Court|Ct)',
|
| 338 |
+
r'\d+\s+[A-Za-z\s]+,\s*[A-Za-z\s]+,\s*[A-Z]{2}\s+\d{5}'
|
| 339 |
+
]
|
| 340 |
+
|
| 341 |
+
for pattern in address_patterns:
|
| 342 |
+
match = re.search(pattern, content, re.IGNORECASE)
|
| 343 |
+
if match:
|
| 344 |
+
address = match.group(0)
|
| 345 |
+
break
|
| 346 |
+
|
| 347 |
+
# Try to determine industry from page content
|
| 348 |
+
industry = None
|
| 349 |
+
industry_keywords = {
|
| 350 |
+
"technology": ["software", "tech", "IT", "development", "programming"],
|
| 351 |
+
"healthcare": ["medical", "health", "hospital", "clinic", "doctor"],
|
| 352 |
+
"finance": ["bank", "financial", "investment", "insurance", "accounting"],
|
| 353 |
+
"retail": ["store", "shop", "retail", "commerce", "sales"],
|
| 354 |
+
"consulting": ["consulting", "advisory", "strategy", "management"],
|
| 355 |
+
"manufacturing": ["manufacturing", "production", "factory", "industrial"]
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
content_lower = content.lower()
|
| 359 |
+
for industry_name, keywords in industry_keywords.items():
|
| 360 |
+
if any(keyword in content_lower for keyword in keywords):
|
| 361 |
+
industry = industry_name.title()
|
| 362 |
+
break
|
| 363 |
+
|
| 364 |
+
return ContactExtractionResult(
|
| 365 |
+
business_name=business_name.strip(),
|
| 366 |
+
phones=phones,
|
| 367 |
+
emails=emails,
|
| 368 |
+
website=url,
|
| 369 |
+
social_profiles=social_profiles,
|
| 370 |
+
address=address,
|
| 371 |
+
industry=industry
|
| 372 |
+
)
|
| 373 |
+
|
| 374 |
+
except Exception as e:
|
| 375 |
+
raise HTTPException(status_code=500, detail=f"Extraction failed: {str(e)}")
|
| 376 |
+
finally:
|
| 377 |
+
await browser.close()
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
class BulkExtractionRequest(BaseModel):
|
| 381 |
+
urls: List[str]
|
| 382 |
+
extract_social: bool = True
|
| 383 |
+
extract_address: bool = True
|
| 384 |
+
extract_industry: bool = True
|
| 385 |
+
|
| 386 |
+
class BulkExtractionResult(BaseModel):
|
| 387 |
+
url: str
|
| 388 |
+
status: str # "success" or "error"
|
| 389 |
+
error_message: Optional[str] = None
|
| 390 |
+
contact_data: Optional[ContactExtractionResult] = None
|
| 391 |
+
|
| 392 |
+
class BulkExtractionResponse(BaseModel):
|
| 393 |
+
total_urls: int
|
| 394 |
+
successful: int
|
| 395 |
+
failed: int
|
| 396 |
+
results: List[BulkExtractionResult]
|
| 397 |
+
|
| 398 |
+
|
| 399 |
+
@app.post("/bulk-extract",
|
| 400 |
+
response_model=BulkExtractionResponse,
|
| 401 |
+
summary="Bulk Contact Extraction (Premium)",
|
| 402 |
+
description="Extract contact information from multiple websites simultaneously. Perfect for lead generation agencies and sales teams processing large prospect lists.",
|
| 403 |
+
tags=["Bulk", "Premium", "Lead Generation"])
|
| 404 |
+
async def bulk_extract_contacts(request: BulkExtractionRequest):
|
| 405 |
+
"""
|
| 406 |
+
Extract contact information from multiple websites in a single request.
|
| 407 |
+
|
| 408 |
+
**Premium Features:**
|
| 409 |
+
- Process up to 20 URLs simultaneously
|
| 410 |
+
- Configurable extraction options
|
| 411 |
+
- Detailed error handling per URL
|
| 412 |
+
- Optimized for bulk lead generation
|
| 413 |
+
- Progress tracking and analytics
|
| 414 |
+
|
| 415 |
+
**Perfect For:**
|
| 416 |
+
- Lead generation agencies
|
| 417 |
+
- Sales team prospecting
|
| 418 |
+
- Market research projects
|
| 419 |
+
- Contact database building
|
| 420 |
+
- Competitive intelligence
|
| 421 |
+
|
| 422 |
+
**Use Cases:**
|
| 423 |
+
- Process prospect lists from trade shows
|
| 424 |
+
- Enrich existing contact databases
|
| 425 |
+
- Research competitor contact information
|
| 426 |
+
- Build targeted marketing lists
|
| 427 |
+
- Automate sales prospecting workflows
|
| 428 |
+
"""
|
| 429 |
+
if len(request.urls) > 20:
|
| 430 |
+
raise HTTPException(status_code=400, detail="Maximum 20 URLs allowed per request")
|
| 431 |
+
|
| 432 |
+
results = []
|
| 433 |
+
successful = 0
|
| 434 |
+
failed = 0
|
| 435 |
+
|
| 436 |
+
async with async_playwright() as p:
|
| 437 |
+
browser = await p.chromium.launch(headless=True)
|
| 438 |
+
|
| 439 |
+
for url in request.urls:
|
| 440 |
+
page = None
|
| 441 |
+
try:
|
| 442 |
+
validated_url = validate_url(url)
|
| 443 |
+
page = await browser.new_page()
|
| 444 |
+
|
| 445 |
+
# Set shorter timeout for bulk processing
|
| 446 |
+
await page.goto(validated_url, wait_until="networkidle", timeout=20000)
|
| 447 |
+
|
| 448 |
+
# Extract basic contact info (simplified for speed)
|
| 449 |
+
title = await page.title()
|
| 450 |
+
business_name = title.split(" - ")[0] if " - " in title else title
|
| 451 |
+
|
| 452 |
+
content = await page.content()
|
| 453 |
+
phones = extract_phone_numbers(content)
|
| 454 |
+
emails = extract_emails(content)
|
| 455 |
+
|
| 456 |
+
# Optional extractions based on request
|
| 457 |
+
social_profiles = {}
|
| 458 |
+
address = None
|
| 459 |
+
industry = None
|
| 460 |
+
|
| 461 |
+
if request.extract_social:
|
| 462 |
+
try:
|
| 463 |
+
social_links = await page.query_selector_all("a[href*='linkedin.com'], a[href*='facebook.com']")
|
| 464 |
+
for link in social_links[:2]: # Limit for performance
|
| 465 |
+
href = await link.get_attribute("href")
|
| 466 |
+
if "linkedin.com" in href:
|
| 467 |
+
social_profiles["linkedin"] = href
|
| 468 |
+
elif "facebook.com" in href:
|
| 469 |
+
social_profiles["facebook"] = href
|
| 470 |
+
except:
|
| 471 |
+
pass
|
| 472 |
+
|
| 473 |
+
contact_data = ContactExtractionResult(
|
| 474 |
+
business_name=business_name.strip(),
|
| 475 |
+
phones=phones,
|
| 476 |
+
emails=emails,
|
| 477 |
+
website=validated_url,
|
| 478 |
+
social_profiles=social_profiles,
|
| 479 |
+
address=address,
|
| 480 |
+
industry=industry
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
+
results.append(BulkExtractionResult(
|
| 484 |
+
url=url,
|
| 485 |
+
status="success",
|
| 486 |
+
contact_data=contact_data
|
| 487 |
+
))
|
| 488 |
+
successful += 1
|
| 489 |
+
|
| 490 |
+
except Exception as e:
|
| 491 |
+
results.append(BulkExtractionResult(
|
| 492 |
+
url=url,
|
| 493 |
+
status="error",
|
| 494 |
+
error_message=f"Extraction failed: {str(e)}"
|
| 495 |
+
))
|
| 496 |
+
failed += 1
|
| 497 |
+
|
| 498 |
+
finally:
|
| 499 |
+
if page:
|
| 500 |
+
await page.close()
|
| 501 |
+
|
| 502 |
+
await browser.close()
|
| 503 |
+
|
| 504 |
+
return BulkExtractionResponse(
|
| 505 |
+
total_urls=len(request.urls),
|
| 506 |
+
successful=successful,
|
| 507 |
+
failed=failed,
|
| 508 |
+
results=results
|
| 509 |
+
)
|