Advertisement
TheBat

GE

Mar 23rd, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.net.URL;
  4.  
  5.  
  6. public class GE {
  7. /**
  8. * Find item price on the grand exchange
  9. * @param itemName The name of the item
  10. * @return Price of the item
  11. */
  12. public static int getGuidePrice(final String itemName) {
  13. return Integer.parseInt(lookup(itemName)[1]);
  14. }
  15.  
  16. /**
  17. * Find item price on the grand exchange
  18. * @param itemID The id of the item.
  19. * @return Price of the item
  20. */
  21. public static int getGuidePrice(final int itemID) {
  22. return Integer.parseInt(lookup(itemID)[1]);
  23. }
  24.  
  25. /**
  26. * Get the ID of itemname (used for injecton bots)
  27. * @param itemName: name of the item
  28. * @return : item id as an integer
  29. */
  30. public int getID(final String itemName) {
  31. return Integer.parseInt(lookup(itemName)[2]);
  32. }
  33.  
  34. /**
  35. * Gets the itemname given the itemID
  36. * @param itemID : id of the item
  37. * @return name of the item
  38. */
  39. public int getName(final int itemID) {
  40. return Integer.parseInt(lookup(itemID)[0]);
  41. }
  42.  
  43. /**
  44. * Looks up grand exchange information and returns a string array with the following contents
  45. * String[0] = item name
  46. * String[1] = item price
  47. * String[2] = item id
  48. * @param itemName: name of the item to grab information about on the grandexchange website
  49. * @return : a string array of grand exchange information on the item id provided
  50. */
  51. public static String[] lookup(final String itemName) {
  52. try {
  53. final URL url = new URL("http://services.runescape.com/m=itemdb_rs/results.ws?query=" + itemName);
  54. final BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
  55. String input;
  56. while ((input = br.readLine()) != null) {
  57. if(input.toLowerCase().contains("alt=\"" + itemName.toLowerCase().trim() + "\"")) {
  58. return lookup(Integer.parseInt(input.substring(input.indexOf("id=") + 3, input.lastIndexOf("\" alt"))));
  59. }
  60. }
  61. } catch (final Exception ignored) { }
  62. return null;
  63. }
  64.  
  65.  
  66. /**
  67. * Looks up grand exchange information and returns a string array with the following contents
  68. * String[0] = item name
  69. * String[1] = item price
  70. * String[2] = item id
  71. * @param itemID for the item being looked up on the grand exchange
  72. * @return : a string array of grand exchange information on the item id provided
  73. */
  74. public static String[] lookup(final int itemID) {
  75. try {
  76. String[] info = {"0", "0", "0", "0"};
  77. final URL url = new URL("http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=" + itemID);
  78. final BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
  79. String input;
  80. while ((input = br.readLine()) != null) {
  81. if(input.startsWith("<meta name=\"keywords")) {
  82. info[0] = input.substring(input.lastIndexOf(",") + 1, input.lastIndexOf("\"")).trim();
  83. if(info[0].equals("java")) return null;
  84. }
  85. if(input.contains("Current guide price:")) {
  86. input = br.readLine();
  87. info[1] = formatter(input.substring(4, input.lastIndexOf('<')));
  88. info[2] = ("" + itemID);
  89. return info;
  90. }
  91. }
  92. } catch (final Exception ignored) {}
  93. return null;
  94. }
  95.  
  96. /**
  97. * Formats a string removing all abbreviations and commas out of it and returning just the raw integer value as a string
  98. * @param num : string number to be formatted. Used for numbers like 3.2m or 20.6k
  99. * @return : a complete number without any abbreviations in it.
  100. */
  101. public static String formatter(String num) {
  102. try {
  103. return num.replaceAll("\\.","").replaceAll("m", "00000").replaceAll("k", "00").replaceAll(",", "");
  104. } catch (Exception e) {}
  105. return "0";
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement