Advertisement
Chiddix

PriceGrabber

Mar 28th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. package org.rabrg.util;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7.  
  8. /**
  9.  * Grabs prices from the Zybez RuneScape 2007 marketplace.
  10.  * @author Ryan Greene
  11.  *
  12.  */
  13. public final class PriceGrabber {
  14.  
  15.     /**
  16.      * The base of the URL used to grab prices.
  17.      */
  18.     private static final String BASE_URL = "http://forums.zybez.net/runescape-2007-prices/api/";
  19.  
  20.     /**
  21.      * Gets the price of the item with the specified name.
  22.      * @param itemName The name of the item.
  23.      * @return The price of the item.
  24.      * @throws IOException If an IOException occurs.
  25.      */
  26.     public static Price getPrice(final String itemName) throws IOException {
  27.         try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(BASE_URL + itemName).openStream()))) {
  28.             String line = reader.readLine().replaceAll("" + '"', "");
  29.            
  30.             int id = Integer.parseInt(line.split("id:")[1].split(",")[0]);
  31.             String name = line.split("name:")[1].split(",")[0];
  32.             String image = line.split("image:")[1].split(",")[0];
  33.             float recentHigh = Float.parseFloat(line.split("recent_high:")[1].split(",")[0]);
  34.             float recentLow = Float.parseFloat(line.split("recent_low:")[1].split(",")[0]);
  35.             float average = Float.parseFloat(line.split("average:")[1].split(",")[0]);
  36.             int highAlch = Integer.parseInt(line.split("high_alch:")[1].split(",")[0]);
  37.            
  38.             return new Price(id, name, image, recentHigh, recentLow, average, highAlch);
  39.         }
  40.     }
  41.  
  42.     /**
  43.      * Represents a single price of an item.
  44.      * @author Ryan Greene
  45.      *
  46.      */
  47.     public static final class Price {
  48.  
  49.         /**
  50.          * The id of the item.
  51.          */
  52.         private final int id;
  53.  
  54.         /**
  55.          * The name of the item.
  56.          */
  57.         private final String name;
  58.  
  59.         /**
  60.          * The image of the item.
  61.          */
  62.         private final String image;
  63.  
  64.         /**
  65.          * The recent high price of the item.
  66.          */
  67.         private final float recentHigh;
  68.  
  69.         /**
  70.          * The recent low price of the item.
  71.          */
  72.         private final float recentLow;
  73.  
  74.         /**
  75.          * The average price of the item.
  76.          */
  77.         private final float average;
  78.  
  79.         /**
  80.          * The high alch value of the item.
  81.          */
  82.         private final int highAlch;
  83.  
  84.         /**
  85.          * Constructs a new price.
  86.          * @param id
  87.          * @param name
  88.          * @param image
  89.          * @param recentHigh
  90.          * @param recentLow
  91.          * @param average
  92.          * @param highAlch
  93.          */
  94.         private Price(int id, String name, String image, float recentHigh, float recentLow, float average, int highAlch) {
  95.             this.id = id;
  96.             this.name = name;
  97.             this.image = image;
  98.             this.recentHigh = recentHigh;
  99.             this.recentLow = recentLow;
  100.             this.average = average;
  101.             this.highAlch = highAlch;
  102.         }
  103.  
  104.         /**
  105.          * Gets the id of the item.
  106.          * @return The id of the item.
  107.          */
  108.         public int getId() {
  109.             return id;
  110.         }
  111.  
  112.         /**
  113.          * Gets the name of the item.
  114.          * @return The name of the item.
  115.          */
  116.         public String getName() {
  117.             return name;
  118.         }
  119.  
  120.         /**
  121.          * Gets the image of the item.
  122.          * @return The image of the item.
  123.          */
  124.         public String getImage() {
  125.             return image;
  126.         }
  127.  
  128.         /**
  129.          * Gets the recent high price of the item.
  130.          * @return The recent high price of the item.
  131.          */
  132.         public float getRecentHigh() {
  133.             return recentHigh;
  134.         }
  135.  
  136.         /**
  137.          * Gets the recent low price of the item.
  138.          * @return The recent low price of the item.
  139.          */
  140.         public float getRecentLow() {
  141.             return recentLow;
  142.         }
  143.  
  144.         /**
  145.          * Gets the average price of the item.
  146.          * @return The average price of the item.
  147.          */
  148.         public float getAverage() {
  149.             return average;
  150.         }
  151.  
  152.         /**
  153.          * Gets the high alch value of the item.
  154.          * @return The high alch value of the item.
  155.          */
  156.         public int getHighAlch() {
  157.             return highAlch;
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement