-
Notifications
You must be signed in to change notification settings - Fork 0
/
yelpBusinessLocation.js
95 lines (80 loc) · 2.88 KB
/
yelpBusinessLocation.js
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
const apiKey =
"VOFrlTItWh9XawE_uJ12b0moG-009HKnslg6ceXkkvcRXBi-613Ui6eTqMbuqXBvay4plE4ijOJK2ABa27x1ANnXoaepMEo1OVFD7nrcuYwtSFPdkiHq4EUd6FdkW3Yx";
const yelp = require("yelp-fusion");
let { getYelpInfo } = require("./scrapeBusinessDomain");
let { scrapeEmailFromDomain } = require("./scrapeContactInfo");
const client = yelp.client(apiKey);
let { postDataToAppsScript, writeEmailsToFile } = require("./utils");
const { getEmailsFromToofr } = require("./getEmailsFromToofr");
async function getBusinessData(vertical, location, scriptUrl) {
let offset = 0;
let flag = true;
let bussArray = [];
let locationsArray = [];
while (flag) {
// console.log("TERM", vertical, "LOCATION", location);
await client
.search({
term: vertical,
location,
limit: 50,
offset,
scriptUrl
})
.then(async res => {
let totalRes = res.jsonBody.total;
// console.log("TOTAL RES: ", totalRes);
bussArray = [...bussArray, ...res.jsonBody.businesses];
let filtered = bussArray.map(el => el.url);
for (let i = 0; i < filtered.length; i++) {
const element = filtered[i];
// console.log("ELEMENT: ", element);
let bussinesDomain = (await getYelpInfo(element)) || "";
// let nameBussinesYelp =
// (await getFirstLastBusinessName(element)) || "";
let firstName = bussinesDomain.firstName;
let lastName = bussinesDomain.lastName
? bussinesDomain.lastName.replace(/[.,\s]/g, "")
: bussinesDomain.lastName;
// console.log("BUSSINESS NAME FROM YELP", firstName, lastName);
let emailBussines =
(await scrapeEmailFromDomain(bussinesDomain.website)) || "";
if (emailBussines) {
// console.log("EMail bussines", emailBussines.split(","));
let crawLedEmails = await writeEmailsToFile(
emailBussines.split(",")
);
}
let toofrEmailBussines =
(await getEmailsFromToofr(
firstName,
lastName,
bussinesDomain.website
)) || "";
console.log("BUSSINES DOMAIN: ", bussinesDomain.website);
console.log("EMAILBUSSINESS ", emailBussines);
let locations = [
vertical,
location,
bussinesDomain.website,
emailBussines,
element
];
locationsArray.push(locations);
}
if (totalRes > offset) {
offset += 50;
} else {
flag = false;
}
console.log("YELP LOCATIONS", locationsArray);
await postDataToAppsScript(scriptUrl, locationsArray, "locations");
})
.catch(e => {
console.log(e);
flag = false;
});
}
console.log("Total buss", bussArray.length);
}
module.exports = { getBusinessData };