warfront1 Posted July 24, 2013 Share Posted July 24, 2013 No externals were used in this class, all manual regex parsing, I will post usage later! package scripts; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ZybezRSItem { private static String Json = ""; public ZybezRSItem(String ItemName){ try { Json = readUrl("http://forums.zybez.net/runescape-2007-prices/api/"+ItemName.replaceAll("\\s","+")); } catch (Exception e) { Json = ""; e.printStackTrace(); } } public int getAveragePrice(){ Pattern pattern = Pattern.compile("(?<=\"average\":\")[0-9]+"); Matcher matcher = pattern.matcher(Json); while (matcher.find()) return(Integer.parseInt(matcher.group())); return 0; } public int getRecentHigh(){ Pattern pattern = Pattern.compile("(?<=\"recent_high\":\")[0-9]+"); Matcher matcher = pattern.matcher(Json); while (matcher.find()) return(Integer.parseInt(matcher.group())); return 0; } public int getRecentLow(){ Pattern pattern = Pattern.compile("(?<=\"recent_low\":\")[0-9]+"); Matcher matcher = pattern.matcher(Json); while (matcher.find()) return(Integer.parseInt(matcher.group())); return 0; } public int getHighAlchPrice(){ Pattern pattern = Pattern.compile("(?<=\"high_alch\":\")[0-9]+"); Matcher matcher = pattern.matcher(Json); while (matcher.find()) return(Integer.parseInt(matcher.group())); return 0; } public String getImageUrl(){ Pattern pattern = Pattern.compile("(?<=\"image\":\")[^\"]*"); Matcher matcher = pattern.matcher(Json); while (matcher.find()) return((matcher.group()).replace("\\", "")); return ""; } public String getZybezID(){ Pattern pattern = Pattern.compile("(?<=\"id\":\")[^\"]*"); Matcher matcher = pattern.matcher(Json); while (matcher.find()) return((matcher.group()).replace("\\", "")); return ""; } private static String readUrl(String urlString) throws Exception { BufferedReader reader = null; try { URL url = new URL(urlString); reader = new BufferedReader(new InputStreamReader(url.openStream())); StringBuffer buffer = new StringBuffer(); int read; char[] chars = new char[1024]; while ((read = reader.read(chars)) != -1) buffer.append(chars, 0, read); return buffer.toString(); } finally { if (reader != null) reader.close(); } } public static String getAllOffersAsJson(){ Pattern pattern = Pattern.compile("(?<=\"offers\":\\[)[^\\]]*"); Matcher matcher = pattern.matcher(Json); while (matcher.find()) return((matcher.group())); return ""; } public static Map<String, String[]> getAllOffersInMap(){ Map<String, String[]> ReturnMap = new HashMap<String, String[]>(); String JsonOffersAsString = getAllOffersAsJson(); Pattern pattern = Pattern.compile("(?<=\"selling\":\")[0|1]"); Matcher matcher = pattern.matcher(JsonOffersAsString); int sizeofreturn = 0; while (matcher.find()){ sizeofreturn = sizeofreturn + 1; } String[] SellingOrBuying = new String[sizeofreturn]; String[] QuantityArray = new String[sizeofreturn]; String[] PriceArray = new String[sizeofreturn]; String[] DateArray = new String[sizeofreturn]; String[] RSNameArray = new String[sizeofreturn]; String[] ContactArray = new String[sizeofreturn]; String[] NotesArray = new String[sizeofreturn]; int counter = 0; pattern = Pattern.compile("(?<=\"selling\":\")[0|1]"); matcher = pattern.matcher(JsonOffersAsString); while (matcher.find()){ SellingOrBuying[counter]=((matcher.group())); counter = counter + 1; } counter = 0; pattern = Pattern.compile("(?<=\"quantity\":\")[0-9]+"); matcher = pattern.matcher(JsonOffersAsString); while (matcher.find()){ QuantityArray[counter]=((matcher.group())); counter = counter + 1; } counter = 0; pattern = Pattern.compile("(?<=\"price\":\")[0-9]+"); matcher = pattern.matcher(JsonOffersAsString); while (matcher.find()){ PriceArray[counter]=((matcher.group())); counter = counter + 1; } counter = 0; pattern = Pattern.compile("(?<=\"date\":\")[0-9]+"); matcher = pattern.matcher(JsonOffersAsString); while (matcher.find()){ DateArray[counter]=((matcher.group())); counter = counter + 1; } counter = 0; pattern = Pattern.compile("(?<=\"rs_name\":\")[^\"]*"); matcher = pattern.matcher(JsonOffersAsString); while (matcher.find()){ RSNameArray[counter]=((matcher.group())); counter = counter + 1; } counter = 0; pattern = Pattern.compile("(?<=\"contact\":\")[^\"]*"); matcher = pattern.matcher(JsonOffersAsString); while (matcher.find()){ ContactArray[counter]=((matcher.group())); counter = counter + 1; } counter = 0; pattern = Pattern.compile("(?<=\"notes\":\")[^\"]*"); matcher = pattern.matcher(JsonOffersAsString); while (matcher.find()){ NotesArray[counter]=((matcher.group())); counter = counter + 1; } ReturnMap.put("selling", SellingOrBuying); ReturnMap.put("quantity", QuantityArray); ReturnMap.put("price", QuantityArray); ReturnMap.put("date", DateArray); ReturnMap.put("rs_name", RSNameArray); ReturnMap.put("contact", ContactArray); ReturnMap.put("notes", NotesArray); return ReturnMap; } public String getFullJsonReturn(){ return Json; } } Link to comment Share on other sites More sharing options...
Jack Posted July 24, 2013 Share Posted July 24, 2013 Thanks I really didnt feel like doing this Usage for those interested ZybezRSItem coal = new ZybezRSItem("Coal"); int avgPrice = coal.getAveragePrice(); int highAlchPrice = coal.getHighAlchPrice(); Link to comment Share on other sites More sharing options...
Led Zeppelin Posted July 25, 2013 Share Posted July 25, 2013 Sweet could work it into some other scripts to know the exact GP p/h like EOC bots Link to comment Share on other sites More sharing options...
GoldenGates Posted July 25, 2013 Share Posted July 25, 2013 Already have my own snippet, which works fine, not as many methods though, still nice job. Link to comment Share on other sites More sharing options...
QBots Posted August 12, 2013 Share Posted August 12, 2013 public static String getName() { Pattern pattern = Pattern.compile("(?<=\"name\":\")[A-Za-z0-9\\s]{1,}"); Matcher matcher = pattern.matcher(Json); while (matcher.find()) return(matcher.group()); return "null"; } Add that to it. It's needed for failsafes as some items will return the wrong price. ie. "roll" which doesn't exist on zybez so it will return like 1m Link to comment Share on other sites More sharing options...
Herbz Posted August 12, 2013 Share Posted August 12, 2013 Looks dope! Link to comment Share on other sites More sharing options...