HIT Scraper WITH EXPORT

Snag HITs.

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

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