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