-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.js
More file actions
191 lines (167 loc) · 5.73 KB
/
Copy pathscraper.js
File metadata and controls
191 lines (167 loc) · 5.73 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
#!/usr/bin/env bun
/**
* Company & Agent Info Scraper using Google Gemini API & LibSQL
* Usage: bun scraper.js [limit] [output.json]
* Requires TURSO_DATABASE_URL, TURSO_AUTH_TOKEN, and GEMINI_API_KEY in environment
*/
import { writeFileSync } from "fs";
import https from "https";
import { createClient } from "@libsql/client";
// Configuration
const LIMIT = process.env.SCRAPER_LIMIT || process.argv[2] || 2;
const OUTPUT_PATH = process.argv[3] || "enriched_entities.json";
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
const TURSO_URL = process.env.TURSO_DATABASE_URL;
const TURSO_TOKEN = process.env.TURSO_AUTH_TOKEN;
if (!GEMINI_API_KEY || !TURSO_URL || !TURSO_TOKEN) {
console.error(
"Missing required environment variables (GEMINI_API_KEY, TURSO_DATABASE_URL, TURSO_AUTH_TOKEN)"
);
process.exit(1);
}
class CompanyInfoScraper {
constructor(apiKey) {
this.apiKey = apiKey;
this.results = [];
}
log(message, level = "INFO") {
const timestamp = new Date().toLocaleTimeString();
console.log(`[${timestamp}] [${level}] ${message}`);
}
async callGemini(prompt, retryCount = 0) {
return new Promise((resolve, reject) => {
const data = JSON.stringify({
contents: [{ parts: [{ text: prompt }] }],
generationConfig: {
temperature: 0.1,
maxOutputTokens: 1024,
},
});
const options = {
hostname: "generativelanguage.googleapis.com",
path: `/v1beta/models/gemini-2.5-flash:generateContent?key=${this.apiKey}`,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": data.length,
},
};
const req = https.request(options, (res) => {
let body = "";
res.on("data", (chunk) => (body += chunk));
res.on("end", async () => {
try {
const response = JSON.parse(body);
if (response.error) {
if (response.error.code === 429 && retryCount < 3) {
const wait = Math.pow(2, retryCount) * 10000;
this.log(
`Rate limited. Retrying in ${wait / 1000}s...`,
"WARNING"
);
await new Promise((r) => setTimeout(r, wait));
return resolve(await this.callGemini(prompt, retryCount + 1));
}
return reject(new Error(response.error.message));
}
if (
response.candidates &&
response.candidates[0]?.content?.parts?.[0]?.text
) {
resolve(response.candidates[0].content.parts[0].text);
} else {
reject(new Error("No response from Gemini"));
}
} catch (error) {
reject(new Error(`Failed to parse response: ${error.message}`));
}
});
});
req.on("error", (error) =>
reject(new Error(`API request failed: ${error.message}`))
);
req.write(data);
req.end();
});
}
extractJSON(text) {
try {
const cleanText = text.replace(/```json|```/g, "").trim();
return JSON.parse(cleanText);
} catch (e) {
return null;
}
}
async scrapeEntity(entity) {
const { id, name, type } = entity;
this.log(`Scraping ${type}: ${name} (ID: ${id})`);
try {
const prompt = `Research the pharmaceutical ${type} named "${name}".
Find their official website, description of their role in medicine, and key specializations.
Return ONLY a JSON object with this format:
{
"id": ${id},
"name": "${name}",
"type": "${type}",
"website": "URL",
"description": "Comprehensive description",
"specialties": "Key therapeutic areas or services",
"headquarters": "City, Country"
}
If information is unavailable, use "Not found". Return ONLY the JSON.`;
const result = await this.callGemini(prompt);
const data = this.extractJSON(result);
if (data) {
this.log(`Successfully scraped ${name}`, "SUCCESS");
return { ...data, status: "success" };
}
return {
id,
name,
type,
status: "failed",
error: "Invalid JSON response",
};
} catch (error) {
this.log(`Error processing ${name}: ${error.message}`, "ERROR");
return { id, name, type, status: "error", error: error.message };
}
}
async run() {
this.log("Connecting to LibSQL...");
const client = createClient({ url: TURSO_URL, authToken: TURSO_TOKEN });
try {
this.log(`Fetching up to ${LIMIT} unique companies and agents...`);
const companies = await client.execute({
sql: "SELECT id, name FROM companies LIMIT ?",
args: [Math.ceil(LIMIT / 2)],
});
const agents = await client.execute({
sql: "SELECT id, name FROM agents LIMIT ?",
args: [Math.floor(LIMIT / 2)],
});
const entities = [
...companies.rows.map((r) => ({ ...r, type: "company" })),
...agents.rows.map((r) => ({ ...r, type: "agent" })),
];
this.log(`Found ${entities.length} entities to process.`);
for (const entity of entities) {
const result = await this.scrapeEntity(entity);
this.results.push(result);
// Small delay to be polite
await new Promise((r) => setTimeout(r, 10000));
}
this.saveResults();
} catch (error) {
this.log(`Critical Error: ${error.message}`, "ERROR");
} finally {
client.close();
}
}
saveResults() {
writeFileSync(OUTPUT_PATH, JSON.stringify(this.results, null, 2));
this.log(`Results saved to ${OUTPUT_PATH}`, "SUCCESS");
}
}
const scraper = new CompanyInfoScraper(GEMINI_API_KEY);
scraper.run().catch(console.error);