-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostings.js
executable file
·599 lines (493 loc) · 13.6 KB
/
postings.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
function sendScraperRequest (divId,url)
{
// Overview:
// send request to scraper.
// scraper should send back an array of json objects.
// can either dump the objects to the dom, store them in db, or javascript.
$.ajax
(
{
url:"butler.php",
type: "post",
dataType: "text",
data:
{
user:gregsList.user.name,
pass:gregsList.user.password,
func: "scrapePostings",
url: url// encodeURIComponent(url),
// url: encodeURIComponent(url)
},
success: function(resp)
{
console.log('got a response from scraper');
console.log(JSON.parse(resp));
gregsList.refresh();
} // end success func
} // end ajax json
); // end ajax function
}
function getIndeedScraperDialogString(divId)
{
// create and return a string of html
var string = '<div id="'+divId+'" title="'+divId+'">';
string += '<table>';
string += '<tr><td>Keyword: <input id="scraperKeyword"></td></tr>';
string += '<tr><td>Location: <input id="scraperLocation"></td></tr>';
string += '<tr><td>Limit: <input id="scraperLimit"></td></tr>';
string += '</table></div>';
return string;
}
function getScraperParameters()
{
// sample url.
// url = "http://www.indeed.com/jobs?q=software+developer&l=San+Francisco,+CA&limit=75&start=0";
// url = "http://www.indeed.com/jobs?q=software+developer&l=San+Francisco,+CA&limit=100&start=0";
// get url from dialog
var url = "http://www.indeed.com/jobs?q=";
// url += $("#scraperKeyword")[0].value; // need to replace all spaces in the string with plus signs
var keyword = $("#scraperKeyword")[0].value; // get value from input box
keyword = keyword.replace(/ /g,"+"); // replace ' ' chars with '+' throughout entire string
url += keyword; // append replaced string to url
url += "&l="; // append location flag
var location = $("#scraperLocation")[0].value; // get value from input box
location = location.replace(/ /g,"+"); // replace ' ' chars with '+' throughout entire string
url += location; // append replaced string to url
// url += "&limit=100"; // hardcode limit for now.
url += "&limit="; // softcode limit.
var limit = $("#scraperLimit")[0].value; // get value from input box
url += limit;
console.log('scraping url:');
console.log(url);
return url;
}
function openScraperDialog()
{
// get dialog string
var divId = 'scraperDialog';
var scraperDialogString = getIndeedScraperDialogString(divId);
$(scraperDialogString).dialog(
{
modal:true,
buttons:
{
Cancel: function()
{
$(this).dialog("close");
},
"Scrape":function()
{
var url = getScraperParameters();
// remove input boxes
var toDestroy = ["scraperLocation","scraperKeyword",
"scraperLimit","scraperDialog"];
for (var i=0; i < toDestroy.length; i++)
removeElement(toDestroy[i]);
// get postings from scraper, store them in div
$(this).dialog("close");
sendScraperRequest(divId,url);
}
}
});
}
function scrapePostings()
{
// startup messages
console.log("scraping postings.");
console.log(this);
// open input dialog
openScraperDialog();
// get results from ajax
// sendScraperRequest();
}
function insertPosting()
{
var object = this.postings;
console.log("insert posting object");
console.log(object);
// build form
var string = '<div id="postingUpdater" title="Add '+object.type+'">';
string += '<table>';
string += '<tr>';
string += '<td>title</td>';
string += '<td>';
string += '<input id="embedTitle" value="">';
string += '</td>';
string += '</tr>';
string += '<tr><td>url</td>';
string += '<td><input id="embedURL" value=""></td></tr>';
string += '<tr><td>company</td>';
string += '<td><input id="embedCompany" value=""></td></tr>';
string += '<tr><td>location</td>';
string += '<td><input id="embedLocation" value=""></td></tr>';
string += '<tr><td>source</td>';
string += '<td><input id="embedSource" value=""></td></tr>';
string += '</table></div>';
$(string).dialog
(
{
modal:true,
buttons:
{
Cancel: function()
{
$(this).dialog("close")
},
"Add":function()
{
// get values from form
var title = $("#embedTitle")[0].value;
// var url = '<a href = \\"' + $("#embedURL")[0].value + '\\">link</a>';
var url = $("#embedURL")[0].value;
// instead of using a string, try creating an element,
// and embedding the url in element
var comp = $("#embedCompany")[0].value;
var loc = $("#embedLocation")[0].value;
var source = $("#embedSource")[0].value;
console.log("title:"+title);
console.log("url:"+url);
console.log("comp:"+comp);
console.log("loc:"+loc);
console.log("source:"+source);
// give values to butler
$(this).dialog("close");
// emptyElement($("#postingUpdater")[0]);
var toDestroy =
[
"embedSource","embedLocation",
"embedCompany","embedURL",
"embedTitle","postingUpdater"
];
for (var i=0; i < toDestroy.length; i++)
removeElement(toDestroy[i]);
$.ajax
(
{
url: "butler.php",
type: "post",
dataType: "text",
data:
{
user: object.parent.user.name,
pass: object.parent.user.password,
func: "insertPosting",
url: url,// encodeURIComponent(url),
//url: encodeURIComponent(url),
title: title,
company: comp,
location: loc,
source: source
},
success: function(resp)
{
if (resp !== '')
{
console.log(JSON.parse(resp));
if (JSON.parse(resp) === true)
{
console.log("input worked");
// displayTable(object,[]);
// getStuff(object.parent);
object.parent.refresh();
}
else
{
console.log("input failed");
}
}
else
console.log('empty response');
//
// clear text fields
} // end success func
} // end ajax json
) // end ajax parameters
} // end add button func
} // end buttons
} //end dialog json
) //end dialog func
} //end insertPosting func
/*
Open a dialog box containing the current posting's contents.
Send them off to the server when finished.
*/
function editPosting()
{
var object = this.data;
console.log("this.sid:");
console.log(this.sid);
var link = this.sid;
console.log("object.destroyer:");
console.log(object.destroyer);
var index;
// find hash index
for (var i=0; i < object.contents.length; i++)
{
if (object.contents[i].sid === link)
index = i;
}
console.log("index:"+index);
var current = object.contents[index];
var linkString = current.url;
var urlString = linkString.substr(linkString.lastIndexOf("href=\"")+1,
linkString.lastIndexOf("</a>"));
console.log("urlString: "+urlString);
console.log("linkString: "+ linkString);
// build form
var string = '<div id="postingUpdater" title="Update '+object.type+'">';
string += '<table>';
string += '<tr>';
string += '<td>title</td>';
string += '<td>';
string += '<input id="embedTitle" value="'+current.title+'">';
string += '</td>';
string += '</tr>';
string += '<tr><td>url</td>';
string += '<td><input id="embedURL" value="'+urlString+'"></td></tr>';
string += '<tr><td>company</td>';
string += '<td><input id="embedCompany" value="'+current.company+'"></td></tr>';
string += '<tr><td>location</td>';
string += '<td><input id="embedLocation" value="'+current.location+'"></td></tr>';
string += '<tr><td>source</td>';
string += '<td><input id="embedSource" value="'+current.source+'"></td></tr>';
string += '</table></div>';
$(string).dialog
(
{
modal:true,
buttons:
{
Cancel: function()
{
$(this).dialog("close")
},
"Update":function()
{
// get values from form
var title = $("#embedTitle")[0].value;
var url = $("#embedURL")[0].value;
var comp = $("#embedCompany")[0].value;
var loc = $("#embedLocation")[0].value;
var source = $("#embedSource")[0].value;
console.log("title:"+title);
console.log("url:"+url);
console.log("comp:"+comp);
console.log("loc:"+loc);
console.log("source:"+source);
// give values to butler
$(this).dialog("close");
// emptyElement($("#postingUpdater")[0]);
var toDestroy =
[
"embedSource","embedLocation",
"embedCompany","embedURL",
"embedTitle","postingUpdater"
];
for (var i=0; i < toDestroy.length; i++)
removeElement(toDestroy[i]);
$.ajax
(
{
url: "butler.php",
type: "post",
dataType: "text",
data:
{
user: object.parent.user.name,
pass: object.parent.user.password,
func: object.updater,
sid: link,
title: title,
url: encodeURIComponent(url),
company: comp,
location: loc,
source: source
},
success: function(resp)
{
console.log(resp);
//console.log(JSON.parse(resp));
//
if (JSON.parse(resp) === true)
{
console.log("removal worked");
// displayTable(object,[]);
// getStuff(object);
object.parent.refresh();
}
else
{
console.log("removal failed");
}
}
}
);
}
}
}
);
}
function setMotivation(object,postingId,motivation)
{
console.log("setting motivation for " + postingId);
console.log(object);
$.ajax
(
{
url: "butler.php",
type: "post",
dataType: "text",
data:
{
user: object.parent.user.name,
pass: object.parent.user.password,
func: "setMotivation",
postingId: postingId,
motive: motivation,
},
success: function(resp)
{
console.log(resp);
//console.log(JSON.parse(resp));
//
if (JSON.parse(resp) === "true")
{
// console.log("removal worked");
// displayTable(object);
getStuff(object);
// object.parent.refresh();
}
else
{
console.log("setMotivation() failed");
}
}
}
);
}
function togglePostingStatus(object,postingId)
{
$.ajax
(
{
url: "butler.php",
type: "post",
dataType: "text",
data:
{
user: object.parent.user.name,
pass: object.parent.user.password,
func: "togglePostingStatus",
postingId: postingId,
},
success: function(resp)
{
console.log(resp);
//console.log(JSON.parse(resp));
//
if (JSON.parse(resp) === true)
{
// console.log("removal worked");
// displayTable(object,[]);
getStuff(object);
// object.parent.refresh();
}
else
{
console.log("togglePostingStatus() failed");
}
}
}
);
}
/*
Add input boxes to all of the postings.
This could be generalized to companies and industries also,
but for now it's just postings.
*/
function addMotivationInputs(object)
{
console.log("adding motivation inputs");
var tableId = object.table.id;
// get the nth children (8th in this case)
var ids = $("#" + tableId + " tr td:nth-child(1)");
var cells = $("#" + tableId + " tr td:nth-child(8)");
// console.log(cells);
// console.log(ids);
// loop over cells, adding input elements
for ( var i = 0; i < cells.length; i++ )
{
// get current contents
// var prev = cells[i].html();
var thisCell = cells[i];
var thisId = ids[i].innerHTML;
// console.log(thisId);
// console.log(thisCell);
var content = thisCell.innerHTML;
// console.log("emptying element");
emptyElement(thisCell);
// append an input element
var input = addInput(thisCell, 'number', '', content);
input.sid = thisId;
// handle change events
input.onchange = function()
{
console.log("setting " + this.sid + " to " + this.value);
// if (this.value
setMotivation(object,this.sid,this.value);
}
// cells[i].html('<input type="number"></input>');
}
}
function displayPostingCounts()
{
}
/*
erase gregsList object's postings and rebuild them from input
*/
function fillPostings(object, input)
{
console.log("object");
console.log(object);
// erase object's contents and refill them from input
object.contents = [];
var contents = object.contents;
for (var i = 0; i < input.ids.length; i++)
{
var id = input.ids[i];
var title = input.titles[i];
var url = "<a href=\""+input.urls[i]+"\">link</a>";
var location = input.locations[i];
var company = input.companies[i];
var source = input.sources[i];
var status = input.statuses[i];
var motivation = input.motivations[i];
var p = new posting(id,title,url,location,
company,source,status,motivation);
// console.log("p");
// console.log(p);
contents.push(p);
}
displayTable(object);
// displayPostingsPortlet(object);
// embelishTable(object,popUpDialogForJobPosting);
embelishTable(object,dialogObjectWrapper2);
addMotivationInputs(object);
// display counts and next/prev buttons
displayPostingCounts();
}
function displayPostingsPortlet(object)
{
$("#postingsPortlet").empty();
var main = $("#postingsPortlet")[0];
var table = createAppendedChildToParent('table',main);
table.className += "io";
for (var i = 0; i < object.contents.length; i++)
{
var tr = createAppendedChildToParent('tr',table);
var td = createAppendedChildToParent('td',tr);
var value = object.contents[i].title;
var content = document.createTextNode(value);
td.appendChild(content);
}
}