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.

126 lines
2.9 KiB

  1. package net.jrtechs.www.webCrawler;
  2. import net.jrtechs.www.model.Player;
  3. import net.jrtechs.www.utils.FileReader;
  4. import net.jrtechs.www.utils.WrappedFileWriter;
  5. import org.json.JSONArray;
  6. import org.json.JSONObject;
  7. import java.io.File;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import java.util.List;
  12. /**
  13. * File which handles the file IO for storing
  14. * all the players on the HHD
  15. *
  16. * @author Jeffery Russell 11-18-18
  17. */
  18. public class FileIO
  19. {
  20. /** Base directory to store all the data */
  21. private String baseFilePath;
  22. /**
  23. * Initalizes the base directory
  24. * @param basePath
  25. */
  26. public FileIO(String basePath)
  27. {
  28. this.baseFilePath = basePath;
  29. }
  30. /**
  31. * Helper function to piece together the naming convention
  32. * for the JSON file.
  33. *
  34. * @param id player id
  35. * @return path of the file being saved
  36. */
  37. private String getURL(String id)
  38. {
  39. return baseFilePath + id + ".json";
  40. }
  41. /**
  42. * Determines if we already have the player
  43. * on disk.
  44. *
  45. * @param id steam id of the player
  46. * @return if the file exists on disk
  47. */
  48. public boolean playerExists(String id)
  49. {
  50. String fileName = baseFilePath + id + ".json";
  51. return new File(fileName).isFile();
  52. }
  53. /**
  54. * Returns the date in a form which is easy to read and write
  55. * to from a file.
  56. *
  57. * @return
  58. */
  59. private String getDate()
  60. {
  61. String pattern = "yyyy-MM-dd";
  62. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
  63. return simpleDateFormat.format(new Date());
  64. }
  65. /**
  66. * Reads all the friends from a player on the disk
  67. *
  68. * ** This should only be called if we know the player
  69. * is stored on the disk.
  70. *
  71. * @param id steam id of the player
  72. * @return list of all their friends.
  73. */
  74. public List<String> readFriends(String id)
  75. {
  76. String fileContents = FileReader.readFile(this.getURL(id));
  77. JSONObject player = new JSONObject(fileContents);
  78. if(player.has("friends"))
  79. {
  80. List<String> list = new ArrayList<>();
  81. JSONArray jsonArray = player.getJSONArray("friends");
  82. for(int i = 0 ; i < jsonArray.length();i++)
  83. {
  84. list.add(jsonArray.getString(i));
  85. }
  86. return list;
  87. }
  88. return new ArrayList<>();
  89. }
  90. /**
  91. * Writes the player to the file.
  92. *
  93. * @param player
  94. */
  95. public void writeToFile(Player player, List<String> friendIDS)
  96. {
  97. JSONObject object = new JSONObject();
  98. object.put("name", player.getName());
  99. object.put("date", getDate());
  100. object.put("friends", friendIDS);
  101. String fileName = baseFilePath + player.getId() + ".json";
  102. WrappedFileWriter.writeToFile(object.toString(4), fileName);
  103. }
  104. }