-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
216 lines (188 loc) · 6.72 KB
/
index.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import firebaseDB from "./firebaseDB.js";
import github from "./github.js";
import adzuna from "./adzuna.js";
import glassdoor from "./glassdoor.js";
import indeed from "./indeed.js";
import linkedin from "./linkedin.js";
import algolia from "./algolia.js";
import {getDate} from "./utilities.js";
import workintech_api from "./workintech-aloglia-api.js"
import puppeteer from "puppeteer";
import algoliasearch from "algoliasearch";
const pushArrayToObject = (arr, obj) => arr.forEach(el => obj[el.id] = el);
const getResultsParallel = async ({results, testing, queries}) => {
const browser = await puppeteer.launch();
await Promise.all(queries.map(async query => {
const promises = await Promise.all([github(testing, query),
adzuna(testing, query),
glassdoor(testing, query, browser),
indeed(testing, query, browser),
linkedin(testing, query, browser),
workintech_api(testing, query),
]);
const initial = Object.keys(results).length;
let total = 0;
promises.forEach(promise => {
promise.forEach(el=>el.query = query)
pushArrayToObject(promise, results)
total+=promise.length;
});
const final = Object.keys(results).length;
console.log({
total,
new : final - initial,
duplicates : total - (final - initial),
final
});
return promises;
}))
await browser.close();
}
const getResults = async ({results, testing, queries}) => {
const browser = await puppeteer.launch();
for (const query of queries) {
// console.log(query);
const promises = await Promise.all([
github(testing, query),
adzuna(testing, query),
glassdoor(testing, query, browser),
indeed(testing, query, browser),
linkedin(testing, query, browser),
workintech_api(testing, query),
]);
promises.forEach(pr=>pr.forEach(el=>el.query = query));
let streak = 0;
const initial = Object.keys(results).length;
let total = 0;
promises.forEach(promise => {
const initial = Object.keys(results).length;
pushArrayToObject(promise, results)
const afterUpdate = Object.keys(results).length;
total+=promise.length;
if((afterUpdate - initial) === 0 && promise.length > 0) streak++;
else streak = 0;
if(streak>5) console.log({
streak,
source: promise[0].source,
initial,
afterUpdate,
fetch : promise.length,
diff : afterUpdate - initial
})
})
const final = Object.keys(results).length;
// console.log({
// total,
// new : final - initial,
// duplicates : total - (final - initial),
// final
// });
}
await browser.close();
}
const deleteOldJobs = async (jobsCollectionRef, results) => {
const snapshot = await jobsCollectionRef.get();
const snapshotData = await snapshot.docs.map(doc => doc.data())
console.log(`retrieved snapshot from firebase ${snapshotData.length}`)
console.log(`deleting old jobs`)
let batch = firebaseDB.batch()
let count = 0;
for (const job of snapshotData) {
if (!results.some(j => j.id === job.id)) {
batch.delete(jobsCollectionRef.doc(job.id));
count++;
if(count===499 || job === snapshotData[snapshotData.length - 1]){
count = 0;
const committed = await batch.commit();
console.log(committed)
batch = firebaseDB.batch()
}
}
}
}
const updateNewJobs = async (jobsCollectionRef, results) => {
console.log('starting batch')
let batch = firebaseDB.batch()
let count = 0;
for (const result of results) {
if(!result.id) result.id = jobsCollectionRef.doc().id;
const docRef = jobsCollectionRef.doc(result.id);
console.log(results.indexOf(result))
batch.set(docRef, result);
count++;
if(count===499 || result === results[results.length - 1]){
count = 0;
const committed = await batch.commit();
console.log(committed)
batch = firebaseDB.batch()
}
}
}
const commitJobs = async () => {
const jobsCollectionRef = firebaseDB.collection("jobs");
let results = {};
const args = {
results: results,
testing: false,
queries: [
'Software Engineer',
'Software Developer',
'Web Developer',
'Web Software Developer',
'Web Software engineer',
'fullstack Developer',
'javascript Developer',
'Full stack Developer',
'Front end Developer',
'Back end Developer',
'react developer',
'react software developer',
'react engineer',
'react software engineer',
'vue developer',
'angular developer',
'php developer',
'wordpress developer',
'java developer',
'java software developer',
'python developer',
'django developer',
'Junior Software Developer',
'Junior fullstack Developer',
'Junior Software Engineer',
'Junior Developer',
]
};
console.time()
await getResultsParallel(args)
console.timeEnd()
const uniqueJobs = Object.values(results);
console.log(`add datetimestamps`)
uniqueJobs.forEach(job=>job.created = getDate(job.created))
console.log(uniqueJobs.length)
await deleteOldJobs(jobsCollectionRef, uniqueJobs)
await updateNewJobs(jobsCollectionRef, uniqueJobs)
await algolia(uniqueJobs)
}
const transfer_from_algolia_to_firestore = async () => {
let hits = [];
const client = algoliasearch('KCCE701SC2', '719a29d1dfb3929dd72afd2b3c35c3ab');
const index = client.initIndex('thejobhunt');
await index.browseObjects({
query: '', // Empty query will match all records
batch: batch => {
hits = hits.concat(batch);
}
}).then(() => {
hits.forEach(hit=> delete hit.objectID)
const jobsCollectionRef = firebaseDB.collection("jobs");
deleteOldJobs(jobsCollectionRef, hits)
.then(()=>
updateNewJobs(jobsCollectionRef, hits)
.then(()=>
console.log('stored'))
)
});
}
commitJobs().then( res => console.log(res)).catch(err => console.log(err))
// transfer_from_algolia_to_firestore().then( res => console.log(res)).catch(err => console.log(err))