Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.meowzika;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.ListIterator;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import com.lukaspradel.steamapi.core.exception.SteamApiException;
- import com.lukaspradel.steamapi.data.json.friendslist.Friend;
- import com.lukaspradel.steamapi.data.json.friendslist.Friendslist;
- import com.lukaspradel.steamapi.data.json.friendslist.GetFriendList;
- import com.lukaspradel.steamapi.data.json.playersummaries.GetPlayerSummaries;
- import com.lukaspradel.steamapi.data.json.playersummaries.Player;
- import com.lukaspradel.steamapi.webapi.client.SteamWebApiClient;
- import com.lukaspradel.steamapi.webapi.request.GetFriendListRequest;
- import com.lukaspradel.steamapi.webapi.request.GetFriendListRequest.Relationship;
- import com.lukaspradel.steamapi.webapi.request.GetPlayerSummariesRequest;
- import com.lukaspradel.steamapi.webapi.request.builders.SteamWebApiRequestFactory;
- import edu.princeton.cs.algs4.Graph;
- import edu.princeton.cs.algs4.In;
- import edu.princeton.cs.algs4.Out;
- import edu.princeton.cs.algs4.StdOut;
- import edu.princeton.cs.algs4.SymbolGraph;
- public class SteamSnooper {
- private SteamWebApiClient client;
- // Used to connect a person with other people.
- private SymbolGraph sg;
- // <steamID, ["bob", "jim", "joe"]>
- private HashMap<String, List<String>> mappedFriends = new HashMap<>();
- // <steamID, friendsState> where state can be public, private and unchecked
- // this is concurrent so I can iterate over it and change values simultaneously
- // this could be a problem since i haven't completely understood it myself.
- private ConcurrentHashMap<String, String> friendsStatus = new ConcurrentHashMap<>();
- // this will be printed to the out file below like so: "name name"
- private ArrayList<String> outConnections = new ArrayList<>();
- // allConnections.txt
- private Out out;
- // Initializes the client and the out file.
- public SteamSnooper(String steamWebApiKey) throws FileNotFoundException {
- // This needs to check if the steamId is a valid steamId.
- if (steamWebApiKey == null) {
- throw new java.lang.IllegalArgumentException();
- }
- // create the client first using my steam web api key.
- client = new SteamWebApiClient.SteamWebApiClientBuilder(steamWebApiKey).build();
- // creates an initial allConnections.txt file.
- out = new Out("allConnections.txt");
- }
- // clicks the person, opening their friends list if possible.
- public void openFriendsOf(String steamId) throws FileNotFoundException {
- if (steamId == null) {
- throw new java.lang.NullPointerException();
- }
- String status = null;
- // does it exist in the hashmap?
- if (friendsStatus.containsKey(steamId)) {
- // if that name has been checked for friends, do not visit it.
- status = friendsStatus.get(steamId);
- if (status == "public" || status == "private") {
- return; // just exits the method
- }
- }
- // Request the friendsList using steamId
- GetFriendListRequest friendsListrequest = SteamWebApiRequestFactory
- .createGetFriendListRequest(steamId, Relationship.FRIEND);
- GetFriendList processedFriendsListRequest = null;
- try {
- processedFriendsListRequest = client.<GetFriendList>processRequest(friendsListrequest);
- } catch (SteamApiException e) {
- // The friendsListRequest was not successful so make it private.
- if (status == "unchecked") {
- friendsStatus.replace(steamId, "private");
- } else {
- friendsStatus.put(steamId, "private");
- }
- return;
- }
- // get the FriendsList.
- Friendslist friendsList = processedFriendsListRequest.getFriendslist();
- List<Friend> list = friendsList.getFriends(); // make it a list of friends.
- // Make a list of steamIds.
- List<String> steamIds = new ArrayList<>();
- Iterator<Friend> friends = list.iterator();
- // iterate over the friends and put their steamIds into the List.
- while (friends.hasNext()) {
- String tempSteamId = friends.next().getSteamid();
- steamIds.add(tempSteamId);
- // if friendsStatus doesn't contain the steamId
- if (!friendsStatus.contains(tempSteamId)) {
- // then make it unchecked for now
- friendsStatus.put(tempSteamId, "unchecked");
- }
- }
- // After the friends are put in the list, map that person to the list.
- mappedFriends.put(steamId, steamIds);
- // the steamId is now visited.
- friendsStatus.put(steamId, "public");
- }
- public void openFriendsOfNotVisited() throws FileNotFoundException, SteamApiException {
- Iterator<String> steamIds = friendsStatus.keySet().iterator();
- while (steamIds.hasNext()) {
- String steamId = (String) steamIds.next();
- if (steamId == "unchecked") {
- openFriendsOf(steamId);
- }
- }
- }
- // this prints to the "out" reference like so:
- // steamId steamId
- private void updateOutFile() {
- // iterate over the steamIds in mappedFriends
- for (Map.Entry<String, List<String>> entry : mappedFriends.entrySet()) {
- String steamId = entry.getKey();
- List<String> steamIdFriends = entry.getValue();
- for (String steamIdFriend : steamIdFriends) {
- // this checks if there exists a parallel connection
- // example: "name1 name2" is parallel to "name2 name1"
- if (!outConnections.contains(steamIdFriend + " " + steamId)) {
- outConnections.add(steamId + " " + steamIdFriend);
- out.println(steamId + " " + steamIdFriend);
- }
- }
- }
- }
- private void updateGraph() {
- updateOutFile();
- sg = new SymbolGraph("allConnections.txt", " ");
- }
- public void printConnections() {
- updateGraph();
- In in = new In("allConnections.txt");
- // prints the Connections.
- while (in.hasNextLine()) {
- String source = in.readLine();
- String[] twoIds = source.split(" "); // steamId steamId
- StdOut.printf("%s %s\n", twoIds[0], twoIds[1]);
- }
- }
- public void printFriendsOf(String steamId) {
- updateGraph();
- Graph g = sg.graph();
- StdOut.printf("%-30s\n", steamId);
- for (int w : g.adj(sg.indexOf(steamId))) {
- StdOut.printf("%30s\n", sg.nameOf(w));
- }
- }
- public void printSteamIdsAndNumberOfFriends() {
- updateGraph();
- Graph g = sg.graph();
- for (int v = 0; v < getV(); v++) {
- StdOut.printf("%-30s has %d friend(s)\n", sg.nameOf(v), g.degree(v));
- }
- }
- public void printNamesOfSteamIds(List<String> steamIds) throws SteamApiException {
- // Gets summaries of the players, be it steamIds, personanames, etc.
- GetPlayerSummariesRequest playerSummariesRequest = SteamWebApiRequestFactory
- .createGetPlayerSummariesRequest(steamIds);
- GetPlayerSummaries processedPlayerSummariesRequest = client
- .<GetPlayerSummaries>processRequest(playerSummariesRequest);
- var response = processedPlayerSummariesRequest.getResponse();
- var players = response.getPlayers();
- ListIterator<Player> playersIterator = players.listIterator();
- while (playersIterator.hasNext()) {
- var player = playersIterator.next();
- String personaname = player.getPersonaname();
- String steamId = player.getSteamid();
- StdOut.printf("personaname: %-35s steamid: %s\n", personaname, steamId);
- }
- }
- public int getV() {
- Graph g = sg.graph();
- return g.V();
- }
- public int getE() {
- Graph g = sg.graph();
- return g.E();
- }
- public static void run() throws FileNotFoundException, SteamApiException {
- String steamWebApiKey = ""; // go to steam and setup your account with the steam api
- String mySteamId = "76561198004223766";
- SteamSnooper s = new SteamSnooper(steamWebApiKey);
- // attempts to open friends of someone
- s.openFriendsOf(mySteamId);
- // attempts to open the friends of every indexed friend
- s.openFriendsOfNotVisited();
- StdOut.println("finished s.openFriendsOfNotVisited()");
- // prints steamId and the connections
- s.printConnections();
- // a private profile
- // String steamId = "";
- // s.printFriendsOf(steamId);
- }
- public static void main(String[] args) throws FileNotFoundException, SteamApiException {
- final long startTime = System.currentTimeMillis();
- SteamSnooper.run();
- final long endTime = System.currentTimeMillis();
- System.out.printf("Execution time: %dms", (endTime - startTime));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement