TheCloakdOne 389 Share Posted November 23, 2020 As part of another project i needed a nicer way of handling Item names -> ID's without bloating out scripts with bulky json files so built an API to provide this information to anyone who needs it. API is built in Golang & on a highly available platform so shouldnt ever go down. Feel free to use if there is enough traffic ill add a redis cache to speed it up further. The API will take an String input (item name or ID) and return the current Runelite Buy & Sell prices along with the Item ID. If no item can be found item id will be 0. Site now also supports HTTP traffic (swap HTTP for HTTPS in examples) Examples: Glory: http://mule.cloakd.co.uk/lookup/items/name?name=Amulet%20of%20glory(6) http://mule.cloakd.co.uk/lookup/items/id?id=11978 Dragon Scim: http://mule.cloakd.co.uk/lookup/items/name?name=Dragon%20scimitar http://mule.cloakd.co.uk/lookup/items/id?id=4587 Bucket: http://mule.cloakd.co.uk/lookup/items/name?name=Bucket http://mule.cloakd.co.uk/lookup/items/id?id=1925 Response: { "id": 11978, "name": "Amulet of glory(6)", "buy_price": 11999, "sell_price": 11999 } Code Snippets Response Class: public class ItemResponse { public int id = -1; public String name = ""; public int buy_price = 0; public int sell_price = 0; } Example Function: function ItemResponse getItem(string name) { String encName = ""; try { encName = URLEncoder.encode(name, StandardCharsets.UTF_8.toString()); } catch (UnsupportedEncodingException e) { return null; } String resp = this.getJson("http://mule.cloakd.co.uk", "/lookup/items/name?name=" + encName); if (resp == null) return null; return gson.fromJson(resp, ItemResponse.class); } function ItemResponse getItem(int id) { String resp = this.getJson("http://mule.cloakd.co.uk", "/lookup/items/id?id=" + id); if (resp == null) return null; return gson.fromJson(resp, ItemResponse.class); } Example JSON Call: public String getJson(String baseURL, String targetURL) { MethodProvider.logInfo("GET - " + baseURL + targetURL); HttpURLConnection connection = null; try { URL url = new URL(baseURL + targetURL); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/json; utf-8"); connection.setRequestProperty("Accept", "application/json"); connection.setUseCaches(false); connection.setDoOutput(true); int code = connection.getResponseCode(); if (code >= 400) { MethodProvider.logInfo("Network HTTP error: " + code); return null; } if (code == 204) { MethodProvider.logInfo("Network HTTP No Content: " + code); return null; } //Get Response InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+ String line; while ((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); return response.toString(); } catch (Exception e) { MethodProvider.logInfo("getJson Err: " + e.getMessage()); e.printStackTrace(); return null; } finally { if (connection != null) { connection.disconnect(); } } } Link to comment Share on other sites More sharing options...
Hashtag 8670 Share Posted November 23, 2020 Thanks for the contribution! Link to comment Share on other sites More sharing options...
Aeon 43 Share Posted November 23, 2020 Great snippet! Much cleaner than my method... Link to comment Share on other sites More sharing options...
TheCloakdOne 389 Author Share Posted December 1, 2020 Now updated to support ID also if needed: function ItemResponse getItem(int id) { String resp = this.getJson("http://mule.cloakd.co.uk", "/lookup/items/id?id=" + id); if (resp == null) return null; return gson.fromJson(resp, ItemResponse.class); } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.