HIT Scraper WITH EXPORT

Snag HITs.

目前为 2014-06-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name HIT Scraper WITH EXPORT
  3. // @author Kerek and TJ
  4. // @description Snag HITs.
  5. // Based in part on code from mmmturkeybacon Export Mturk History and mmmturkeybacon Color Coded Search with Checkpoints
  6. // @namespace http://userscripts.org/users/536998
  7. // @match https://www.mturk.com/mturk/findhits?match=true#hit_scraper*
  8. // @match https://www.mturk.com/mturk/findhits?match=true?hit_scraper*
  9. // @version 1.3.0.9
  10. // @grant GM_xmlhttpRequest
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_deleteValue
  14. // @require http://code.jquery.com/jquery-latest.min.js
  15. // ==/UserScript==
  16.  
  17. //alter the requester ignore last as you desire, case insensitive
  18. var ignore_list = ["oscar smith", "jonathon weber", "jerry torres", "Crowdsource", "we-pay-you-fast", "turk experiment", "jon brelig"];
  19.  
  20. //this searches extra pages if you skip too much, helps fill out results if you hit a chunk of ignored HITs. Change to true for this behavior.
  21. var correct_for_skips = false;
  22.  
  23. //weight the four TO ratings for the coloring. Default has pay twice as important as fairness and nothing for communication and fast.
  24. var COMM_WEIGHT = 0;
  25. var PAY_WEIGHT = 10;
  26. var FAIR_WEIGHT = 5;
  27. var FAST_WEIGHT = 0;
  28.  
  29. //display your hitdb records if applicable
  30. var check_hitDB = true;
  31.  
  32. //default text size
  33. var default_text_size=11;
  34.  
  35.  
  36.  
  37. var HITStorage = {};
  38. var indexedDB = window.indexedDB || window.webkitIndexedDB ||
  39. window.mozIndexedDB;
  40. window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.mozIDBTransaction;
  41. window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.mozIDBKeyRange;
  42. HITStorage.IDBTransactionModes = { "READ_ONLY": "readonly", "READ_WRITE": "readwrite", "VERSION_CHANGE": "versionchange" };
  43. var IDBKeyRange = window.IDBKeyRange;
  44.  
  45. HITStorage.indexedDB = {};
  46. HITStorage.indexedDB = {};
  47. HITStorage.indexedDB.db = null;
  48.  
  49. HITStorage.indexedDB.onerror = function(e) {
  50. console.log(e);
  51. };
  52.  
  53. var v=4;
  54.  
  55. HITStorage.indexedDB.checkTitle = function(title,button) {
  56. var request = indexedDB.open("HITDB", v);
  57. request.onsuccess = function(e) {
  58. HITStorage.indexedDB.db = e.target.result;
  59. var db = HITStorage.indexedDB.db;
  60. if (!db.objectStoreNames.contains("HIT"))
  61. {
  62. db.close();
  63. return;
  64. }
  65. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  66. var store = trans.objectStore("HIT");
  67.  
  68. var index = store.index("title");
  69. index.get(title).onsuccess = function(event)
  70. {
  71. if (event.target.result === undefined)
  72. {
  73. console.log(title + ' not found');
  74. history[button].titledb=false;
  75. }
  76. else
  77. {
  78. console.log(title + ' found');
  79. history[button].titledb=true;
  80. }
  81. db.close();
  82. };
  83. };
  84. request.onerror = HITStorage.indexedDB.onerror;
  85. };
  86.  
  87. HITStorage.indexedDB.checkRequester = function(id,button) {
  88. var request = indexedDB.open("HITDB", v);
  89. request.onsuccess = function(e) {
  90. HITStorage.indexedDB.db = e.target.result;
  91. var db = HITStorage.indexedDB.db;
  92. if (!db.objectStoreNames.contains("HIT"))
  93. {
  94. db.close();
  95. return;
  96. }
  97. var trans = db.transaction(["HIT"], HITStorage.IDBTransactionModes.READ_ONLY);
  98. var store = trans.objectStore("HIT");
  99.  
  100. var index = store.index("requesterId");
  101. index.get(id).onsuccess = function(event)
  102. {
  103. if (event.target.result === undefined)
  104. {history[button].reqdb=false;
  105. console.log(id + ' not found');
  106. }
  107. else
  108. {
  109. history[button].reqdb=true;
  110. console.log(id + ' found');
  111. }
  112. db.close();
  113. };
  114. };
  115. request.onerror = HITStorage.indexedDB.onerror;
  116. };
  117.  
  118. var PAGES_TO_SCRAPE = 3;
  119. var MINIMUM_HITS = 100;
  120. var SEARCH_REFRESH=0;
  121. var URL_BASE = "/mturk/searchbar?searchWords=&selectedSearchType=hitgroups";
  122. var initial_url = URL_BASE;
  123. var TO_REQ_URL = "http://turkopticon.ucsd.edu/reports?id=";
  124. var found_key_list=[];
  125. var last_clear_time = new Date().getTime();
  126. var searched_once = false;
  127. var save_new_results_time = 120;
  128. var save_results_time = 3600;
  129. var default_type = 0;
  130. var cur_loc = window.location.href;
  131. var time_input = document.createElement("INPUT");
  132. time_input.value = 0;
  133. var page_input = document.createElement("INPUT");
  134. page_input.value = 3;
  135. var min_input = document.createElement("INPUT");
  136. var new_time_display_input = document.createElement("INPUT");
  137. new_time_display_input.value = 300;
  138. var reward_input = document.createElement("INPUT");
  139. var qual_input = document.createElement("INPUT");
  140. qual_input.type = "checkbox";
  141. qual_input.checked = true;
  142. var masters_input = document.createElement("INPUT");
  143. masters_input.type = "checkbox";
  144. var sort_input1 = document.createElement("INPUT");
  145. sort_input1.type = "radio";
  146. sort_input1.name = "sort_type";
  147. sort_input1.value = "latest";
  148. sort_input1.checked = true;
  149. var sort_input2 = document.createElement("INPUT");
  150. sort_input2.type = "radio";
  151. sort_input2.name = "sort_type";
  152. sort_input2.value = "most";
  153. var sort_input3 = document.createElement("INPUT");
  154. sort_input3.type = "radio";
  155. sort_input3.name = "sort_type";
  156. sort_input3.value = "amount";
  157.  
  158. var search_input = document.createElement("INPUT");
  159.  
  160. var LINK_BASE = "https://www.mturk.com";
  161. var BACKGROUND_COLOR = "rgb(19, 19, 19)";
  162. var STATUSDETAIL_DELAY = 250;
  163. var MPRE_DELAY = 3000;
  164.  
  165. var next_page = 1;
  166.  
  167. var GREEN = '#66CC66'; // > 4
  168. var LIGHTGREEN = '#ADFF2F'; // > 3 GREEN YELLOW
  169. var YELLOW = '#FFD700';
  170. var ORANGE = '#FF9900'; // > 2
  171. var RED = '#FF3030'; // <= 2
  172. var BLUE = '#C0D9D9'; // no TO
  173. var GREY = 'lightGrey';
  174. var BROWN = '#94704D';
  175. var DARKGREY = '#9F9F9F';
  176. $('body').css('background', BACKGROUND_COLOR);
  177.  
  178. var API_PROXY_BASE = 'https://api.turkopticon.istrack.in/';
  179. var API_MULTI_ATTRS_URL = API_PROXY_BASE + 'multi-attrs.php?ids=';
  180. var REVIEWS_BASE = 'http://turkopticon.ucsd.edu/';
  181.  
  182. var control_panel_HTML = '<div id="control_panel" style="margin: 0 auto 0 auto;' +
  183. 'border-bottom: 1px solid #000000; margin-bottom: 5px; ' +
  184. 'background-color: ' + BACKGROUND_COLOR + ';"></div>';
  185. $('body > :not(#control_panel)').hide(); //hide all nodes directly under the body
  186. $('body').prepend(control_panel_HTML);
  187.  
  188. var control_panel = document.getElementById("control_panel");
  189. var big_red_button = document.createElement("BUTTON");
  190. var progress_report = document.createTextNode("Stopped");
  191. var text_area = document.createElement("TABLE");
  192. big_red_button.textContent = "Show Interface";
  193. big_red_button.onclick = function(){show_interface();};
  194. control_panel.appendChild(big_red_button);
  195.  
  196. show_interface();
  197.  
  198. var global_run = false;
  199. var statusdetail_loop_finished = false;
  200. var date_header = "";
  201. var history = {};
  202. var wait_loop;
  203.  
  204. function set_progress_report(text, force)
  205. {
  206. if (global_run == true || force == true)
  207. {
  208. progress_report.textContent = text;
  209. }
  210. }
  211.  
  212. function get_progress_report()
  213. {
  214. return progress_report.textContent;
  215. }
  216.  
  217. function wait_until_stopped()
  218. {
  219. if (global_run == true)
  220. {
  221. if (statusdetail_loop_finished == true)
  222. {
  223. big_red_button.textContent = "Start";
  224. set_progress_report("Finished", false);
  225. }
  226. else
  227. {
  228. setTimeout(function(){wait_until_stopped();}, 500);
  229. }
  230. }
  231. }
  232.  
  233. function display_wait_time(wait_time)
  234. {
  235. if (global_run == true)
  236. {
  237. var current_progress = get_progress_report();
  238. if (current_progress.indexOf("Searching again in")!==-1)
  239. {
  240. set_progress_report(current_progress.replace(/Searching again in \d+ seconds/ , "Searching again in " + wait_time + " seconds"),false);
  241. }
  242. else
  243. set_progress_report(current_progress + " Searching again in " + wait_time + " seconds.", false);
  244. if (wait_time>1)
  245. setTimeout(function(){display_wait_time(wait_time-1);}, 1000);
  246. }
  247. }
  248.  
  249. function dispArr(ar)
  250. {
  251. var disp = "";
  252. for (var z = 0; z < ar.length; z++)
  253. {
  254. disp += "id " + z + " is " + ar[z] + " ";
  255. }
  256. console.log(disp);
  257. }
  258.  
  259. function scrape($src)
  260. {
  261. var $requester = $src.find('a[href^="/mturk/searchbar?selectedSearchType=hitgroups&requester"]');
  262. var $title = $src.find('a[class="capsulelink"]');
  263. var $reward = $src.find('span[class="reward"]');
  264. var $preview = $src.find('a[href^="/mturk/preview?"]');
  265. var $qualified = $src.find('a[href^="/mturk/notqualified?"]');
  266. var $times = $src.find('a[id^="duration_to_complete"]');
  267. var $descriptions = $src.find('a[id^="description"]');
  268. var not_qualified_group_IDs=[];
  269. var $quals = $src.find('a[id^="qualificationsRequired"]');
  270. $qualified.each(function(){
  271. var groupy = $(this).attr('href');
  272. groupy = groupy.replace("/mturk/notqualified?hitId=","");
  273. not_qualified_group_IDs.push(groupy);
  274. });
  275. var $mixed = $src.find('a[href^="/mturk/preview?"],a[href^="/mturk/notqualified?"]');
  276. var listy =[];
  277. $mixed.each(function(){
  278. var groupy = $(this).attr('href');
  279. groupy = groupy.replace("/mturk/notqualified?hitId=","");
  280. groupy = groupy.replace("/mturk/preview?groupId=","");
  281. listy.push(groupy);
  282. });
  283. listy = listy.filter(function(elem, pos) {
  284. return listy.indexOf(elem) == pos;
  285. });
  286.  
  287. for (var j = 0; j < $requester.length; j++)
  288. {
  289. var $hits = $requester.eq(j).parent().parent().parent().parent().parent().parent().find('td[class="capsule_field_text"]');
  290. var requester_name = $requester.eq(j).text().trim();
  291. var requester_link = $requester.eq(j).attr('href');
  292. var group_ID=listy[j];
  293. var preview_link = "/mturk/preview?groupId=" + group_ID;
  294. var title = $title.eq(j).text().trim();
  295. var reward = $reward.eq(j).text().trim();
  296. var hits = $hits.eq(4).text().trim();
  297. var time = $times.eq(j).parent()[0].nextSibling.nextSibling.innerHTML;
  298. var description = $descriptions.eq(j).parent()[0].nextSibling.nextSibling.innerHTML;
  299. //console.log(description);
  300. var requester_id = requester_link.replace('/mturk/searchbar?selectedSearchType=hitgroups&requesterId=','');
  301. var accept_link;
  302. accept_link = preview_link.replace('preview','previewandaccept');
  303. /*HIT SCRAPER ADDITION*/
  304. var qElements = $quals.eq(j).parent().parent().parent().find('tr');
  305. //console.log(qElements);
  306.  
  307. var qualifications = [];
  308. for (var i = 1; i < qElements.length; i++) {
  309. qualifications.push((qElements[i].childNodes[1].textContent.trim().replace(/\s+/g, ' ').indexOf("Masters") != -1 ? "[color=red][b]"+qElements[i].childNodes[1].textContent.trim().replace(/\s+/g, ' ')+"[/b][/color]" : qElements[i].childNodes[1].textContent.trim().replace(/\s+/g, ' ')));
  310. }
  311. var qualList = (qualifications.join(', ') ? qualifications.join(', ') : "None");
  312.  
  313. key = requester_name+title+reward+group_ID;
  314. found_key_list.push(key);
  315. if (history[key] == undefined)
  316. {
  317. history[key] = {requester:"", title:"", description:"", reward:"", hits:"", req_link:"", quals:"", prev_link:"", rid:"", acc_link:"", new_result:"", qualified:"", found_this_time:"", initial_time:"", reqdb:"",titledb:"",time:""};
  318. history[key].req_link = requester_link;
  319. history[key].prev_link = preview_link;
  320. history[key].requester = requester_name;
  321. history[key].title = title;
  322. history[key].reward = reward;
  323. history[key].hits = hits;
  324. history[key].rid = requester_id;
  325. history[key].acc_link = accept_link;
  326. history[key].time = time;
  327. history[key].quals = qualList;
  328. history[key].description = description;
  329. HITStorage.indexedDB.checkRequester(requester_id,key);
  330. HITStorage.indexedDB.checkTitle(title,key);
  331. if (searched_once)
  332. {
  333. history[key].initial_time = new Date().getTime();//-1000*(save_new_results_time - SEARCH_REFRESH);
  334. history[key].new_result = 0;
  335. }
  336. else
  337. {
  338. history[key].initial_time = new Date().getTime()-1000*save_new_results_time;
  339. history[key].new_result = 1000*save_new_results_time;
  340. }
  341. if (not_qualified_group_IDs.indexOf(group_ID)!==-1)
  342. history[key].qualified = false;
  343. else
  344. history[key].qualified = true;
  345.  
  346. history[key].found_this_time = true;
  347. }
  348. else
  349. {
  350. history[key].new_result = new Date().getTime() - history[key].initial_time;
  351. history[key].found_this_time = true;
  352. history[key].hits = hits;
  353. }
  354. }
  355. }
  356.  
  357. function statusdetail_loop(next_URL)
  358. {
  359. if (global_run == true)
  360. {
  361. if (next_URL.length != 0)
  362. {
  363. $.get(next_URL, function(data)
  364. {
  365. var $src = $(data);
  366. var maxpagerate = $src.find('td[class="error_title"]:contains("You have exceeded the maximum allowed page request rate for this website.")');
  367. if (maxpagerate.length == 0)
  368. {
  369. set_progress_report("Processing page " + next_page, false);
  370. scrape($src);
  371. $next_URL = $src.find('a[href^="/mturk/viewsearchbar"]:contains("Next")');
  372. next_URL = ($next_URL.length != 0) ? $next_URL.attr("href") : "";
  373. next_page++;
  374. if (default_type == 1)
  375. {
  376. var hmin = MINIMUM_HITS+1;
  377. for (j = 0; j < found_key_list.length; j++)
  378. {
  379. if (history[found_key_list[j]].hits < hmin)
  380. {
  381. next_URL = "";
  382. next_page = -1;
  383. break;
  384. }
  385. }
  386. }
  387. else if (next_page > PAGES_TO_SCRAPE && correct_for_skips)
  388. {
  389. var skipped_hits = 0;
  390. var added_pages = 0;
  391. for (j = 0; j < found_key_list.length; j++)
  392. {
  393. var obj = history[found_key_list[j]];
  394. if (! ignore_check(obj.requester,obj.title))
  395. skipped_hits++;
  396. }
  397. added_pages = Math.floor(skipped_hits/10);
  398. if (skipped_hits%10 >6)
  399. added_pages++;
  400. if (next_page > PAGES_TO_SCRAPE + added_pages)
  401. {
  402. next_URL = "";
  403. next_page = -1;
  404. }
  405. }
  406. else if (next_page > PAGES_TO_SCRAPE)
  407. {
  408. next_URL = "";
  409. next_page = -1;
  410. }
  411. setTimeout(function(){statusdetail_loop(next_URL);}, STATUSDETAIL_DELAY);
  412. }
  413. else
  414. {
  415. console.log("MPRE");
  416. setTimeout(function(){statusdetail_loop(next_URL);}, MPRE_DELAY);
  417. }
  418. });
  419. }
  420. else
  421. {
  422. searched_once = true;
  423. var found_hits = found_key_list.length;
  424. var shown_hits = 0;
  425. var new_hits = 0;
  426. var url = API_MULTI_ATTRS_URL;
  427. var rids = [];
  428. var lastRow = text_area.rows.length - 1;
  429. for (i = lastRow; i>0; i--)
  430. text_area.deleteRow(i);
  431. for (j = 0; j < found_key_list.length; j++)
  432. {
  433. //(function(url,rids,j) {
  434. var obj = history[found_key_list[j]];
  435. if (ignore_check(obj.requester,obj.title) && obj.found_this_time){
  436. ++shown_hits;
  437. //console.log(obj);
  438. //hit export will update col_heads[1]
  439. var col_heads = ["<a href='"+ LINK_BASE+obj.req_link +"' target='_blank'>" + obj.requester + "</a>","<a href='"+ LINK_BASE+obj.prev_link +"' target='_blank' title='"+ obj.description +"'>" + obj.title + "</a>",obj.reward,obj.hits,"TO down","<a href='"+ LINK_BASE+obj.acc_link +"' target='_blank'>Accept</a>"];
  440. var row = text_area.insertRow(text_area.rows.length);
  441. url += obj.rid + ',';
  442. rids.push(obj.rid);
  443. if (check_hitDB)
  444. {
  445. col_heads.push("R");
  446. col_heads.push("T");
  447. }
  448. if (!obj.qualified)
  449. {
  450. col_heads.push("Not Qualified");
  451. }
  452. for (i=0; i<col_heads.length; i++)
  453. {
  454. var this_cell = row.insertCell(i);
  455. row.cells[i].style.fontSize = default_text_size;
  456. this_cell.innerHTML = col_heads[i];
  457. if(i>1)
  458. this_cell.style.textAlign = 'center';
  459. if (check_hitDB)
  460. {
  461. if (i==6)
  462. {
  463. if (obj.reqdb)
  464. this_cell.style.backgroundColor = GREEN;
  465. else
  466. this_cell.style.backgroundColor = RED;
  467. }
  468. else if (i==7)
  469. {
  470. if (obj.titledb)
  471. this_cell.style.backgroundColor = GREEN;
  472. else
  473. this_cell.style.backgroundColor = RED;
  474. }
  475. else if (i==8)
  476. this_cell.style.backgroundColor = DARKGREY;
  477. }
  478. else if (i==6)
  479. this_cell.style.backgroundColor = DARKGREY;
  480. }
  481. if (Object.keys(history).length>0)
  482. {
  483. if (obj.new_result < 1000*save_new_results_time)
  484. {
  485. new_hits++;
  486. for (i in col_heads)
  487. {
  488. row.cells[i].style.fontSize = default_text_size + 1;
  489. row.cells[i].style.fontWeight = "bold";
  490. }
  491. }
  492. }
  493. button = document.createElement('button'); //HIT SCRAPER ADDITION
  494. button.textContent = 'vB';
  495. button.title = 'Export this HIT description as vBulletin formatted text';
  496. button.style.height = '14px';
  497. button.style.width = '30px';
  498. button.style.fontSize = '8px';
  499. button.style.border = '1px solid';
  500. button.style.padding = '0px';
  501. button.style.backgroundColor = 'transparent';
  502. //button.addEventListener("click", function() {export_func_deleg(j);}.bind(null,j), false);
  503. button.addEventListener("click", (function (obj,j) { return function() {export_func_deleg(obj,j);}})(obj,j));
  504. row.cells[1].appendChild(button);
  505. }
  506. //});
  507. }
  508. set_progress_report("Scrape complete. " + shown_hits + " HITs found (" + new_hits + " new results). " + (found_hits - shown_hits) + " HITs ignored.", false);
  509. url = url.substring(0,url.length - 1);
  510. //console.log(url);
  511. var success_flag = false;
  512. GM_xmlhttpRequest(
  513. {
  514. method: "GET",
  515. url: url,
  516. onload: function (results)
  517. {
  518. //console.log(results.responseText);
  519. rdata = $.parseJSON(results.responseText);
  520. for (i = 0; i < rids.length; i++)
  521. {
  522. text_area.rows[i+1].style.backgroundColor = GREY;
  523. if (rdata[rids[i]])
  524. {
  525. var pay = rdata[rids[i]].attrs.pay
  526. var reviews = rdata[rids[i]].reviews
  527. var average = 0;
  528. var sum = 0;
  529. var divisor = 0;
  530. var comm = rdata[rids[i]].attrs.comm;
  531. var fair = rdata[rids[i]].attrs.fair;
  532. var fast = rdata[rids[i]].attrs.fast;
  533. if (comm > 0)
  534. {
  535. sum += COMM_WEIGHT*comm;
  536. divisor += COMM_WEIGHT;
  537. }
  538. if (pay > 0)
  539. {
  540. sum += PAY_WEIGHT*pay;
  541. divisor += PAY_WEIGHT;
  542. }
  543. if (fair > 0)
  544. {
  545. sum += FAIR_WEIGHT*fair;
  546. divisor += FAIR_WEIGHT;
  547. }
  548. if (fast > 0)
  549. {
  550. sum += FAST_WEIGHT*fast;
  551. divisor += FAST_WEIGHT;
  552. }
  553. if (divisor > 0)
  554. {
  555. average = sum/divisor;
  556. }
  557. text_area.rows[i+1].cells[4].innerHTML = "<a href='"+ TO_REQ_URL+rids[i] +"' target='_blank'>" + pay + "</a>";
  558. if (reviews > 4)
  559. {
  560. if (average > 4.49)
  561. text_area.rows[i+1].style.backgroundColor = GREEN;
  562. else if (average > 3.49)
  563. text_area.rows[i+1].style.backgroundColor = LIGHTGREEN;
  564. //else if (average > 2.99)
  565. // text_area.rows[i+1].style.backgroundColor = YELLOW;
  566. else if (average > 1.99)
  567. text_area.rows[i+1].style.backgroundColor = ORANGE;
  568. else if (average > 0)
  569. text_area.rows[i+1].style.backgroundColor = RED;
  570. }
  571. }
  572. else
  573. {
  574. text_area.rows[i+1].cells[4].innerHTML = "No data";
  575. }
  576. }
  577. success_flag = true;
  578. }
  579. });
  580. if (!success_flag)
  581. for (i = 0; i < rids.length; i++) text_area.rows[i+1].style.backgroundColor = GREY;
  582. statusdetail_loop_finished = true;
  583. if (SEARCH_REFRESH>0)
  584. {
  585. wait_loop = setTimeout(function(){if (global_run) start_it();}, 1000*SEARCH_REFRESH);
  586. display_wait_time(SEARCH_REFRESH);
  587. }
  588. else
  589. {
  590. global_run = false;
  591. big_red_button.textContent = "Start";
  592. }
  593. }
  594. }
  595. }
  596.  
  597. function ignore_check(r,t){
  598. return -1 == ignore_list.map(function(item) { return item.toLowerCase(); }).indexOf(r.toLowerCase());
  599. }
  600.  
  601. function start_running()
  602. {
  603. if (big_red_button.textContent == "Start")
  604. {
  605. global_run = true;
  606. initial_url = URL_BASE;
  607. if (search_input.value.length>0)
  608. {
  609. initial_url = initial_url.replace("searchWords=", "searchWords=" + search_input.value);
  610. }
  611. if (time_input.value.replace(/[^0-9]+/g,"") != "")
  612. {
  613. SEARCH_REFRESH = Number(time_input.value);
  614. }
  615. if (page_input.value.replace(/[^0-9]+/g,"") != "")
  616. {
  617. PAGES_TO_SCRAPE = Number(page_input.value);
  618. }
  619. if (min_input.value.replace(/[^0-9]+/g,"") != "")
  620. {
  621. MINIMUM_HITS = Number(min_input.value);
  622. }
  623. if (new_time_display_input.value.replace(/[^0-9]+/g,"") != "")
  624. {
  625. save_new_results_time = Number(new_time_display_input.value);
  626. }
  627. if (reward_input.value.replace(/[^0-9]+/g,"") != "")
  628. {
  629. initial_url += "&minReward=" + reward_input.value;
  630. }
  631. else
  632. {
  633. initial_url += "&minReward=0.00";
  634. }
  635. if (qual_input.checked)
  636. {
  637. initial_url += "&qualifiedFor=on"
  638. }
  639. else
  640. {
  641. initial_url += "&qualifiedFor=off"
  642. }
  643. if (masters_input.checked)
  644. {
  645. initial_url += "&requiresMasterQual=on"
  646. }
  647. if (sort_input1.checked)
  648. {
  649. initial_url+= "&sortType=LastUpdatedTime%3A1";
  650. default_type = 0;
  651. }
  652. else if (sort_input2.checked)
  653. {
  654. initial_url+= "&sortType=NumHITs%3A1";
  655. default_type = 1;
  656. }
  657. else if (sort_input3.checked)
  658. {
  659. initial_url+= "&sortType=Reward%3A1";
  660. default_type = 0;
  661. }
  662. initial_url+="&pageNumber=1&searchSpec=HITGroupSearch"
  663. start_it();
  664. }
  665. else
  666. {
  667. global_run = false;
  668. clearTimeout(wait_loop);
  669. big_red_button.textContent = "Start";
  670. set_progress_report("Stopped", true);
  671. }
  672. }
  673.  
  674. function start_it()
  675. {
  676. statusdetail_loop_finished = false;
  677. big_red_button.textContent = "Stop";
  678. found_key_list=[];
  679. var ctime = new Date().getTime()
  680. if (ctime - last_clear_time > save_results_time*666)
  681. {
  682. var last_history=history;
  683. history = {};
  684. for (var key in last_history)
  685. {
  686. if (last_history[key].new_result<save_results_time*1000)
  687. {
  688. history[key]=last_history[key];
  689. if (last_history[key].found_this_time)
  690. {
  691. last_history[key].found_this_time = false;
  692. if (last_history[key].new_result>save_new_results_time*1000)
  693. last_history[key].initial_time = ctime-1000*save_new_results_time;
  694. }
  695. }
  696.  
  697. }
  698. last_clear_time = ctime;
  699. }
  700. next_page = 1;
  701. statusdetail_loop(initial_url);
  702. }
  703.  
  704.  
  705. function show_interface()
  706. {
  707. control_panel.style.color = BROWN;
  708. control_panel.style.fontSize = 14;
  709. control_panel.removeChild(big_red_button);
  710. control_panel.appendChild(document.createTextNode("Auto-refresh delay: "));
  711. time_input.onkeydown = function(event){if (event.keyCode == 13){start_running();}};
  712. time_input.title = "Enter search refresh delay in seconds\n" + "Enter 0 for no auto-refresh\n" + "Default is 0 (no auto-refresh)";
  713. time_input.size = 3;
  714. control_panel.appendChild(time_input);
  715. control_panel.appendChild(document.createTextNode(" "));
  716. control_panel.appendChild(document.createTextNode("Pages to scrape: "));
  717. page_input.onkeydown = function(event){if (event.keyCode == 13){start_running();}};
  718. page_input.title = "Enter number of pages to scrape\n" + "Default is 4";
  719. page_input.size = 3;
  720. control_panel.appendChild(page_input);
  721. control_panel.appendChild(document.createTextNode(" "));
  722. control_panel.appendChild(document.createTextNode("Minimum batch size: "));
  723. min_input.onkeydown = function(event){if (event.keyCode == 13){start_running();}};
  724. min_input.title = "Enter minimum HITs for batch search\n" + "Default is 100";
  725. min_input.size = 3;
  726. control_panel.appendChild(min_input);
  727. control_panel.appendChild(document.createTextNode(" "));
  728. control_panel.appendChild(document.createTextNode("New HIT highlighting: "));
  729. new_time_display_input.onkeydown = function(event){if (event.keyCode == 13){start_running();}};
  730. new_time_display_input.title = "Enter time (in seconds) to keep new HITs highlighted\n" + "Default is 300 (5 minutes)";
  731. new_time_display_input.size = 6;
  732. control_panel.appendChild(new_time_display_input);
  733. control_panel.appendChild(document.createElement("P"));
  734. control_panel.appendChild(document.createTextNode("Minimum reward: "));
  735. reward_input.size = 6;
  736. control_panel.appendChild(reward_input);
  737. control_panel.appendChild(document.createTextNode(" "));
  738.  
  739. control_panel.appendChild(document.createTextNode("Qualified"));
  740. control_panel.appendChild(qual_input);
  741. control_panel.appendChild(document.createTextNode(" "));
  742. control_panel.appendChild(document.createTextNode("Masters"));
  743. control_panel.appendChild(masters_input);
  744. control_panel.appendChild(document.createTextNode(" "));
  745. control_panel.appendChild(document.createTextNode("Sort types: "));
  746. control_panel.appendChild(sort_input1);
  747. control_panel.appendChild(document.createTextNode("Latest"));
  748. control_panel.appendChild(sort_input2);
  749. control_panel.appendChild(document.createTextNode("Most Available"));
  750. control_panel.appendChild(sort_input3);
  751. control_panel.appendChild(document.createTextNode("Amount"));
  752. control_panel.appendChild(document.createElement("P"));
  753. control_panel.appendChild(search_input);
  754. search_input.size = 20;
  755. search_input.title = "Enter a search term to include\n" + "Default is blank (no included terms)";
  756. search_input.placeholder="Enter search terms here";
  757. control_panel.appendChild(document.createTextNode(" "));
  758. big_red_button.textContent = "Start";
  759. big_red_button.onclick = function(){start_running();};
  760. control_panel.appendChild(big_red_button);
  761. control_panel.appendChild(document.createTextNode(" "));
  762. control_panel.appendChild(progress_report);
  763. control_panel.appendChild(document.createElement("P"));
  764. text_area.style.fontWeight = 400;
  765. text_area.createCaption().innerHTML = "HITs";
  766. var col_heads = ['Requester','Title','Reward','HITs Available','TO pay',"Accept HIT"];
  767. var row = text_area.createTHead().insertRow(0);
  768. text_area.caption.style.fontWeight = 800;
  769. text_area.caption.style.color = BROWN;
  770. if (default_text_size > 10)
  771. text_area.cellPadding=Math.min(Math.max(1,Math.floor((default_text_size-10)/2)),5);
  772. //console.log(text_area.cellPadding);
  773. //text_area.cellPadding=2;
  774. text_area.caption.style.fontSize = 28;
  775. text_area.rows[0].style.fontWeight = 800;
  776. text_area.rows[0].style.color = BROWN;
  777. for (i=0; i<col_heads.length; i++)
  778. {
  779. var this_cell = row.insertCell(i);
  780. this_cell.innerHTML = col_heads[i];
  781. this_cell.style.fontSize = 14;
  782. if (i > 1)
  783. this_cell.style.textAlign = 'center';
  784. }
  785. control_panel.appendChild(text_area);
  786. }
  787.  
  788.  
  789. /********HIT EXPORT ADDITIONS*****/
  790.  
  791. var EDIT = false;
  792. var HIT;
  793.  
  794. var TO_BASE = "http://turkopticon.ucsd.edu/";
  795. var API_BASE = "https://api.turkopticon.istrack.in/";
  796. var API_URL = API_BASE + "multi-attrs.php?ids=";
  797. DEFAULT_TEMPLATE = '[table][tr][td][b]Title:[/b] [url={prev_link}][COLOR=blue]{title}[/COLOR][/url]\n';
  798. DEFAULT_TEMPLATE += '[b]Requester:[/b] [url=https://www.mturk.com/mturk/searchbar?selectedSearchType=hitgroups&requesterId={rid}][COLOR=blue]{requester}[/COLOR][/url]';
  799. DEFAULT_TEMPLATE += ' [{rid}] ([url='+TO_BASE+'{rid}][COLOR=blue]TO[/COLOR][/url])';
  800. DEFAULT_TEMPLATE += '\n[b]TO Ratings:[/b]{to_stuff}';
  801. DEFAULT_TEMPLATE += '\n[b]Description:[/b] {description}';
  802. DEFAULT_TEMPLATE += '\n[b]Time:[/b] {time}';
  803. DEFAULT_TEMPLATE += '\n[b]Reward:[/b] [COLOR=green][b]{reward}[/b][/COLOR]';
  804. DEFAULT_TEMPLATE += '\n[b]Qualifications:[/b] {quals}[/td][/tr][/table]';
  805.  
  806. var TEMPLATE;
  807. var EASYLINK;
  808.  
  809. if (typeof GM_getValue === 'undefined')
  810. TEMPLATE = null;
  811. else {
  812. TEMPLATE = GM_getValue('HITScraper Template');
  813. EASYLINK = GM_getValue('HITScraper Easylink');
  814. }
  815. if (TEMPLATE == null) {
  816. TEMPLATE = DEFAULT_TEMPLATE;
  817. }
  818.  
  819. function buildXhrUrl(rai) {
  820. var url = API_URL;
  821. var ri = rai;
  822. url += rai;
  823. return url;
  824. }
  825.  
  826. function makeXhrQuery(url) {
  827. var xhr = new XMLHttpRequest();
  828. try{
  829. xhr.open('GET', url, false);
  830. xhr.send(null);
  831. return $.parseJSON(xhr.response);
  832. }
  833. catch(err){
  834. return "TO DOWN";
  835. }
  836. }
  837.  
  838. function getNamesForEmptyResponses(rai, resp) {
  839. for (var rid in rai) {
  840. if (rai.hasOwnProperty(rid) && resp[rid] == "") {
  841. resp[rid] = $.parseJSON('{"name": "' + rai[rid][0].innerHTML + '"}');
  842. }
  843. }
  844. return resp;
  845. }
  846.  
  847. function getKeys(obj) {
  848. var keys = [];
  849. for (var key in obj) {
  850. keys.push(key);
  851. }
  852. return keys;
  853. }
  854.  
  855. function export_func_deleg(item,index) {
  856. //console.log(item);
  857. export_func(item);
  858. }
  859.  
  860. function export_func(item) {
  861. HIT = item;
  862. edit_button.textContent = 'Edit Template';
  863. apply_template(item);
  864. div.style.display = 'block';
  865. textarea.select();
  866. }
  867.  
  868. function apply_template(hit_data) {
  869. var txt = TEMPLATE;
  870.  
  871. var vars = ['title', 'requester', 'rid', 'description', 'reward', 'quals', 'prev_link', 'time', 'hits', 'to_stuff', 'to_text'];
  872.  
  873. var resp = null;
  874. if (txt.indexOf('{to_text}') >= 0 || txt.indexOf('{to_stuff}') >= 0){
  875. var url = buildXhrUrl(hit_data["rid"]);
  876. resp = makeXhrQuery(url);
  877. //console.log(resp);
  878. }
  879. var toText = "";
  880. var toStuff = "";
  881. var toData = "";
  882. var numResp = (resp == null || resp == "TO DOWN" ? "n/a" : resp[hit_data["rid"]].reviews);
  883. if (resp == "TO DOWN"){
  884. toStuff = " [URL=\""+TO_BASE+hit_data['rid']+"\"]TO down.[/URL]";
  885. toText = toStuff;
  886. }
  887. else if (resp == null || resp[hit_data["rid"]].attrs == null && resp != "TO DOWN") {
  888. toStuff = " No TO ";
  889. toText = " No TO ";
  890. toStuff += "[URL=\""+TO_BASE+"report?requester[amzn_id]=" + hit_data['rid'] + "&requester[amzn_name]=" + hit_data['requester'] + "\"]";
  891. toStuff += "(Submit a new TO rating for this requester)[/URL]";
  892. }
  893. else {
  894. for (var key in resp[hit_data["rid"]].attrs) {
  895. //toText += "\n[*]"+key+": "+resp[hit_data["requesterId"]].attrs[key]+"\n";
  896. var i = 0;
  897. var color = "green";
  898. var name = key;
  899. var num = Math.floor(resp[hit_data["rid"]].attrs[key]);
  900. switch (key){
  901. case "comm":
  902. name = "Communicativity";
  903. break;
  904. case "pay":
  905. name = "Generosity";
  906. break;
  907. case "fast":
  908. name = "Promptness";
  909. break;
  910. case "fair":
  911. name = "Fairness";
  912. break;
  913. default:
  914. name = key;
  915. break;
  916. }
  917. switch (num){
  918. case 0:
  919. color = "red";
  920. break;
  921. case 1:
  922. color = "red";
  923. break;
  924. case 2:
  925. color = "orange";
  926. break;
  927. case 3:
  928. color = "yellow";
  929. break;
  930. default:
  931. break;
  932. }
  933. toText += (num > 0 ? "\n[color="+color+"]" : "\n");
  934. for (i; i < num; i++){
  935. toText += "[b]☢[/b]"
  936. }
  937. toText += (num > 0 ? "[/color]" : "")
  938. if (i < 5){
  939. toText += "[color=white]";
  940. for (i; i < 5; i++)
  941. toText += "[b]☢[/b]";
  942. toText += "[/color]";
  943. }
  944. toText += " "+Number(resp[hit_data["rid"]].attrs[key]).toFixed(2)+" "+name;
  945. toData += Number(resp[hit_data["rid"]].attrs[key]).toFixed(2) + ",";
  946. }
  947. //toText += "[/list]";
  948. toText += (txt.indexOf('{to_stuff}') >= 0 ? "" : "\nNumber of Reviews: "+numResp+"\n[URL=\""+TO_BASE+"report?requester[amzn_id]=" + hit_data['rid'] + "&requester[amzn_name]=" + hit_data['requester'] + "\"](Submit a new TO rating for this requester)[/URL]");
  949. toStuff = '\n[img]http://data.istrack.in/to/' + toData.slice(0,-1) + '.png[/img]';
  950. toStuff += (txt.indexOf('{to_stuff}') >= 0 ? (txt.indexOf('{to_text}') >= 0 ? "" : toText) : "");
  951. toStuff += "\nNumber of Reviews: "+numResp;
  952. toStuff += "[URL=\""+TO_BASE+"report?requester[amzn_id]=" + hit_data['rid'] + "&requester[amzn_name]=" + hit_data['requester'] + "\"]";
  953. toStuff += "\n(Submit a new TO rating for this requester)[/URL]";
  954. }
  955. for (var i = 0; i < vars.length; i++) {
  956. t = new RegExp('\{' + vars[i] + '\}', 'g');
  957. if (vars[i] == "to_stuff") {
  958. txt = txt.replace(t, toStuff);
  959. }
  960. else if (vars[i] == "to_text"){
  961. txt = txt.replace(t, toText);
  962. }
  963. else if (vars[i] == "prev_link"){
  964. txt = txt.replace(t,"https://www.mturk.com"+hit_data[vars[i]]);
  965. }
  966. else if (vars[i] == "acc_link"){
  967. txt = txt.replace(t,"https://www.mturk.com"+hit_data[vars[i]]);
  968. }
  969. else
  970. txt = txt.replace(t, hit_data[vars[i]]);
  971. }
  972. textarea.value = txt;
  973. }
  974.  
  975. function hide_func(div) {
  976. if (EDIT == false)
  977. div.style.display = 'none';
  978. }
  979.  
  980. function edit_func() {
  981. if (EDIT == true) {
  982. EDIT = false;
  983. TEMPLATE = textarea.value;
  984. edit_button.textContent = 'Edit Template';
  985. apply_template(HIT);
  986. }
  987. else {
  988. console.log("Editing");
  989. EDIT = true;
  990. edit_button.textContent = 'Show Changes';
  991. save_button.disabled = false;
  992. textarea.value = TEMPLATE;
  993. }
  994. }
  995.  
  996. function default_func() {
  997. GM_deleteValue('HITScraper Template');
  998. TEMPLATE = DEFAULT_TEMPLATE;
  999. EDIT = false;
  1000. edit_button.textContent = 'Edit Template';
  1001. apply_template(HIT);
  1002. }
  1003.  
  1004. function save_func() {
  1005. if (EDIT)
  1006. TEMPLATE = textarea.value;
  1007. GM_setValue('HITScraper Template', TEMPLATE);
  1008. }
  1009.  
  1010. var div = document.createElement('div');
  1011. var textarea = document.createElement('textarea');
  1012. var div2 = document.createElement('label');
  1013.  
  1014. div.style.position = 'fixed';
  1015. div.style.width = '500px';
  1016. div.style.height = '235px';
  1017. div.style.left = '50%';
  1018. div.style.right = '50%';
  1019. div.style.margin = '-250px 0px 0px -250px';
  1020. div.style.top = '300px';
  1021. div.style.padding = '5px';
  1022. div.style.border = '2px';
  1023. div.style.backgroundColor = 'black';
  1024. div.style.color = 'white';
  1025. div.style.zIndex = '100';
  1026.  
  1027. textarea.style.padding = '2px';
  1028. textarea.style.width = '500px';
  1029. textarea.style.height = '200px';
  1030. textarea.title = '{title}\n{requester}\n{rid}\n{description}\n{reward}\n{quals}\n{prev_link}\n{time}\n{hit}\n{to_stuff}\n{to_text}';
  1031.  
  1032. div.textContent = 'Press Ctrl+C to copy to clipboard. Click textarea to close';
  1033. div.style.fontSize = '12px';
  1034. div.appendChild(textarea);
  1035.  
  1036. var edit_button = document.createElement('button');
  1037. var save_button = document.createElement('button');
  1038. var default_button = document.createElement('button');
  1039. var easy_button = document.createElement('button');
  1040.  
  1041. edit_button.textContent = 'Edit Template';
  1042. edit_button.setAttribute('id', 'edit_button');
  1043. edit_button.style.height = '18px';
  1044. edit_button.style.width = '100px';
  1045. edit_button.style.fontSize = '10px';
  1046. edit_button.style.paddingLeft = '3px';
  1047. edit_button.style.paddingRight = '3px';
  1048. edit_button.style.backgroundColor = 'white';
  1049.  
  1050. save_button.textContent = 'Save Template';
  1051. save_button.setAttribute('id', 'save_button');
  1052. save_button.style.height = '18px';
  1053. save_button.style.width = '100px';
  1054. save_button.style.fontSize = '10px';
  1055. save_button.style.paddingLeft = '3px';
  1056. save_button.style.paddingRight = '3px';
  1057. save_button.style.backgroundColor = 'white';
  1058. save_button.style.marginLeft = '5px';
  1059.  
  1060. easy_button.textContent = 'Change Adfly Url';
  1061. easy_button.setAttribute('id', 'easy_button');
  1062. easy_button.style.height = '18px';
  1063. easy_button.style.width = '100px';
  1064. easy_button.style.fontSize = '10px';
  1065. easy_button.style.paddingLeft = '3px';
  1066. default_button.textContent = ' D ';
  1067. default_button.setAttribute('id', 'default_button');
  1068. default_button.style.height = '18px';
  1069. default_button.style.width = '20px';
  1070. default_button.style.fontSize = '10px';
  1071. default_button.style.paddingLeft = '3px';
  1072. default_button.style.paddingRight = '3px';
  1073. default_button.style.backgroundColor = 'white';
  1074. default_button.style.marginLeft = '5px';
  1075. default_button.title = 'Return default template';
  1076. div.appendChild(edit_button);
  1077. div.appendChild(save_button);
  1078. div.appendChild(default_button);
  1079. div.appendChild(easy_button);
  1080. save_button.disabled = true;
  1081.  
  1082. div.style.display = 'none';
  1083. textarea.addEventListener("click", function() {hide_func(div);}, false);
  1084. edit_button.addEventListener("click", function() {edit_func();}, false);
  1085. save_button.addEventListener("click", function() {save_func();}, false);
  1086. default_button.addEventListener("click", function() {default_func();}, false);
  1087. document.body.insertBefore(div, document.body.firstChild);