-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlead_parser.py
More file actions
235 lines (193 loc) · 8.13 KB
/
Copy pathlead_parser.py
File metadata and controls
235 lines (193 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import asyncio
import typer
import uuid
import os
from datetime import datetime
from dotenv import load_dotenv
from database import init_db, get_db_connection
from adapters.discovery import get_discovery_adapter
from core.dedupe import normalize_domain, is_duplicate
from core.crawler import Crawler
from core.extractor import get_email_confidence
from core.scoring import score_lead
from core.exporter import export_leads
from schema import Lead
load_dotenv()
app = typer.Typer()
@app.command()
def init():
"""Initialize the local SQLite database."""
init_db()
typer.echo("Database initialized.")
@app.command()
def discover(country: str, city: str, category: str, limit: int = 50):
"""Discover leads using search engines/directories."""
typer.echo(f"Discovering {category} in {city}, {country} (limit: {limit})")
adapter = get_discovery_adapter()
results = asyncio.run(adapter.discover(city, category, limit))
conn = get_db_connection()
cursor = conn.cursor()
added = 0
for res in results:
lead = Lead(
id=str(uuid.uuid4()),
business_name=res.business_name,
category=res.category,
country=country,
city=res.city,
website_url=res.website_url,
normalized_domain=normalize_domain(res.website_url),
phone=res.phone,
source_type=res.source_type,
source_url=res.source_url,
created_at=datetime.utcnow().isoformat(),
updated_at=datetime.utcnow().isoformat(),
)
if not is_duplicate(lead, conn):
cursor.execute("""
INSERT INTO leads (id, business_name, category, country, city, website_url,
normalized_domain, phone, source_type, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (lead.id, lead.business_name, lead.category, lead.country, lead.city,
lead.website_url, lead.normalized_domain, lead.phone, lead.source_type,
lead.created_at, lead.updated_at))
added += 1
conn.commit()
conn.close()
typer.echo(f"Added {added} new leads to database.")
@app.command()
def crawl(limit: int = 100):
"""Crawl discovered leads to extract emails and score them."""
conn = get_db_connection()
cursor = conn.cursor()
# Get leads that haven't been crawled yet
cursor.execute("SELECT * FROM leads WHERE status = 'discovered' LIMIT ?", (limit,))
rows = cursor.fetchall()
if not rows:
typer.echo("No leads to crawl.")
return
crawler = Crawler()
async def process_leads():
for row in rows:
row_dict = dict(row)
for k, v in row_dict.items():
if v is None and (k.startswith('has_') or k.endswith('_estimate') or k.endswith('_score')):
row_dict[k] = 0
lead = Lead(**row_dict)
typer.echo(f"Crawling {lead.website_url} ...")
crawl_data = await crawler.crawl_site(lead.website_url)
emails = crawl_data['emails']
if emails:
# Naive: pick first high/medium confidence email
best_email = None
best_conf = 'low'
for email in emails:
conf = get_email_confidence(email, lead.normalized_domain, 'crawl')
if conf == 'high':
best_email = email
best_conf = conf
break
elif conf == 'medium' and best_conf == 'low':
best_email = email
best_conf = conf
if best_email:
lead.email = best_email
lead.email_confidence = best_conf
lead.status = "email_found"
else:
lead.email = emails[0]
lead.email_confidence = get_email_confidence(emails[0], lead.normalized_domain, 'crawl')
lead.status = "email_found"
else:
lead.status = "no_email"
lead.contact_page_url = crawl_data['contact_page_url']
lead.last_checked_at = datetime.utcnow().isoformat()
# Score Lead
lead = score_lead(lead)
# Update DB
cursor.execute("""
UPDATE leads
SET email = ?, email_confidence = ?, contact_page_url = ?, status = ?,
website_quality_score = ?, redesign_opportunity_score = ?, lead_priority = ?,
suggested_offer = ?, last_checked_at = ?, updated_at = ?
WHERE id = ?
""", (lead.email, lead.email_confidence, lead.contact_page_url, lead.status,
lead.website_quality_score, lead.redesign_opportunity_score, lead.lead_priority,
lead.suggested_offer, lead.last_checked_at, lead.last_checked_at, lead.id))
conn.commit()
# Close crawler inside async loop
await crawler.close()
asyncio.run(process_leads())
conn.close()
typer.echo("Crawl completed.")
@app.command()
def export(output: str = "leads.csv", format: str = "csv"):
"""Export scored leads."""
count = export_leads(output, format)
typer.echo(f"Exported {count} leads to {output}")
@app.command()
def stats():
"""Print database stats."""
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT status, count(*) as count FROM leads GROUP BY status")
rows = cursor.fetchall()
typer.echo("Lead Stats:")
for row in rows:
typer.echo(f" {row['status']}: {row['count']}")
conn.close()
@app.command()
def enrich(limit: int = 50):
"""Run AI analysis to generate outreach messages and extract extra details."""
from core.ai_analyzer import generate_outreach_message
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM leads WHERE status = 'email_found' AND outreach_message IS NULL LIMIT ?", (limit,))
rows = cursor.fetchall()
if not rows:
typer.echo("No leads ready for enrichment.")
return
enriched = 0
for row in rows:
row_dict = dict(row)
for k, v in row_dict.items():
if v is None and (k.startswith('has_') or k.endswith('_estimate') or k.endswith('_score')):
row_dict[k] = 0
lead = Lead(**row_dict)
typer.echo(f"Enriching {lead.business_name}...")
message = generate_outreach_message(lead)
if message:
cursor.execute("UPDATE leads SET outreach_message = ? WHERE id = ?", (message, lead.id))
conn.commit()
enriched += 1
conn.close()
typer.echo(f"Enriched {enriched} leads.")
@app.command()
def screenshot(limit: int = 10):
"""Capture screenshots for leads."""
from core.screenshot import capture_screenshot
conn = get_db_connection()
cursor = conn.cursor()
# Ideally we add a has_screenshot column, but we can just screenshot top priority leads for now
cursor.execute("SELECT id, website_url FROM leads WHERE lead_priority IN ('A', 'B') LIMIT ?", (limit,))
rows = cursor.fetchall()
if not rows:
typer.echo("No high priority leads found to screenshot.")
return
os.makedirs("screenshots", exist_ok=True)
async def run_screenshots():
for row in rows:
lead_id, url = row["id"], row["website_url"]
output = f"screenshots/{lead_id}.png"
if not os.path.exists(output):
typer.echo(f"Capturing {url} ...")
await capture_screenshot(url, output)
asyncio.run(run_screenshots())
typer.echo("Screenshots complete.")
@app.command()
def ui():
"""Launch the local Streamlit dashboard."""
typer.echo("Starting local Streamlit dashboard...")
os.system("streamlit run dashboard.py")
if __name__ == "__main__":
app()