-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcerts.js
164 lines (135 loc) · 4.56 KB
/
certs.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
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
'use strict'
const {Builder, By, Key, until, Capabilities} = require('selenium-webdriver');
//const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const chromedriver = require('chromedriver');
require('dotenv').config({path: '.env'})
console.log(process.env.LOGIN)
console.log(process.env.PASSWORD)
console.log(process.env.KEY)
const caps = new Capabilities();
caps.setPageLoadStrategy("normal");
const options = new chrome.Options();
options.headless();
options.windowSize=({height:1920,width:1080});
let driver;
function driveron () {
driver = new Builder().
withCapabilities(caps).
forBrowser('chrome').
setChromeOptions(options).
build();
}
function driveroff() {
driver.close();
}
const args = process.argv;
console.log('args: ' + args[0] + ':' + args[1] + ':' + args[2])
var records = [];
let updateMap = [];
let updateCerts = [];
var jsforce = require('jsforce');
var conn = new jsforce.Connection();
conn.login(process.env.LOGIN, process.env.PASSWORD + process.env.KEY, function(err, res) {
var query = conn.query('SELECT id, lastmodifieddate, name, userid__c, Certs__c FROM ' +
' LeaderBoard__c WHERE (userid__c = \'' + args[2] + '\') order by lastmodifieddate, Badges__c limit 1 ')
.on("record", function(record) {
records.push(record);
})
.on("end", function() {
//console.log(records);
for (var i=0; i<records.length; i++) {
var record = records[i];
//console.log(record);
//console.log("UserID: " + record["Userid__c"]); //the field names have to be in the EXACT format returned in the object
getdata(record["Userid__c"], record["Id"], i, query.totalFetched - 1);
}
})
.on("error", function(err) {
})
.run({ autoFetch : true, maxFetch : 4000 }); // synonym of Query#execute();
});
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getdata(tid, id, inti, ttlrecords) {
driveron()
try {
const url = "https://trailblazer.me/id/"+ tid; //query.id;
await driver.get(url);
await new Promise(r => setTimeout(r, 5000));
try {
let elShowMore = await driver.findElements(By.xpath("//*[text()='Show More']"));
console.log('1. count of show more text:' + elShowMore.length);
if (elShowMore.length == 2) {
for(var property in elShowMore) {
console.log('length 2: ' + property + "=" + elShowMore[property]);
if (property==1){
elShowMore[property].click();
await elShowMore[property].getText();
}
}
}
if (elShowMore.length == 1) {
for(var property in elShowMore) {
console.log('length 1: ' + property + "=" + elShowMore[property]);
if (property==0){
elShowMore[property].click();
await elShowMore[property].getText();
}
}
}
} catch(e) {
console.log('Error 1' + e)
}
let inti;
try {
let elcerts = await driver.findElements(By.xpath("//*[text()='Salesforce Certification']"));
console.log('# of certs:' + elcerts.length);
inti = elcerts.length;
} catch(e) {
console.log('Error 2')
}
console.log('certs:' + inti);
updateCerts = [];
for (let i = 0; i <= inti; i++) {
let elcertsItem = await driver.findElements(By.xpath('//*[@id="aura-directive-id-4"]/c-lwc-certifications/c-lwc-card/article/div/slot/c-lwc-achievements-certification-item[' + i + ']'));
for(let c of elcertsItem) {
let strc = await c.getText();
updateCerts[i] = strc.split(/\n/);
updateCerts[i].shift();
updateCerts[i].pop();
}
}
console.log(updateCerts);
driveroff();
let i = 0;
if (inti !=0) {
updateMap[i] = {
Id:id,
Certifications__c : '<div>' + inti + '</div>' + updateCerts.join('</br>')
}
}
} catch (e) {
console.log('Error 4' + e);
}
finally {
}
if (true) {
updateMap = updateMap.filter(function(e){return e});
//console.log(updateMap);
conn.sobject("LeaderBoard__c").update(updateMap,
function(err, rets) {
if (err) { return console.error('err:' , err); }
for (var i=0; i < rets.length; i++) {
if (rets[i].success) {
console.log("Updated Successfully : " + rets[i].id + ' ' + rets[i]);
} else {
console.log('problem?', err)
}
}
});
updateMap = [];
}
//await driver.close();
}