· 6 years ago · Dec 12, 2019, 06:50 PM
1 public static String whatShouldIPlay(String userID) {
2 String api_key = BotManager.getInstance().SteamAPIKey;
3
4 try {
5 JSONParser parser = new JSONParser();
6 Object obj = parser.parse(
7 BotManager.getRemoteContent("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key="
8 + api_key + "&steamid=" + userID + "&format=json&include_appinfo=1"));
9
10 JSONObject jsonObject = (JSONObject) obj;
11
12 JSONObject response = (JSONObject) (jsonObject.get("response"));
13 JSONArray games = (JSONArray) response.get("games");
14
15 if (games.size() > 0) {
16 int randomGame = (int) (Math.random() * games.size() - 1);
17 JSONObject index0 = (JSONObject) games.get(randomGame);
18 String randomGameName = (String) index0.get("name");
19 return randomGameName;
20 } else {
21 return "User has no games";
22
23 }
24
25 } catch (Exception ex) {
26 System.out.println("Failed to query Steam API");
27 return "Error querying API";
28 }
29 }
30
31 public static String krakenCreated_at(String channel) {
32 try {
33 JSONParser parser = new JSONParser();
34 Object obj = parser
35 .parse(BotManager.getRemoteContentTwitch("https://api.twitch.tv/kraken/streams/" + channel, 5));
36
37 JSONObject jsonObject = (JSONObject) obj;
38
39 JSONObject stream = (JSONObject) (jsonObject.get("stream"));
40 if (stream == null)
41 return "(offline)";
42
43 String viewers = (String) stream.get("created_at");
44 return viewers;
45 } catch (Exception ex) {
46 ex.printStackTrace();
47 return "(error)";
48 }
49
50 }
51
52 public static String steam(String userID, String retValues) {
53 String api_key = BotManager.getInstance().SteamAPIKey;
54
55 try {
56 JSONParser parser = new JSONParser();
57 Object obj = parser.parse(BotManager
58 .getRemoteContent("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids="
59 + userID + "&key=" + api_key));
60
61 JSONObject jsonObject = (JSONObject) obj;
62
63 JSONObject response = (JSONObject) (jsonObject.get("response"));
64 JSONArray players = (JSONArray) response.get("players");
65
66 if (players.size() > 0) {
67 JSONObject index0 = (JSONObject) players.get(0);
68 String profileurl = (String) index0.get("profileurl");
69 String gameextrainfo = (String) index0.get("gameextrainfo");
70 String gameserverip = (String) index0.get("gameserverip");
71 String gameid = (String) index0.get("gameid");
72
73 if (retValues.equals("profile"))
74 return JSONUtil.shortenUrlTinyURL(profileurl);
75 else if (retValues.equals("game"))
76 return (gameextrainfo != null ? gameextrainfo : "(unavailable)");
77 else if (retValues.equals("server"))
78 return (gameserverip != null ? gameserverip : "(unavailable)");
79 else if (retValues.equals("store"))
80 return (gameid != null ? "http://store.steampowered.com/app/" + gameid : "(unavailable)");
81 else
82 return "Profile: " + JSONUtil.shortenUrlTinyURL(profileurl)
83 + (gameextrainfo != null ? ", Game: " + gameextrainfo : "")
84 + (gameserverip != null ? ", Server: " + gameserverip : "");
85
86 } else {
87 return "Error querying API";
88 }
89 } catch (Exception ex) {
90 System.out.println("Failed to query Steam API");
91 return "Error querying API";
92 }
93 }