Advertisement
cookmeplox

Untitled

Mar 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. // USAGE INSTRUCTIONS:
  2. // Go to https://apps.runescape.com/runemetrics/app/pvm-kills (logging in, if needed)
  3. // Open up the JavaScript console in your browser
  4. // Paste this code into the console and press enter
  5. // Please report any errors or bugs to Cook Me Plox
  6.  
  7. // change currentBatch to be in [0 ... totalBatches-1]
  8. var currentBatch = 0;
  9. var totalBatches = 1;
  10.  
  11. // start and end times in Unix millis
  12. var start = 1420294400000;
  13. var end = 1620899199999;
  14.  
  15. // get your username
  16. var avatar = $("#a-header-avatar").attr("alt");
  17. var username = avatar.substr(0, avatar.length - 7);
  18.  
  19. var versionID = "1.0/custom";
  20. // whether to skip monsters we think drop nothing
  21. var skip = true;
  22. // whether to batch scraping into separate runs, if you time out frequently
  23. var useBatch = true;
  24.  
  25. var sessionEnded = false;
  26.  
  27. var metadata = JSON.stringify({"runID": Math.random(), "versionID": versionID, "skip": skip, "currentBatch": currentBatch, "totalBatches": totalBatches});
  28.  
  29. // these monster IDs (we think!) don't drop anything, so we can skip them
  30. var missing = "1329".split(" ");
  31.  
  32. var totalMonsters = undefined;
  33. var visitedMonsters = 0;
  34.  
  35. function postData(url, data, username) {
  36. // this is my server's URL. It only posts data from getDrops and getKills.
  37. var postUrl = "https://cookmeplox.pythonanywhere.com/runemetricslogger/";
  38. $.post(postUrl, data + "&&&" + username + "&&&" + url + "^^^" + metadata);
  39. }
  40.  
  41. // get data from RuneMetrics, retrying on failure, and stopping if session ends.
  42. function safeGet(url, callback) {
  43. if (sessionEnded) {
  44. return;
  45. }
  46. $.ajax(url, {
  47. success: callback,
  48. error: function(xhr) {
  49. if (sessionEnded) {
  50. return;
  51. }
  52. if (xhr.status === 0) {
  53. console.error("Uh oh! We think your session ended prematurely. Try refreshing the page and running again. If this persists, contact Cook Me Plox.");
  54. sessionEnded = true;
  55. } else {
  56. if (this.retries > 0) {
  57. console.log("Failed to GET url " + url + " -- retrying (" + this.retries + " left)");
  58. this.retries--;
  59. $.ajax(this);
  60. return;
  61. } else {
  62. console.error("Failed to GET url " + url + " -- retries exhausted.");
  63. }
  64. }
  65. },
  66. retries: 3
  67. });
  68. }
  69.  
  70. function getDrops(start, end, id) {
  71. var url = "https://apps.runescape.com/runemetrics/aggregations/npc/drop-log/range/day?&start=" + start + "&end=" + end + "&id=" + id;
  72. safeGet(url, function(data) {
  73. visitedMonsters++;
  74. console.log("Got drops for monster #" + id + "; " + visitedMonsters + "/" + totalMonsters + " done");
  75. postData(url, JSON.stringify(data), username);
  76. });
  77. }
  78.  
  79. function getKills(start, end) {
  80. metadata.runID = Math.random();
  81. visitedMonsters = 0;
  82. var url = "https://apps.runescape.com/runemetrics/aggregations/npc/kill/range/day?&start=" + start + "&end=" + end;
  83. safeGet(url, function(data) {
  84. console.log("Got kills");
  85. postData(url, JSON.stringify(data), username);
  86. var kills = data;
  87. var aggKills = {};
  88. kills.monsterKills.forEach(function(day) {
  89. Object.keys(day).forEach(function(hourKey) {
  90. if (hourKey == "day") {
  91. return;
  92. }
  93. Object.keys(day[hourKey]).forEach(function(idKey) {
  94. var thisKills = day[hourKey][idKey].kills;
  95. aggKills[idKey] = aggKills.hasOwnProperty(idKey) ? (aggKills[idKey] + thisKills) : thisKills;
  96. });
  97. });
  98. });
  99.  
  100. var flatKills = [];
  101. Object.keys(aggKills).forEach(function(key) {
  102. flatKills.push([key, aggKills[key]]);
  103. });
  104.  
  105. flatKills.sort(function(a, b) {
  106. return b[1] - a[1];
  107. });
  108. if (skip) {
  109. flatKills = flatKills.filter(function(key) {
  110. return missing.indexOf(key[0]) !== -1;
  111. });
  112. }
  113. if (useBatch) {
  114. flatKills = flatKills.filter(function(key) {
  115. return parseInt(key[0]) % totalBatches === currentBatch;
  116. });
  117. }
  118. totalMonsters = flatKills.length;
  119. flatKills.forEach(function(elt, i) {
  120. var id = elt[0];
  121. getDrops(start, end, id);
  122. });
  123. });
  124. }
  125.  
  126. getKills(start, end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement