-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (31 loc) · 950 Bytes
/
index.js
File metadata and controls
36 lines (31 loc) · 950 Bytes
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
import dotenv from "dotenv";
import { getJson } from "serpapi";
import fs, { write } from "fs";
import path from "path"
dotenv.config();
const apiKey = process.env.API_KEY;
const writeToCsv = (data) => {
const csvContent = data.map(row => row.join(",")).join("\n");
const filePath = path.join(process.cwd(), "output.csv");
fs.writeFile(filePath, csvContent, "utf8", (err) => {
if (err) {
console.error("Error writing CSV file:", err);
} else {
console.log("CSV file has been saved to", filePath);
}
});
}
getJson({
engine: "google_maps",
q: "Coffee",
ll: "@40.76173745837114,-73.9774600487914,14z",
api_key: apiKey,
}, (json) => {
const data = [];
data.push(["Name", "Phone", "Rating", "Reviews", "Address"]);
json.local_results.forEach(({ title, phone, rating, reviews, address }) => {
data.push([title, phone, rating, reviews, `"${address}"`])
});
writeToCsv(data);
console.log(json)
});