Advertisement
Tezlaz

prices

Aug 6th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. package scripts.tools;
  2.  
  3. import org.tribot.api.General;
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.util.HashMap;
  9.  
  10. public class Prices {
  11. public final static String[] userAgents = new String[] {
  12. "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36",
  13. "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
  14. "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/533.1 (KHTML, like Gecko) Maxthon/3.0.8.2 Safari/533.1",
  15. "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko",
  16. "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1",
  17. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246",
  18. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A" };
  19.  
  20. public static int getOSbuddyPrice(int itemID) {
  21. return handleResult("https://api.rsbuddy.com/grandExchange?a=guidePrice&i=", itemID, "overall");
  22. }
  23.  
  24. public static int getGEPrice(int itemID) {
  25. return handleResult("http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=", itemID,
  26. "price");
  27. }
  28.  
  29. private static int handleResult(String url, int itemID, String key) {
  30. String queryResult = httpRequest(url + itemID);
  31. if (queryResult != null) {
  32. HashMap<String, String> results = JSONToMap(queryResult);
  33. if (results.containsKey(key)) {
  34. return Integer.parseInt(results.get(key));
  35. }
  36. }
  37. return -1;
  38. }
  39.  
  40. private static String httpRequest(final String url) {
  41. try {
  42. String content = "";
  43. String line;
  44. URLConnection con = new URL(url).openConnection();
  45. con.setConnectTimeout(10000);
  46. con.setReadTimeout(10000);
  47. con.setRequestProperty("User-Agent", userAgents[General.random(0, userAgents.length - 1)]);
  48. BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  49. while ((line = in.readLine()) != null) {
  50. content = content + line;
  51. }
  52. in.close();
  53. return (content.isEmpty()) ? null : content;
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. return null;
  58. }
  59.  
  60. private static HashMap<String, String> JSONToMap(String json) {
  61. HashMap<String, String> map = new HashMap<>();
  62. String[] parts = json.replace("{", "").replace("}", "").replaceAll("\"", "").split(",");
  63. for (String part : parts) {
  64. String[] pieces = part.split(":");
  65. if (map.containsKey(pieces[0])) {
  66. int c = 1;
  67. while (map.containsKey(pieces[0] + c)) {
  68. c++;
  69. }
  70. pieces[0] = pieces[0] + c;
  71. }
  72. map.put(pieces[0], pieces[1]);
  73. }
  74. return map;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement