Advertisement
Guest User

snooper

a guest
Jan 22nd, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.35 KB | None | 0 0
  1. package com.meowzika;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.ListIterator;
  9. import java.util.Map;
  10. import java.util.concurrent.ConcurrentHashMap;
  11.  
  12. import com.lukaspradel.steamapi.core.exception.SteamApiException;
  13. import com.lukaspradel.steamapi.data.json.friendslist.Friend;
  14. import com.lukaspradel.steamapi.data.json.friendslist.Friendslist;
  15. import com.lukaspradel.steamapi.data.json.friendslist.GetFriendList;
  16. import com.lukaspradel.steamapi.data.json.playersummaries.GetPlayerSummaries;
  17. import com.lukaspradel.steamapi.data.json.playersummaries.Player;
  18. import com.lukaspradel.steamapi.webapi.client.SteamWebApiClient;
  19. import com.lukaspradel.steamapi.webapi.request.GetFriendListRequest;
  20. import com.lukaspradel.steamapi.webapi.request.GetFriendListRequest.Relationship;
  21. import com.lukaspradel.steamapi.webapi.request.GetPlayerSummariesRequest;
  22. import com.lukaspradel.steamapi.webapi.request.builders.SteamWebApiRequestFactory;
  23.  
  24. import edu.princeton.cs.algs4.Graph;
  25. import edu.princeton.cs.algs4.In;
  26. import edu.princeton.cs.algs4.Out;
  27. import edu.princeton.cs.algs4.StdOut;
  28. import edu.princeton.cs.algs4.SymbolGraph;
  29.  
  30. public class SteamSnooper {
  31.     private SteamWebApiClient                 client;
  32.     // Used to connect a person with other people.
  33.     private SymbolGraph                       sg;
  34.  
  35.     // <steamID, ["bob", "jim", "joe"]>
  36.     private HashMap<String, List<String>>     mappedFriends  = new HashMap<>();
  37.  
  38.     // <steamID, friendsState> where state can be public, private and unchecked
  39.     // this is concurrent so I can iterate over it and change values simultaneously
  40.     // this could be a problem since i haven't completely understood it myself.
  41.     private ConcurrentHashMap<String, String> friendsStatus  = new ConcurrentHashMap<>();
  42.  
  43.     // this will be printed to the out file below like so: "name name"
  44.     private ArrayList<String>                 outConnections = new ArrayList<>();
  45.  
  46.     // allConnections.txt
  47.     private Out                               out;
  48.  
  49.     // Initializes the client and the out file.
  50.     public SteamSnooper(String steamWebApiKey) throws FileNotFoundException {
  51.         // This needs to check if the steamId is a valid steamId.
  52.         if (steamWebApiKey == null) {
  53.             throw new java.lang.IllegalArgumentException();
  54.         }
  55.  
  56.         // create the client first using my steam web api key.
  57.         client = new SteamWebApiClient.SteamWebApiClientBuilder(steamWebApiKey).build();
  58.  
  59.         // creates an initial allConnections.txt file.
  60.         out = new Out("allConnections.txt");
  61.     }
  62.  
  63.     // clicks the person, opening their friends list if possible.
  64.     public void openFriendsOf(String steamId) throws FileNotFoundException {
  65.         if (steamId == null) {
  66.             throw new java.lang.NullPointerException();
  67.         }
  68.  
  69.         String status = null;
  70.         // does it exist in the hashmap?
  71.         if (friendsStatus.containsKey(steamId)) {
  72.             // if that name has been checked for friends, do not visit it.
  73.             status = friendsStatus.get(steamId);
  74.             if (status == "public" || status == "private") {
  75.                 return; // just exits the method
  76.             }
  77.         }
  78.  
  79.         // Request the friendsList using steamId
  80.         GetFriendListRequest friendsListrequest = SteamWebApiRequestFactory
  81.                 .createGetFriendListRequest(steamId, Relationship.FRIEND);
  82.         GetFriendList processedFriendsListRequest = null;
  83.  
  84.         try {
  85.             processedFriendsListRequest = client.<GetFriendList>processRequest(friendsListrequest);
  86.         } catch (SteamApiException e) {
  87.             // The friendsListRequest was not successful so make it private.
  88.             if (status == "unchecked") {
  89.                 friendsStatus.replace(steamId, "private");
  90.             } else {
  91.                 friendsStatus.put(steamId, "private");
  92.             }
  93.             return;
  94.         }
  95.  
  96.         // get the FriendsList.
  97.         Friendslist friendsList = processedFriendsListRequest.getFriendslist();
  98.         List<Friend> list = friendsList.getFriends(); // make it a list of friends.
  99.  
  100.         // Make a list of steamIds.
  101.         List<String> steamIds = new ArrayList<>();
  102.  
  103.         Iterator<Friend> friends = list.iterator();
  104.         // iterate over the friends and put their steamIds into the List.
  105.         while (friends.hasNext()) {
  106.             String tempSteamId = friends.next().getSteamid();
  107.             steamIds.add(tempSteamId);
  108.  
  109.             // if friendsStatus doesn't contain the steamId
  110.             if (!friendsStatus.contains(tempSteamId)) {
  111.                 // then make it unchecked for now
  112.                 friendsStatus.put(tempSteamId, "unchecked");
  113.             }
  114.         }
  115.         // After the friends are put in the list, map that person to the list.
  116.         mappedFriends.put(steamId, steamIds);
  117.  
  118.         // the steamId is now visited.
  119.         friendsStatus.put(steamId, "public");
  120.     }
  121.  
  122.     public void openFriendsOfNotVisited() throws FileNotFoundException, SteamApiException {
  123.         Iterator<String> steamIds = friendsStatus.keySet().iterator();
  124.         while (steamIds.hasNext()) {
  125.             String steamId = (String) steamIds.next();
  126.             if (steamId == "unchecked") {
  127.                 openFriendsOf(steamId);
  128.             }
  129.         }
  130.     }
  131.  
  132.     // this prints to the "out" reference like so:
  133.     // steamId steamId
  134.     private void updateOutFile() {
  135.         // iterate over the steamIds in mappedFriends
  136.         for (Map.Entry<String, List<String>> entry : mappedFriends.entrySet()) {
  137.             String steamId = entry.getKey();
  138.             List<String> steamIdFriends = entry.getValue();
  139.  
  140.             for (String steamIdFriend : steamIdFriends) {
  141.                 // this checks if there exists a parallel connection
  142.                 // example: "name1 name2" is parallel to "name2 name1"
  143.                 if (!outConnections.contains(steamIdFriend + " " + steamId)) {
  144.                     outConnections.add(steamId + " " + steamIdFriend);
  145.                     out.println(steamId + " " + steamIdFriend);
  146.                 }
  147.             }
  148.         }
  149.     }
  150.  
  151.     private void updateGraph() {
  152.         updateOutFile();
  153.         sg = new SymbolGraph("allConnections.txt", " ");
  154.     }
  155.  
  156.     public void printConnections() {
  157.         updateGraph();
  158.  
  159.         In in = new In("allConnections.txt");
  160.  
  161.         // prints the Connections.
  162.         while (in.hasNextLine()) {
  163.             String source = in.readLine();
  164.             String[] twoIds = source.split(" "); // steamId steamId
  165.             StdOut.printf("%s %s\n", twoIds[0], twoIds[1]);
  166.         }
  167.     }
  168.  
  169.     public void printFriendsOf(String steamId) {
  170.         updateGraph();
  171.  
  172.         Graph g = sg.graph();
  173.         StdOut.printf("%-30s\n", steamId);
  174.         for (int w : g.adj(sg.indexOf(steamId))) {
  175.             StdOut.printf("%30s\n", sg.nameOf(w));
  176.         }
  177.     }
  178.  
  179.     public void printSteamIdsAndNumberOfFriends() {
  180.         updateGraph();
  181.  
  182.         Graph g = sg.graph();
  183.         for (int v = 0; v < getV(); v++) {
  184.             StdOut.printf("%-30s has %d friend(s)\n", sg.nameOf(v), g.degree(v));
  185.         }
  186.     }
  187.  
  188.     public void printNamesOfSteamIds(List<String> steamIds) throws SteamApiException {
  189.         // Gets summaries of the players, be it steamIds, personanames, etc.
  190.         GetPlayerSummariesRequest playerSummariesRequest = SteamWebApiRequestFactory
  191.                 .createGetPlayerSummariesRequest(steamIds);
  192.         GetPlayerSummaries processedPlayerSummariesRequest = client
  193.                 .<GetPlayerSummaries>processRequest(playerSummariesRequest);
  194.  
  195.         var response = processedPlayerSummariesRequest.getResponse();
  196.         var players = response.getPlayers();
  197.  
  198.         ListIterator<Player> playersIterator = players.listIterator();
  199.  
  200.         while (playersIterator.hasNext()) {
  201.             var player = playersIterator.next();
  202.             String personaname = player.getPersonaname();
  203.             String steamId = player.getSteamid();
  204.             StdOut.printf("personaname: %-35s steamid: %s\n", personaname, steamId);
  205.         }
  206.     }
  207.  
  208.     public int getV() {
  209.         Graph g = sg.graph();
  210.         return g.V();
  211.     }
  212.  
  213.     public int getE() {
  214.         Graph g = sg.graph();
  215.         return g.E();
  216.     }
  217.  
  218.     public static void run() throws FileNotFoundException, SteamApiException {
  219.         String steamWebApiKey = ""; // go to steam and setup your account with the steam api
  220.         String mySteamId = "76561198004223766";
  221.  
  222.         SteamSnooper s = new SteamSnooper(steamWebApiKey);
  223.         // attempts to open friends of someone
  224.         s.openFriendsOf(mySteamId);
  225.         // attempts to open the friends of every indexed friend
  226.         s.openFriendsOfNotVisited();
  227.         StdOut.println("finished s.openFriendsOfNotVisited()");
  228.         // prints steamId and the connections
  229.         s.printConnections();
  230.         // a private profile
  231.         // String steamId = "";
  232.         // s.printFriendsOf(steamId);
  233.     }
  234.  
  235.     public static void main(String[] args) throws FileNotFoundException, SteamApiException {
  236.         final long startTime = System.currentTimeMillis();
  237.         SteamSnooper.run();
  238.         final long endTime = System.currentTimeMillis();
  239.         System.out.printf("Execution time: %dms", (endTime - startTime));
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement