-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scrapper.js
495 lines (376 loc) · 15 KB
/
Scrapper.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// ==UserScript==
// @name Scrapper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Scraper appliancerepairmen
// @author Randy Gil
// @match http://technician.theappliancerepairmen.com/artis-tech-home/available-jobs/
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @resource w3css https://www.w3schools.com/w3css/4/w3.css
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addStyle
// @grant GM_getResourceText
// @grant GM_getResourceURL
// ==/UserScript==
document.head.appendChild(cssElement(GM_getResourceURL ("w3css")));
function cssElement(url) {
let link = document.createElement("link");
link.href = url;
link.rel="stylesheet";
link.type="text/css";
return link;
}
let fillTable
const DEBUG = true;
(function() {
let STOP = false
let CONFIG = JSON.parse(localStorage.getItem('config'))
if (!CONFIG) {
CONFIG = {
appliance: '',
paymentMethod: '',
zones: []
}
}
function addVueApp() {
let D = document;
let targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
let scriptNode = D.createElement ('script');
scriptNode.src = DEBUG ? 'http://localhost:8080/dist/build.js' : 'https://cdn.jsdelivr.net/gh/randygil/scraper-config@master/dist/build.js';
targ.appendChild (scriptNode);
}
let tabID = sessionStorage.tabID && sessionStorage.closedLastTab !== '2' ? sessionStorage.tabID : sessionStorage.tabID = Math.random();
sessionStorage.closedLastTab = '2';
$(window).on('unload beforeunload', function() {
sessionStorage.closedLastTab = '1';
});
function isEnabled() {
return localStorage.getItem('enable') === tabID
}
function dateToString(date) {
return `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`
}
let vueApp = document.createElement ('div');
vueApp.id = 'app'
let body = document.querySelector('body')
body.appendChild(vueApp)
addVueApp()
/* Inicio del script */
let list = []
let jobs = $("#availablejobs").children('.left.even')
$.each(jobs,function(a,e) {
let content = $(e).first().children('.span-12').first()
let jobn = $($(content).children('.span-6')[0]).text()
let location = $($(content).children('.span-6')[1]).text()
let schedule = $($(content).children('.span-6')[2]).text()
let appliance = $($(content).children('.span-4')[4]).text()
let brand = $($(content).children('.span-4')[6]).text()
let paymentMethod = $($(content).children('.span-4')[8]).text()
let serviceFee = $($(content).children('.span-6')[3]).text()
let jobType = $($(content).children('.span-6')[13]).text()
let symptoms = $($(content).children('.span-12')[0]).text()
list.push({
job: jobn,
location: location.toLowerCase(),
appliance: appliance.toLowerCase(),
brand: brand.toLowerCase(),
schedule: schedule.toLowerCase(),
paymentMethod: paymentMethod.toLowerCase(),
serviceFee: serviceFee.toLowerCase(),
symptoms: symptoms.toLowerCase(),
})
});
//let zone = GM_config.get('zone')
let filtersZone = []
for (let zone of CONFIG.zones) {
if (zone.status) {
let configs = zone.zipcode.split('\n')
filtersZone = filtersZone.concat(configs)
}
}
let filtersAppliance = CONFIG.appliance.split('\n')
let filtersPaymentMethod = CONFIG.paymentMethod.split('\n')
filtersPaymentMethod = filtersPaymentMethod.map((e) => e.toLowerCase())
if (DEBUG) {
list = [
{
job: '123123',
appliance: 'Refrigerator (Side By Side)',
location: ' ...terrace, doral Florida 33178',
paymentMethod: 'credit card',
schedule: 'August 8, 2019 9 AM-11 AM',
symptoms: 'prueba de simtomas'
}
]
}
//console.log(list)
//console.log(filtersZone)
if (isEnabled()) {
$("body").prepend('<a class="btn btn-danger" href="" id="toggleEnable">Desactivar Scraper</a>')
}else {
$("body").prepend('<a class="btn btn-danger" href="" id="toggleEnable">Activar Scraper</a>')
}
$("#toggleEnable").click(function(e) {
e.preventDefault();
if (!isEnabled()) {
localStorage.setItem('enable', tabID)
alert('Activado en esta pestaña')
window.location.reload()
} else {
localStorage.removeItem('enable')
alert('Desactivado en esta pestaña')
STOP = true
}
});
// $("body").prepend(`<a class="btn btn-primary" href="" id="openconfig">Configurar scraper (${filtersZone.length}) ${DEBUG == true ? '- Desarrollo': ''} </a>`)
if (CONFIG.status == 'Activo') {
$("body").prepend('<a class="btn btn-primary" href="" id="toggleStatus">Detener scraper</a>')
}
$("#openconfig").click(function(e) {
e.preventDefault();
GM_config.open();
STOP = true;
});
$("#toggleStatus").click(function(e) {
e.preventDefault();
alert('detenido');
STOP = true;
});
let status = CONFIG.status
let jobsList = localStorage.getItem('jobs') ? JSON.parse(localStorage.getItem('jobs')) : [];
if (status == 'Activo' && isEnabled()) {
console.log('activo')
let matches = []
let elements = list.filter((elem, index) => {
let findCodeMatch
let zipCodeMatch = CONFIG.zones.find((zone) => {
if (zone && zone.status) {
let f = elem.schedule
let month = f.split(',')[0].split(' ')[0]
let day = f.split(',')[0].split(' ')[1]
let year = f.split(',')[1].trim().split(' ')[0]
let hourA = parseInt(f.split(',')[1].trim().split(' ')[1])
let ttA = f.split(',')[1].trim().split(' ')[2].split('-')[0]
if (ttA == 'NN') {
hourA = 0;
ttA = 'PM'
}
if (ttA == 'PM') {
hourA +=12
}
let hourB = parseInt(f.split(',')[1].trim().split(' ')[2].split('-')[1])
let ttB = f.split(',')[1].trim().split(' ')[3]
if (ttB == 'NN') {
hourB = 0;
ttB = 'PM'
}
if (ttB == 'PM') {
hourB +=12
}
console.log('hourA', hourA)
hourA = new Date(`${year}-${month}-${day} ${hourA}:00`)
hourB = new Date(`${year}-${month}-${day} ${hourB}:00`)
let from = new Date(`${year}-${month}-${day}`)
from.setHours(zone.timeStart.split(':')[0])
from.setMinutes(zone.timeStart.split(':')[1])
let to = new Date(`${year}-${month}-${day}`)
to.setHours(zone.timeEnd.split(':')[0])
to.setMinutes(zone.timeEnd.split(':')[1])
const today = new Date()
console.log(`${hourA} >= ${from} && ${hourA} <= ${to} && ${hourB} <= ${to}`)
if (!(hourA >= from && hourA <= to && hourB <= to )) {
return false
}
const find = zone.dates.filter((date) => {
date = dateToString(new Date(date))
return date == dateToString(hourB)
})
if (find[0]) {
return false
}
const zipcodes = zone.zipcode.split('\n')
let findzipCodeMatch = zipcodes.find((zipcode) => {
if (zipcode) {
let res = elem.location.includes(zipcode)
return res
}
})
findCodeMatch = findzipCodeMatch
return findzipCodeMatch
}
})
console.log(zipCodeMatch)
let paymentMethodMatch = filtersPaymentMethod.find((paymentMethod) => {
if (paymentMethod) {
let res = elem.paymentMethod.includes(paymentMethod.toLowerCase())
return res
}
})
let applianceMatch = filtersAppliance.find((appliance) => {
if (appliance) {
let res = elem.appliance.toLowerCase().includes(appliance.toLowerCase())
return res
}
})
// console.log(findCodeMatch, applianceMatch, paymentMethodMatch)
elem.match = {}
if (zipCodeMatch) {
elem.match.location = findCodeMatch
}
if (applianceMatch) {
elem.match.appliance = applianceMatch
}
if (paymentMethodMatch) {
elem.match.paymentMethod = paymentMethodMatch
}
console.log('match', elem.match)
list[index] = elem
if (!!zipCodeMatch && !!applianceMatch && !!paymentMethodMatch) {
return true
}
}, [])
//console.log(list)
for (let e=0; e<= list.length; e++) {
let actualJob = list[e]
if (actualJob) {
//console.log('actualJob', actualJob)
let findJob = jobsList.find((e) => { return e.job === actualJob.job } )
if (!findJob) { jobsList.unshift(actualJob) }
}
}
localStorage.setItem('jobs', JSON.stringify(jobsList));
if (elements.length>0) {
let job = elements[0]
let localjobs = localStorage.getItem('jobsAccepted') ? JSON.parse(localStorage.getItem('jobsAccepted')) : [];
localjobs.unshift(job)
localStorage.setItem('jobsAccepted', JSON.stringify(localjobs) );
let url = DEBUG ? 'http://localhost:8000/notify.php' : 'http://localhost/scraper/notify.php'
const data = {
action: 'jobAccepted',
...job
}
fetch(url,{
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data),
}).then((e) => {
e.text()
}).then((e) => {
console.log(e)
})
if (!DEBUG) {
$('#book-'+elements[0].job).submit();
} else {
alert('¡Simulacro! Aceptaré el trabajo n° '+job.job)
}
}
setTimeout(function() {
if (!STOP) {
window.location.reload()
}
}, CONFIG.interval*1000)
}
let tableContent = `
<table class="w3-table w3-bordered w3-striped w3-border test w3-hoverable">
<thead>
<tr class="w3-green">
<th>Fecha</th>
<th>Numero</th>
<th>Location</th>
<th>Appliance</th>
<th>Payment</th>
<th>Symptoms</th>
</tr>
</thead>
<tbody id="jobsList">
`;
tableContent += `</tbody><tbody>
</tbody>
</table>`;
let modalhtml = `
<div id="id01" class="w3-modal">
<div class="w3-modal-content" style=" width: 100%;>
<div class="w3-container">
<span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-display-topright">×</span>
${tableContent}
<button class="w3-btn w3-black" id="prevPage" >Atras</button>
<button class="w3-btn w3-black" id="nextPage" >Siguiente</button>
<button class="w3-btn w3-black" id="descargarCsv" >Exportar</button>
</div>
</div>
</div>
`;
$("body").prepend('<a class="btn btn-success" href="" id="openHistorial">Ver historial</a>')
$("#openHistorial").click(function(event) {
STOP = true;
event.preventDefault();
document.getElementById('id01').style.display='block'
});
function paginate (array, page_size, page_number) {
--page_number; // because pages logically start with 1, but technically with 0
return array.slice(page_number * page_size, (page_number + 1) * page_size);
}
function fillTable(page_number) {
let jobsPaginated = paginate(jobsList, 6, pageNumber)
let tableContent = ''
$("#jobsList").html('')
for (const j of jobsPaginated) {
tableContent += `<tr>
<td>${j.schedule}</td>
<td>${j.job}</td>
<td>${j.location} ${j.match.location ? `<strong>(${j.match.location})</strong>` : ''}</td>
<td>${j.appliance} ${j.match.appliance ? `<strong>(${j.match.appliance})</strong>` : ''}</td>
<td>${j.paymentMethod} ${j.match.paymentMethod ? `<strong>(${j.match.paymentMethod})</strong>` : ''}</td>
<td>${j.symptoms}</td>
</tr>
`;
$("#jobsList").html(tableContent)
}
}
function downloadCsv() {
let csv = ''
const items = jobsList
csv += `Job N°, Schedule, Location, Appliance, Payment Method, Symptons \r\n`
// Loop the array of objects
console.log(items.length)
for(let row = 0; row < items.length; row++){
let keysAmount = Object.keys(items[row]).length
let keysCounter = 0
let j = items[row]
if (!j.match) {
j.match = {}
}
let fila = `${j.job}, ${j.schedule},${j.location} ${j.match.location ? `(${j.match.location})` : ''}, ${j.appliance} ${j.match.appliance ? `(${j.match.appliance})` : ''}, ${j.paymentMethod} ${j.match.paymentMethod ? `(${j.match.paymentMethod})` : ''}, ${j.symptoms}`
csv += fila + '\r\n'
keysCounter++
keysCounter = 0
}
// Once we are done looping, download the .csv by creating a link
let link = document.createElement('a')
link.id = 'download-csv'
link.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(csv));
link.setAttribute('download', 'export-jobs.csv');
document.body.appendChild(link)
document.querySelector('#download-csv').click()
}
$('body').prepend(modalhtml)
let pageNumber = 1
$("#prevPage").click(function() {
if (pageNumber > 1) {
pageNumber--;
fillTable(pageNumber)
}
});
$("#nextPage").click(function() {
pageNumber++
fillTable(pageNumber)
});
fillTable(pageNumber)
$('#descargarCsv').click(function(e) {
e.preventDefault();
console.log('click')
downloadCsv()
});
})();