Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Using official Grand Exchange API.


    heatslinger

    Recommended Posts

    Hi guys, I am fairly new to Dreambot, but I have been scripting for the past few weeks and have found something you all may find useful. I attempted to use the rsbuddy Grand Exchange API; however, I noticed the API could be somewhat unreliable sometimes. So, using some java magic, I just made a quick class to pull information about any given item off the official Runescape Grand Exchange API. The class is commented so just have a look.

    package hoWoodcutter.task;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    
    public class GExchange {
    
    	/*
    	 * BufferedReader for reading text from the API.
    	 * StringBuffer for appending the characters in the API into a string no longer than 1024 chars.
    	 */
    	private static BufferedReader bReader;
    	private static StringBuilder buffer;
    
    	/*
    	 * Returns a string of JSON containing information about the item from the URL.
    	 * 
    	 * PARSE AT YOUR OWN WILL.
    	 */
    	private static String getData(int itemID) {
    		try {
    			buffer = new StringBuilder();
    			int read;
    			char[] chars = new char[1024];
    			URL url = new URL(
    					"http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=" + itemID);
    			bReader = new BufferedReader(new InputStreamReader(url.openStream()));
    
    			while ((read = bReader.read(chars)) != -1) {
    				buffer.append(chars, 0, read);
    			}
    
    			return buffer.toString();
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (bReader != null) {
    					bReader.close();
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    		return null;
    	}
    }
    

    Thanks!

    Link to comment
    Share on other sites

    Always good to have more implementations available! Nice work. I haven't seen someone use a StringBuffer before.

     

     

    (Moved to snippets)

    Shouldn't really even be using StringBuffer as he doesn't need the thread-safe (which makes it slower) properties for an HTTP req, Stringbuilder is a better alternative

    Link to comment
    Share on other sites

    He just doesn't have the try with resources that you do and didn't use readLine().

    which would place like 15 redundant lines

     

    and his variables aren't local for some reason

    Link to comment
    Share on other sites

    • 3 years later...

    I would just use an existing JSON Parser...

     

    THIS > https://dreambot.org/forums/index.php/topic/5559-price-lookup/ < is way over complicating the GetPrice() logic.

     

    import java.awt.Image;
    import java.io.IOException;
    import java.net.URL;
    import java.util.Scanner;
    
    import javax.imageio.ImageIO;
    
    import org.json.simple.JSONObject;
    import org.json.simple.parser.ParseException;
    import org.json.simple.parser.JSONParser;
    
    public class Item {
    	
    	private int id;
    	private String name;
    	
    	public Item(String name, int id) {
    		this.id=id;
    		this.name=name;
    	}
    	
    	public String get6MonthTrend() {
    		return getSubData("day180").get("trend").toString();
    	}
    	public String get6MonthChange() {
    		return getSubData("day180").get("change").toString();
    	}
    	public String get3MonthTrend() {
    		return getSubData("day90").get("trend").toString();
    	}
    	public String get3MonthChange() {
    		return getSubData("day90").get("change").toString();
    	}
    	public String get1MonthTrend() {
    		return getSubData("day30").get("trend").toString();
    	}
    	public String get1MonthChange() {
    		return getSubData("day30").get("change").toString();
    	}
    	public String getTodaysTrend() {
    		return getSubData("today").get("trend").toString();
    	}
    	public String getTodaysChange() {
    		return getSubData("today").get("price").toString();
    	}
    	public String getCurrentPrice() {
    		return getSubData("current").get("price").toString();
    	}
    	public String getCurrentTrend() {
    		return getSubData("current").get("trend").toString();
    	}
    	public Image getIcon() {
    		Image image = null;
    		try {
    			URL url = new URL(getData("icon_large").toString());
    			image = ImageIO.read(url);
    			return image;
    		}
    		catch(IOException e) {
    			
    		}
    		return image;
    	}
    	public boolean isMembers() {
    		String bool = getData("members").toString();
    		return Boolean.parseBoolean(bool);
    	}
    	public String getName() {
    		return name;
    	}
    	public Object getData(String key) {
    		URL url;
    		Scanner s;
    		String out="";
    		String code="";
    		JSONObject data = null;
    		try {
    			url = new URL("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item="+id);
    			s = new Scanner(url.openStream(), "UTF-8");
    			while(true) {
    				if(s.hasNext()) {
    					out = s.next();
    					code = code+out;
    				}
    				else {
    					url.openStream().close();
    					s.close();
    					break;
    				}
    			}
    			if(code=="") {
    				return null;
    			}
    			
    			 try{
    				 JSONParser parser = new JSONParser();
    				 JSONObject item  = (JSONObject) parser.parse(code);				 
    				 data = (JSONObject) parser.parse(item.get("item").toString());
    				 return data.get(key);
    			 }
    			 catch(ParseException e) {
    			 }
    		}
    		catch(IOException e) {
    		}
    		return null;
    	}
    	
    	public JSONObject getSubData(String key) {
    		 try{
    			 JSONParser parser = new JSONParser();				 
    			 JSONObject obj = (JSONObject)parser.parse(getData(key).toString());
    			 return obj;
    		 }
    		 catch(ParseException e) {
    			 e.printStackTrace();
    		 }
    		return null;
    	}
    
    	
    	
     }

     

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.