-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
214 lines (180 loc) · 5.24 KB
/
run.js
File metadata and controls
214 lines (180 loc) · 5.24 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
require("dotenv").config();
const fs = require("fs");
const { Parser } = require("json2csv");
// const { fetchBusinesses } = require("./fetchBusinesses");
const { getMockBusinesses } = require("./mockBusinesses");
const { scrapeWebsite } = require("./scrapeWebsite");
const { analyzeWithLLM, LLM_FALLBACK } = require("./analyzeWithLLM");
const { generateOutreach } = require("./generateOutreach");
const { scoreBusiness, sortResults } = require("./score");
const { pushToSheets } = require("./pushToSheets");
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
async function main() {
const businesses = getMockBusinesses().map((b, i) => ({
id: `mock-${i}`,
name: b.name,
phone: b.phone,
review_count: b.reviews,
rating: 4.5,
url: b.website,
categories: [{ alias: b.category, title: b.category }],
location: {},
}));
const pipeline = [];
for (let i = 0; i < businesses.length; i += 1) {
const b = businesses[i];
let scrape = {
websiteUrl: null,
scraped: false,
error: null,
hasForm: false,
hasChat: false,
hasBooking: false,
hasPhone: false,
};
try {
scrape = await scrapeWebsite(b.url);
} catch (e) {
scrape.error = e.message || "scrape_exception";
}
// deterministic missing features
const missing_features = [
!scrape.hasForm && "contact form",
!scrape.hasChat && "live chat",
!scrape.hasBooking && "online booking",
].filter(Boolean);
const llmCtx = {
name: b.name,
review_count: b.review_count,
rating: b.rating,
websiteUrl: scrape.websiteUrl,
scraped: scrape.scraped,
scrapeError: scrape.error,
hasForm: scrape.hasForm,
hasChat: scrape.hasChat,
hasBooking: scrape.hasBooking,
hasPhone: scrape.hasPhone,
};
let llm = { missing_features: [], pitch: "" };
try {
llm = await analyzeWithLLM(llmCtx);
} catch {
llm = { ...LLM_FALLBACK };
}
// fallback if LLM fails
if (!llm.missing_features || llm.missing_features.length === 0) {
llm.missing_features = missing_features;
}
const scores = scoreBusiness({
categories: b.categories,
phone: b.phone,
review_count: b.review_count,
hasForm: scrape.hasForm,
hasChat: scrape.hasChat,
hasBooking: scrape.hasBooking,
hasPhone: scrape.hasPhone,
missing_features: llm.missing_features,
scrapeError: scrape.error,
});
// safer outreach (avoid false claims if scrape failed)
const { outreach } = await generateOutreach({
name: b.name,
llm: {
missing_features: scrape.error
? []
: llm.missing_features || [],
},
});
pipeline.push({
business: b,
websiteUrl: scrape.websiteUrl,
scrapeError: scrape.error,
signals: {
hasForm: scrape.hasForm,
hasChat: scrape.hasChat,
hasBooking: scrape.hasBooking,
hasPhone: scrape.hasPhone,
},
llm,
scores,
outreach,
});
await sleep(Number(process.env.REQUEST_PAUSE_MS) || 400);
}
const sorted = sortResults(pipeline);
// only strong leads
const topProspects = sorted
.filter((r) => r.scores.priority > 10)
.slice(0, 3);
// skip noisy Google Sheets error
let sheetsMeta = "not_configured";
try {
await pushToSheets(sorted);
sheetsMeta = "connected";
} catch {
sheetsMeta = "not_configured";
}
const output = {
generated_at: new Date().toISOString(),
count: topProspects.length,
google_sheets: sheetsMeta,
results: topProspects.map((r) => {
let label = "LOW";
if (r.scores.priority >= 20) label = "HOT";
else if (r.scores.priority >= 12) label = "WARM";
const status = r.scrapeError
? "partial"
: r.signals.hasForm ||
r.signals.hasChat ||
r.signals.hasBooking ||
r.signals.hasPhone
? "verified"
: "low_confidence";
return {
name: r.business.name,
yelp_url: r.business.url,
website_url: r.websiteUrl,
phone: r.business.phone,
review_count: r.business.review_count,
rating: r.business.rating,
lead_label: label,
priority_tier:
label === "HOT"
? "High conversion potential"
: label === "WARM"
? "Moderate opportunity"
: "Low priority",
status,
signals: r.signals,
scores: r.scores,
llm: r.llm,
outreach: r.outreach,
};
}),
};
// save JSON
fs.writeFileSync("output.json", JSON.stringify(output, null, 2));
// generate CSV (ranked)
const flatResults = output.results.map((r, index) => ({
rank: index + 1,
name: r.name,
priority: r.scores.priority,
lead_label: r.lead_label,
priority_tier: r.priority_tier,
status: r.status,
missing_features: r.llm.missing_features.join(", "),
outreach: r.outreach,
}));
const parser = new Parser();
const csv = parser.parse(flatResults);
fs.writeFileSync("leads.csv", csv);
console.log("✅ output.json + leads.csv generated");
}
main().catch((err) => {
console.error(
JSON.stringify({ error: err.message || String(err) }, null, 2)
);
process.exitCode = 1;
});