icekontroi

Item Parser

Oct 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. package scripts;
  2.  
  3. import java.io.*;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.HashMap;
  8.  
  9. public class ItemParser {
  10.  
  11. static HashMap<String, Integer> ALL = new HashMap<>(), F2P = new HashMap<>(), P2P = new HashMap<>();
  12.  
  13. public static void main(String[] args) {
  14.  
  15. try {
  16.  
  17. URL url = new URL("https://rsbuddy.com/exchange/summary.json");
  18.  
  19. try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
  20.  
  21. String summary = reader.readLine();
  22.  
  23. if (summary == null) {
  24. return;
  25. }
  26.  
  27. String[] pieces = summary.split(": \\{");
  28.  
  29. for (String piece : pieces) {
  30.  
  31. String[] split = piece.split(":");
  32.  
  33. String name = null; Integer ID = null; Boolean membership = null;
  34.  
  35. for (int i = 0; i < split.length; i++) {
  36.  
  37. if (split[i].contains("\"id\"")) {
  38. ID = Integer.parseInt(split[i + 1].split(",")[0].replaceAll("[^\\d.]", "").trim());
  39. } else if (split[i].contains("\"name\"")) {
  40. name = split[i + 1].split(",")[0].replaceAll("\"", "").replaceAll("}", "").trim();
  41. } else if (split[i].contains("\"members\"")) {
  42. membership = Boolean.parseBoolean(split[i + 1].split(",")[0].replaceAll("\"", "").replaceAll("}", "").trim());
  43. }
  44. }
  45.  
  46. if (name == null || ID == null || membership == null) {
  47. continue;
  48. }
  49.  
  50. ALL.put(name, ID);
  51.  
  52. if (membership) {
  53. P2P.put(name, ID);
  54. } else {
  55. F2P.put(name, ID);
  56. }
  57. }
  58. }
  59.  
  60. System.out.println("Found " + ALL.size() + " items, " + P2P.size() + " of which were members only and " + F2P.size() + " were not.");
  61.  
  62. // WRITE TO DESKTOP
  63.  
  64. String home = System.getProperty("user.home") + "/Desktop";
  65. File fileALL = new File(home, "ALL.txt"), fileP2P = new File(home, "P2P.txt"), fileF2P = new File(home, "F2P.txt");
  66.  
  67. try (BufferedWriter out1 = new BufferedWriter(new FileWriter(fileALL));
  68. BufferedWriter out2 = new BufferedWriter(new FileWriter(fileP2P));
  69. BufferedWriter out3 = new BufferedWriter(new FileWriter(fileF2P))) {
  70.  
  71. ArrayList<String> names = new ArrayList<>(ALL.keySet());
  72. Collections.sort(names);
  73.  
  74. for (String name : names) {
  75. out1.write(name + ":" + Integer.toString(ALL.get(name)));
  76. out1.newLine();
  77. }
  78.  
  79. names = new ArrayList<>(P2P.keySet());
  80. Collections.sort(names);
  81.  
  82. for (String name : names) {
  83. out2.write(name + ":" + Integer.toString(P2P.get(name)));
  84. out2.newLine();
  85. }
  86. names = new ArrayList<>(F2P.keySet());
  87. Collections.sort(names);
  88.  
  89. for (String name : names) {
  90. out3.write(name + ":" + Integer.toString(F2P.get(name)));
  91. out3.newLine();
  92. }
  93. }
  94.  
  95. } catch (Exception e) {
  96.  
  97. System.out.println("Failed to read RSBuddy summary page:");
  98. e.printStackTrace();
  99. }
  100.  
  101. System.out.println("Finished. The files are on your desktop.");
  102. }
  103. }
Add Comment
Please, Sign In to add comment