Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package net.meltdowntech.steamstats; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; /** * * @author Miguel Obregon <miguel_lego@yahoo.com> */ public class SteamGame { private final Map<String, Object> values; private SteamGame() { values = new TreeMap<>(); } public int getPlaytime() { if (values.containsKey("playtime_forever")) return (Integer) values.get("playtime_forever"); return 0; } public static SteamGame fetchGame(int id) { //Create query url String url = Steam.URL + Steam.GAME_DATA + "?key=" + Steam.KEY + "&appid=" + id; try { //Attempt connection and parse URL steam = new URL(url); URLConnection steamConn = steam.openConnection(); Reader steamReader = new InputStreamReader(steamConn.getInputStream()); JSONTokener steamTokener = new JSONTokener(steamReader); JSONObject data = (JSONObject) steamTokener.nextValue(); JSONObject response = data.getJSONObject("game"); SteamGame game = new SteamGame(); //Parse each field Iterator keys = response.keys(); while (keys.hasNext()) { String key = (String) keys.next(); game.values.put(key, response.get(key)); } return game; } catch (MalformedURLException ex) { Util.printError("Invalid URL"); } catch (IOException | JSONException ex) { Util.printError("Invalid data"); } catch (Exception ex) { Util.printError("Generic error"); } return new SteamGame(); } public static List<SteamGame> fetchGames(SteamUser user, boolean all) { List<SteamGame> games = new ArrayList<>(); Util.printDebug("Fetching games"); //Create query url String url = Steam.URL + Steam.GAME_OWNED + "?key=" + Steam.KEY + "&include_played_free_games&steamid=" + user.getCommunityId(); try { //Attempt connection and parse URL steam = new URL(url); URLConnection steamConn = steam.openConnection(); Reader steamReader = new InputStreamReader(steamConn.getInputStream()); JSONTokener steamTokener = new JSONTokener(steamReader); JSONObject data = (JSONObject) steamTokener.nextValue(); JSONObject response = data.getJSONObject("response"); JSONArray apps = response.getJSONArray("games"); Util.printInfo(String.format("%d game%s allotted", apps.length(), apps.length() == 1 ? "" : "s")); //Parse each game for (int y = 0; y < apps.length(); y++) { JSONObject app = apps.getJSONObject(y); SteamGame game = all ? fetchGame(app.getInt("appid")) : new SteamGame(); //Parse each field Iterator keys = app.keys(); while (keys.hasNext()) { String key = (String) keys.next(); game.values.put(key, app.get(key)); } games.add(game); } } catch (MalformedURLException ex) { Util.printError("Invalid URL"); } catch (IOException | JSONException ex) { Util.printError("Invalid data"); } catch (Exception ex) { Util.printError("Generic error"); } Util.printDebug("Done fetching games"); return games; } }