Advertisement
Guest User

Untitled

a guest
Jul 8th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.Reader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class ZybezItem {
  11.  
  12. private int zybezId;
  13. private String name;
  14. private String image;
  15. private int recentHigh;
  16. private int recentLow;
  17. private int average;
  18. private int highAlch;
  19. private Offer[] offers;
  20.  
  21. public ZybezItem(String name) {
  22. JSONObject json = null;
  23. try {
  24. json = readJsonFromUrl("http://forums.zybez.net/runescape-2007-prices/api/item/"
  25. + format(name));
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. if (json != null && json.has("id")) {
  30. try {
  31. this.zybezId = json.getInt("id");
  32. this.name = json.getString("name");
  33. this.image = json.getString("image");
  34. this.recentHigh = (int) Double.parseDouble(json
  35. .getString("recent_high"));
  36. this.recentLow = (int) Double.parseDouble(json
  37. .getString("recent_low"));
  38. this.average = (int) Double.parseDouble(json
  39. .getString("average"));
  40. this.highAlch = json.getInt("high_alch");
  41. JSONArray jsonOfferArr = json.getJSONArray("offers");
  42. this.offers = new Offer[jsonOfferArr.length()];
  43. for (int i = 0; i < jsonOfferArr.length(); i++) {
  44. this.offers[i] = new Offer(jsonOfferArr.getJSONObject(i),
  45. this.getName());
  46. }
  47. } catch (JSONException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52.  
  53. public ZybezItem(int id) {
  54. JSONObject json = null;
  55. try {
  56. json = readJsonFromUrl("http://forums.zybez.net/runescape-2007-prices/api/item/"
  57. + format(getItemName(id)));
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. if (json != null && json.has("id")) {
  62. try {
  63. this.zybezId = json.getInt("id");
  64. this.name = json.getString("name");
  65. this.image = json.getString("image");
  66. this.recentHigh = (int) Double.parseDouble(json
  67. .getString("recent_high"));
  68. this.recentLow = (int) Double.parseDouble(json
  69. .getString("recent_low"));
  70. this.average = (int) Double.parseDouble(json
  71. .getString("average"));
  72. this.highAlch = json.getInt("high_alch");
  73. JSONArray jsonOfferArr = json.getJSONArray("offers");
  74. this.offers = new Offer[jsonOfferArr.length()];
  75. for (int i = 0; i < jsonOfferArr.length(); i++) {
  76. this.offers[i] = new Offer(jsonOfferArr.getJSONObject(i),
  77. this.getName());
  78. }
  79. } catch (JSONException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }
  84.  
  85. /**
  86. * @return the zybez id associated to the zybezItem (THESE ARE NOT THE SAME
  87. * AS RUNESCAPE IDS)
  88. */
  89. public int getZybezId() {
  90. return zybezId;
  91. }
  92.  
  93. /**
  94. * @return the name of the zybezItem
  95. */
  96. public String getName() {
  97. return name;
  98. }
  99.  
  100. /**
  101. * @return the URL of an image of the zybezItem
  102. */
  103. public String getImage() {
  104. return image;
  105. }
  106.  
  107. /**
  108. * @return the recent high price of the zybezItem
  109. */
  110. public int getRecentHigh() {
  111. return recentHigh;
  112. }
  113.  
  114. /**
  115. * @return the recent low price of the zybezItem
  116. */
  117. public int getRecentLow() {
  118. return recentLow;
  119. }
  120.  
  121. public void setZybezId(int zybezId) {
  122. this.zybezId = zybezId;
  123. }
  124.  
  125. public void setName(String name) {
  126. this.name = name;
  127. }
  128.  
  129. public void setImage(String image) {
  130. this.image = image;
  131. }
  132.  
  133. public void setRecentHigh(int recentHigh) {
  134. this.recentHigh = recentHigh;
  135. }
  136.  
  137. public void setRecentLow(int recentLow) {
  138. this.recentLow = recentLow;
  139. }
  140.  
  141. public void setAverage(int average) {
  142. this.average = average;
  143. }
  144.  
  145. public void setHighAlch(int highAlch) {
  146. this.highAlch = highAlch;
  147. }
  148.  
  149. public void setOffers(Offer[] offers) {
  150. this.offers = offers;
  151. }
  152.  
  153. public int getAverage() {
  154. return average;
  155. }
  156.  
  157. public int getHighAlch() {
  158. return highAlch;
  159. }
  160.  
  161. /**
  162. * @return an array of offer objects
  163. */
  164. public Offer[] getOffers() {
  165. return offers;
  166. }
  167.  
  168. private static String readAll(Reader rd) throws IOException {
  169. StringBuilder sb = new StringBuilder();
  170. int cp;
  171. while ((cp = rd.read()) != -1) {
  172. sb.append((char) cp);
  173. }
  174. return sb.toString();
  175. }
  176.  
  177. public static JSONObject readJsonFromUrl(String url) throws IOException,
  178. JSONException {
  179. HttpURLConnection connection = (HttpURLConnection) new URL(url)
  180. .openConnection();
  181. connection
  182. .setRequestProperty(
  183. "User-Agent",
  184. "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");
  185. InputStreamReader is = new InputStreamReader(
  186. connection.getInputStream());
  187. try {
  188. BufferedReader rd = new BufferedReader(is);
  189. String jsonText = readAll(rd);
  190. JSONObject json = new JSONObject(jsonText);
  191. return json;
  192. } finally {
  193. is.close();
  194. }
  195. }
  196.  
  197. private static String getItemName(int id) {
  198. /*
  199. * Idea taken from WillB
  200. */
  201. URL url;
  202. String txt = null;
  203. try {
  204. url = new URL("http://www.runelocus.com/item-details/?item_id="
  205. + id);
  206. BufferedReader reader = new BufferedReader(new InputStreamReader(
  207. url.openStream()));
  208. StringBuilder sb = new StringBuilder();
  209. while (reader.ready()) {
  210. sb.append(reader.readLine());
  211. }
  212. Pattern p = Pattern.compile("<h2>Information about '(.*?)'</h2>");
  213. Matcher m = p.matcher(sb.toString());
  214. if (m.find()) {
  215. txt = m.group(1);
  216. }
  217. } catch (IOException e) {
  218. e.printStackTrace();
  219. }
  220. return txt;
  221. }
  222.  
  223. private String format(String name) {
  224. return name.replace(" ", "+").replace("'", "");
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement