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.

160 lines
4.6 KiB

  1. package net.jrtechs.www.SteamAPI;
  2. import net.jrtechs.www.utils.ConfigLoader;
  3. import net.jrtechs.www.utils.WebScraper;
  4. import org.json.JSONArray;
  5. import org.json.JSONObject;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. /**
  11. * Class which is used to pull information from the Steam api
  12. *
  13. * @author Jeffery Russell 5-26-18
  14. */
  15. public class APIConnection
  16. {
  17. /** Base url to use for all queries to steam's api **/
  18. private final String baseURL = "http://api.steampowered.com";
  19. /** Path to use when getting info on a player from api **/
  20. private final String playerInfoURL = "/ISteamUser/GetPlayerSummaries/v0002/";
  21. private final String friendListURL = "/ISteamUser/GetFriendList/v0001/";
  22. /** Path to conf file(from within the conf folder) **/
  23. private final String confPath = "SteamAPIKey.json";
  24. /** API key for steam's api - loaded from json conf file **/
  25. private String apiKey;
  26. /**
  27. * Constructor for APIConnection which loads a config file
  28. * and sets the api key to your Steam api key.
  29. */
  30. public APIConnection()
  31. {
  32. ConfigLoader conf = new ConfigLoader(confPath);
  33. apiKey = "?key=" + conf.getValue("api");
  34. }
  35. /**
  36. * Returns a list of the UIDs of all the players friends
  37. *
  38. * @param steamid
  39. * @return
  40. */
  41. public List<String> getFriends(String steamid)
  42. {
  43. List<String> friendsId = new ArrayList<>();
  44. try
  45. {
  46. new JSONObject(WebScraper
  47. .getWebsite(this.baseURL + this.friendListURL +
  48. this.apiKey + "&steamid=" + steamid))
  49. .getJSONObject("friendslist")
  50. .getJSONArray("friends").toList()
  51. .forEach(f->
  52. friendsId.add(((HashMap<String, String>)(f)).get("steamid"))
  53. );
  54. }
  55. catch (Exception ex)
  56. {
  57. ex.printStackTrace();
  58. }
  59. return friendsId;
  60. }
  61. /**
  62. * returns a map from the steam id to the players name
  63. *
  64. * * tricky because we can only request up to 100 ids
  65. * in one request
  66. *
  67. * @param ids
  68. * @return
  69. */
  70. public Map<String, String> getNames(List<String> ids)
  71. {
  72. System.out.println(ids);
  73. Map<String, String> map = new HashMap<>();
  74. while(!ids.isEmpty())
  75. {
  76. String queryUrl = baseURL + playerInfoURL + apiKey + "&steamids=";
  77. int remove = (ids.size() > 100) ? 100 : ids.size();
  78. for(int i = 0; i < remove; i++)
  79. {
  80. queryUrl = queryUrl + "," + ids.remove(0);
  81. }
  82. System.out.println(queryUrl);
  83. JSONArray names = new JSONObject(WebScraper.getWebsite(queryUrl))
  84. .getJSONObject("response").getJSONArray("players");
  85. for(int i = 0; i < names.length(); i++)
  86. {
  87. JSONObject player = names.getJSONObject(i);
  88. System.out.println(player);
  89. map.put(player.getString("steamid"),
  90. player.getString("personaname"));
  91. }
  92. }
  93. return map;
  94. }
  95. /**
  96. * Returns the name of the player with a specific steam id
  97. *
  98. * @param steamid the steam id of player
  99. * @return
  100. */
  101. public String getPlayerName(String steamid)
  102. {
  103. // return ((HashMap<String, String>) new JSONObject(WebScraper
  104. // .getWebsite(this.baseURL + this.playerInfoURL +
  105. // this.apiKey + "&steamids=" + steamid))
  106. // .getJSONObject("response")
  107. // .getJSONArray("players")
  108. // .toList().stream().findAny().get()).get("personaname");
  109. JSONObject response = new JSONObject(WebScraper
  110. .getWebsite(this.baseURL + this.playerInfoURL +
  111. this.apiKey + "&steamids=" + steamid));
  112. if(response.has("response"))
  113. {
  114. response = response.getJSONObject("response");
  115. if(response.has("players"))
  116. {
  117. JSONArray arr = response.getJSONArray("players");
  118. if(arr.length() > 0)
  119. {
  120. return arr.getJSONObject(0).getString("personname");
  121. }
  122. }
  123. }
  124. return null;
  125. }
  126. public static void main(String[] args)
  127. {
  128. APIConnection con = new APIConnection();
  129. con.getFriends("76561198188400721").forEach(System.out::println);
  130. System.out.println(con.getPlayerName("76561198188400721"));
  131. }
  132. }