Skip to content

Commit 3fae885

Browse files
committed
scaling to 5000 contacts
1 parent 22dd7c4 commit 3fae885

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,5 @@ typings/
8888
.dynamodb/
8989

9090
# Custom
91-
data/
91+
data/
92+
5000contacts.csv

generate-contacts-import.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const fs = require("fs");
2+
require("dotenv").config();
3+
const startingNumber = process.env.STARTINGNUMBER;
4+
const fileName = "5000contacts.csv";
5+
6+
const csvHeader =
7+
"Name,Given Name,Additional Name,Family Name,Yomi Name,Given Name Yomi,Additional Name Yomi,Family Name Yomi,Name Prefix,Name Suffix,Initials,Nickname,Short Name,Maiden Name,Birthday,Gender,Location,Billing Information,Directory Server,Mileage,Occupation,Hobby,Sensitivity,Priority,Subject,Notes,Language,Photo,Group Membership,Phone 1 - Type,Phone 1 - Value";
8+
9+
fs.appendFileSync(fileName, csvHeader + "\n");
10+
11+
let numbers = startingNumber.split(" ");
12+
13+
let counter2 = Number(numbers[2]);
14+
let counter3 = Number(numbers[3]);
15+
let counter4 = Number(numbers[4]);
16+
for (let index = 0; index < 5000; index++) {
17+
if (counter4 == 99) {
18+
counter4 = 0;
19+
counter3++;
20+
}
21+
if (counter3 == 99) {
22+
counter3 = 0;
23+
counter2++;
24+
}
25+
let number = `${numbers[0]} ${numbers[1]} ${twoDigit(counter2)} ${twoDigit(
26+
counter3
27+
)} ${twoDigit(counter4)}`;
28+
29+
let csvRow = `Unknown${index},,,,,,,,,,,,,,,,,,,,,,,,,,,,,Mobile,${number}`;
30+
fs.appendFileSync(fileName, csvRow + "\n");
31+
32+
counter4++;
33+
34+
console.log(`${index} -> ${number}`);
35+
}
36+
37+
function twoDigit(number) {
38+
var twodigit = number >= 10 ? number : "0" + number.toString();
39+
return twodigit;
40+
}

index.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const puppeteer = require("puppeteer");
22
const chrono = require("chrono-node");
3+
const fs = require("fs");
34
const { InfluxDB } = require("@influxdata/influxdb-client");
45
require("dotenv").config();
56
console.log(`CONTACT_TARGET=${process.env.CONTACT_TARGET}`);
@@ -29,6 +30,12 @@ const writeApi = client.getWriteApi(
2930
process.env.INFLUXDB_ORG,
3031
process.env.INFLUXDB_BUCKET
3132
);
33+
const fileName = "scan-logs.csv";
34+
fs.appendFileSync(
35+
fileName,
36+
"scanDate,contactLabel,HasWhatsapp,LastSeenDate" + "\n"
37+
);
38+
let noWhatsappContacts = [];
3239

3340
(async () => {
3441
const page = await loadBrowser();
@@ -38,7 +45,13 @@ const writeApi = client.getWriteApi(
3845
while (true) {
3946
for (let index = 0; index <= 99; index++) {
4047
const contact = `Unknown${index}`;
41-
await scanStatus(page, contact);
48+
49+
if (noWhatsappContacts.includes(contact)) {
50+
continue;
51+
}
52+
53+
let result = await scanStatus(page, contact);
54+
fs.appendFileSync(fileName, result + "\n");
4255
await page.waitForTimeout(10000);
4356
}
4457
}
@@ -97,10 +110,11 @@ async function scanStatus(page, contactTarget) {
97110
console.log(
98111
`No whatsApp for ${contactTarget} (or not in phone contacts list).`
99112
);
100-
return;
113+
noWhatsappContacts.push(contactTarget);
114+
return `${new Date().toISOString()},${contactTarget},false,null`;
101115
}
102116
contactElt.click();
103-
await page.waitForTimeout(30000); // Status shows up late sometimes.
117+
await page.waitForTimeout(10000); // Status shows up late sometimes.
104118

105119
let statusElt = await page.$("._3Id9P"); // Status text.
106120

@@ -110,14 +124,14 @@ async function scanStatus(page, contactTarget) {
110124

111125
if (!statusElt) {
112126
console.log(`No status available for ${contactTarget}.`);
113-
return;
127+
return `${new Date().toISOString()},${contactTarget},true,null`;
114128
}
115129
let status = await statusElt.evaluate(x => x.textContent); // `last seen today at 13:15` format.
116130
console.log(`Status for ${contactTarget} is '${status}'.`);
117131

118132
if (status == "click here for contact info") {
119133
console.log(`'click here for contact info' case for ${contactTarget}.`);
120-
return;
134+
return `${new Date().toISOString()},${contactTarget},null,null`;
121135
}
122136

123137
let lastSeenDate = chrono.parseDate(status);
@@ -131,4 +145,5 @@ async function scanStatus(page, contactTarget) {
131145

132146
let data = `status,contactName=${contactTarget} offlineSince=${offlineSince}u`;
133147
writeApi.writeRecord(data);
148+
return `${new Date().toISOString()},${contactTarget},null,${lastSeenDate.toISOString()}`;
134149
}

0 commit comments

Comments
 (0)