Graph database Analysis of the Steam Network
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.3 KiB

  1. package net.jrtechs.www.SteamAPI;
  2. import net.jrtechs.www.server.Player;
  3. import net.jrtechs.www.utils.ConfigLoader;
  4. import net.jrtechs.www.utils.WebScraper;
  5. import net.jrtechs.www.webCrawler.APIThrottler;
  6. import org.json.JSONArray;
  7. import org.json.JSONObject;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. /**
  13. * Class which is used to pull information from the Steam api
  14. *
  15. * @author Jeffery Russell 5-26-18
  16. */
  17. public class APIConnection
  18. {
  19. /** Base url to use for all queries to steam's api **/
  20. private final String baseURL = "http://api.steampowered.com";
  21. /** Path to use when getting info on a player from api **/
  22. private final String playerInfoURL = "/ISteamUser/GetPlayerSummaries/v0002/";
  23. private final String friendListURL = "/ISteamUser/GetFriendList/v0001/";
  24. /** Path to conf file(from within the conf folder) **/
  25. private final String confPath = "SteamAPIKey.json";
  26. /** API key for steam's api - loaded from json conf file **/
  27. private String apiKey;
  28. /**
  29. * Constructor for APIConnection which loads a config file
  30. * and sets the api key to your Steam api key.
  31. */
  32. public APIConnection()
  33. {
  34. ConfigLoader conf = new ConfigLoader(confPath);
  35. apiKey = "?key=" + conf.getValue("api");
  36. }
  37. /**
  38. * Returns a list of the UIDs of all the players friends
  39. *
  40. * @param steamid
  41. * @return
  42. */
  43. public List<String> getFriends(String steamid)
  44. {
  45. List<String> friendsId = new ArrayList<>();
  46. try
  47. {
  48. String apiData = "";
  49. try
  50. {
  51. apiData = WebScraper
  52. .getWebsite(this.baseURL + this.friendListURL +
  53. this.apiKey + "&steamid=" + steamid);
  54. }
  55. catch (SteamConnectionException e)
  56. {
  57. switch (e.getError())
  58. {
  59. case RESTRICTED:
  60. {
  61. //This is fine
  62. System.out.println("Private profile: " + steamid);
  63. return friendsId;
  64. }
  65. case CONNECTION:
  66. {
  67. //spooky 500 error :(
  68. new APIThrottler().wait(120);
  69. try
  70. {
  71. apiData = WebScraper
  72. .getWebsite(this.baseURL + this.friendListURL +
  73. this.apiKey + "&steamid=" + steamid);
  74. }
  75. catch (SteamConnectionException exception2)
  76. {
  77. throw new Exception("Everything is dead");
  78. }
  79. }
  80. }
  81. }
  82. new JSONObject(apiData)
  83. .getJSONObject("friendslist")
  84. .getJSONArray("friends").toList()
  85. .forEach(f->
  86. friendsId.add(((HashMap<String, String>)(f)).get("steamid"))
  87. );
  88. }
  89. catch (Exception ex)
  90. {
  91. ex.printStackTrace();
  92. System.exit(-1);
  93. }
  94. return friendsId;
  95. }
  96. /**
  97. * returns a map from the steam id to the players name
  98. *
  99. * * tricky because we can only request up to 100 ids
  100. * in one request
  101. *
  102. * @param ids
  103. * @return
  104. */
  105. public Map<String, String> getNames(List<String> ids)
  106. {
  107. System.out.println(ids);
  108. Map<String, String> map = new HashMap<>();
  109. while(!ids.isEmpty())
  110. {
  111. String queryUrl = baseURL + playerInfoURL + apiKey + "&steamids=";
  112. int remove = (ids.size() > 100) ? 100 : ids.size();
  113. for(int i = 0; i < remove; i++)
  114. {
  115. queryUrl = queryUrl + "," + ids.remove(0);
  116. }
  117. System.out.println(queryUrl);
  118. JSONArray names;
  119. try
  120. {
  121. names = new JSONObject(WebScraper.getWebsite(queryUrl))
  122. .getJSONObject("response").getJSONArray("players");
  123. }
  124. catch (SteamConnectionException ex)
  125. {
  126. //meh
  127. return map;
  128. }
  129. for(int i = 0; i < names.length(); i++)
  130. {
  131. JSONObject player = names.getJSONObject(i);
  132. if(player.has("steamid") && player.has("personaname"))
  133. {
  134. map.put(player.getString("steamid"),
  135. player.getString("personaname"));
  136. }
  137. }
  138. }
  139. return map;
  140. }
  141. /**
  142. * Wrapper for getNames which returns a list of players instead
  143. * of a map from id's to names
  144. *
  145. * @param ids
  146. * @return
  147. */
  148. public List<Player> getFullPlayers(List<String> ids)
  149. {
  150. Map<String, String> map = this.getNames(ids);
  151. List<Player> players = new ArrayList<>();
  152. for(String id: map.keySet())
  153. {
  154. players.add(new Player(map.get(id),id));
  155. }
  156. return players;
  157. }
  158. /**
  159. * Returns the name of the player with a specific steam id
  160. *
  161. * @param steamid the steam id of player
  162. * @return
  163. */
  164. public String getPlayerName(String steamid)
  165. {
  166. JSONObject response;
  167. try
  168. {
  169. response = new JSONObject(WebScraper
  170. .getWebsite(this.baseURL + this.playerInfoURL +
  171. this.apiKey + "&steamids=" + steamid));
  172. }
  173. catch (SteamConnectionException ex)
  174. {
  175. return "";
  176. }
  177. if(response.has("response"))
  178. {
  179. response = response.getJSONObject("response");
  180. if(response.has("players"))
  181. {
  182. JSONArray arr = response.getJSONArray("players");
  183. if(arr.length() > 0)
  184. {
  185. return arr.getJSONObject(0).getString("personaname");
  186. }
  187. }
  188. }
  189. return null;
  190. }
  191. public static void main(String[] args)
  192. {
  193. APIConnection con = new APIConnection();
  194. con.getFriends("76561198188400721").forEach(System.out::println);
  195. System.out.println(con.getPlayerName("76561198188400721"));
  196. }
  197. }