Jump to content

Zybez price retriever


Boots

Recommended Posts

Retrieves prices from zybez grand exchange.   Use the following format to retrieve prices, the price can be inputted with or without the "+" in bewteen each word

 PriceGrab priceGrab = PriceGrab.getInstance();
        priceGrab.getPrice("snape grass",3);

where the string parameter takes in the actual item name, and the integer parameter will determine if you want high value, low value, or average value. High = 3 , Average = 2, low = 1.


import java.io.*;
import java.net.*;

public class PriceGrab {


    private static PriceGrab oneInstance;
    private URL zybez;
    private URLConnection urlConnection;
    private BufferedReader inputScan;
    private final String zybezUrl = "http://forums.zybez.net/runescape-2007-prices/api/item/";

    public static PriceGrab getInstance(){
        if(oneInstance == null){
            oneInstance = new PriceGrab();
        }
        return oneInstance;
    }

    public int getPrice(String itemName, int command){
        final String AVERAGE = "average",LOW= "recent_high", HIGH="recent_low";
        String item = format(itemName),extracted;
        int price = 0;
        openStream(item);
        extracted = retrieveData(item);
        switch (command){
            case 1:
                return parseInfo(extracted,LOW);

            case 2:
                return parseInfo(extracted,AVERAGE);

            case 3:
               return parseInfo(extracted,HIGH);
        }
        return price;
    }

    private String format(final String string){
        if(string.contains(" "))
            return string.replaceAll(" ", "+");
        else
            return string;
    }

    private void openStream(final String param){
        String appended = zybezUrl.concat(param);

                try {
                    zybez = new URL(appended);
                    urlConnection = zybez.openConnection();
                    urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                }
                catch (MalformedURLException e) {
                    System.out.println("Web address formatted incorrectly, printing stack trace");
                    e.printStackTrace();
                }
                catch(IOException exception) {
                    System.out.println("Url connection has thrown an IOException, printing stack trace");
                    exception.printStackTrace();
                }
    }

    private String retrieveData(final String param){
        String output = null;
        try {
            openStream(param);
            inputScan = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
             output =  inputScan.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {inputScan.close();} catch (IOException e){e.printStackTrace();}
        }
        return output;
    }

    private int parseInfo(String extracted,String value){
        int start, end, price = 0;
        if(extracted.contains(value)){
            start = extracted.indexOf(value);
            end = extracted.indexOf(",",start);
            price = Integer.parseInt(extracted.substring(start, end).replaceFirst(".*?(\\d+).*", "$1"));
        }
        else
            System.out.println("Could not retrieve price");
        return price;
    }
}

Edited by Boots
Link to comment
Share on other sites

I don't know what libraries are in the client by default, but this provides a bit more information wink.png

package org.kenneh.framework.net;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import com.google.gson.Gson;

public class PriceGrabber {

    private static final String URL_BASE = "http://forums.zybez.net/runescape-2007-prices/api/item/";
    private final int itemId;
    private String name;
    
    private int average;
    private int low;
    private int high;
    
    private String imageURL;
    
    private Offer[] offers;
    private Price price;

    public Offer[] getOffers() {
        return offers;
    }
    
    public Price getRaw() {
        return price;
    }
    
    public String getImageURL() {
        return imageURL;
    }

    public int getLow() {
        return low;
    }
    
    public int getHigh() {
        return high;
    }
    
    public int getAverage() {
        return average;
    }
    
    public int getId() {
        return itemId;
    }
    
    public String getName() {
        return name;
    }
    
    public PriceGrabber(final String itemName) {
        this.name = itemName;
        price  = new Gson().fromJson(getJson(getName()), Price.class);
        
        low = (int) price.getLow();
        average = (int) price.getAverage();
        high = (int) price.getHigh();
        name = price.getName();
        imageURL = price.getImageURL();
        itemId = price.getId();
        offers = price.getOffers();
    }

    public PriceGrabber(final int itemId) {
        this.itemId = itemId;
        price = new Gson().fromJson(getJson(String.valueOf(getId())), Price.class);
        
        low = (int) price.getLow();
        average = (int) price.getAverage();
        high = (int) price.getHigh();
        name = price.getName();
        imageURL = price.getImageURL();
        offers = price.getOffers();
    }

    private String getJson(String end) {
        try {
            URL url = new URL(URL_BASE + end.toLowerCase().replaceAll(" ", "%20"));    
            URLConnection urlconn = url.openConnection();
            urlconn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36");
            urlconn.setRequestProperty("Connection", "close");
            BufferedReader in = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
            return in.readLine();
        } catch(Exception a) {
            System.out.println("Error connecting to server.");
        }
        return null;
    }

    public static void main(String[] args) {
        PriceGrabber price = new PriceGrabber("amulet of fury");
        System.out.println(price.getRaw());
        System.out.println(price.getOffers()[0]);
    }

    private class Offer {
        
        private int selling;
        private int quantity;
        private int price;
        private long date;
        private String rs_name;
        private String contact;
        private String notes;
        
        public boolean isSelling() {
            return selling == 1;
        }
        
        public int getQuantity() {
            return quantity;
        }
        
        public int getPrice() {
            return price;
        }
        
        public String getDate() {
            Date d = new Date(date * 1000L);
            return d.toString();
        }
        
        public String getRSName() {
            return rs_name;
        }
        
        public String getContact() {
            return contact;
        }
        
        public String getNotes() {
            return notes;
        }
        
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append(getRSName()).append(" is ").append(isSelling() ? "selling" : "buying").append(" ").append(getQuantity());
            sb.append(" of the item ").append(" for ").append(getPrice()).append(" on ").append(getDate()).append("\t");
            sb.append("Contact ").append(getContact()).append("\t");
            sb.append("Notes: ").append(getNotes());
            return sb.toString();
        }
    }
    
    private class Price {
        
        private int id;
        private String name;
        private String image;
        private double average;
        private double recent_high;
        private double recent_low;
        
        private Offer[] offers;
        
        public Offer[] getOffers() {
            return offers;
        }
        
        public double getHigh() {
            return recent_high;
        }
        
        public double getLow() {
            return recent_low;
        }
        
        public String getImageURL() {
            return image;
        }

        public int getId() {
            return id;
        }
        
        public String getName() {
            return name;
        }

        public double getAverage() {
            return average;
        }
        
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("id=").append(getId()).append("\t");
            sb.append("name=").append(getName()).append("\t");
            sb.append("image=").append(getImageURL()).append("\t");
            sb.append("average=").append(getAverage()).append("\t");
            sb.append("high=").append(getHigh()).append("\t");
            sb.append("low=").append(getLow()).append("\t");
            sb.append("offers=").append(getOffers().length);
            return sb.toString();
        }
        
    }

}

WnG69DJ.png

 

wink.png

Edited by Kenneh
  • Like 1
Link to comment
Share on other sites

 

Nice, I wasn't going for a full zybez retriever, but yours looks good too.

 

Haha, I got bored. It relys on the Gson API and I don't know if that's provided by osbot.

 

Here's a nifty little utility I was able to make with it with little effort.

Vis0yVn.png

 

should extract the picture and maybe an auto fill feature would make it nice

Link to comment
Share on other sites

 

 

Nice, I wasn't going for a full zybez retriever, but yours looks good too.

 

Haha, I got bored. It relys on the Gson API and I don't know if that's provided by osbot.

 

Here's a nifty little utility I was able to make with it with little effort.

Vis0yVn.png

 

should extract the picture and maybe an auto fill feature would make it nice

 

 

I'm really bad at swing, so I'm not sure how to make this look decent.

 

Ecomvfb.png

 

For the auto complete, that'd be a nifty feature, but all this does is grab what you put in the box and appends it to the end of the url. I dont think I can make it auto complete without a list of item names of some sort.

  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...