From c658ed32cfd247d48e4eed9e7794404748eb8de9 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 26 May 2018 11:02:16 -0400 Subject: [PATCH] Added connection to steam api to get names and friends lists. --- .../jrtechs/www/SteamAPI/APIConnection.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java b/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java index 653a174..03c6060 100644 --- a/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java +++ b/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java @@ -2,6 +2,13 @@ package net.jrtechs.www.SteamAPI; import net.jrtechs.www.utils.ConfigLoader; +import net.jrtechs.www.utils.WebScraper; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + /** * Class which is used to pull information from the Steam api * @@ -34,4 +41,52 @@ public class APIConnection apiKey = "?key=" + conf.getValue("api"); } + + + /** + * Returns a list of the UIDs of all the players friends + * + * @param steamid + * @return + */ + public List getFriends(String steamid) + { + List friendsId = new ArrayList<>(); + + new JSONObject(WebScraper + .getWebsite(this.baseURL + this.friendListURL + + this.apiKey + "&steamid=" + steamid)) + .getJSONObject("friendslist") + .getJSONArray("friends").toList() + .forEach(f-> + friendsId.add(((HashMap)(f)).get("steamid")) + ); + return friendsId; + } + + + /** + * Returns the name of the player with a specific steam id + * + * @param steamid the steam id of player + * @return + */ + public String getPlayerName(String steamid) + { + return ((HashMap) new JSONObject(WebScraper + .getWebsite(this.baseURL + this.playerInfoURL + + this.apiKey + "&steamids=" + steamid)) + .getJSONObject("response") + .getJSONArray("players") + .toList().stream().findAny().get()).get("personaname"); + } + + public static void main(String[] args) + { + APIConnection con = new APIConnection(); + + con.getFriends("76561198188400721").forEach(System.out::println); + + System.out.println(con.getPlayerName("76561198188400721")); + } }