Extract.scala 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package extract
  2. import balldontlie._
  3. import game.{Team, PlayerGameStats}
  4. import upickle.default._
  5. object Extract {
  6. def writeOrReadBack[T: ReadWriter](
  7. localCopy: os.Path,
  8. getRemoteData: () => T
  9. ): T = {
  10. if (!os.exists(localCopy)) {
  11. val data = getRemoteData()
  12. Utils.writeToFile[T](data, localCopy)
  13. data
  14. } else {
  15. println(s"file $localCopy already exists, reading back from it")
  16. Utils.readFromFile[T](localCopy)
  17. }
  18. }
  19. def getTeams(local: os.Path, filterTeams: Team => Boolean) = {
  20. def getData() = {
  21. EndpointResponse
  22. .getTeams()
  23. .filter(filterTeams)
  24. }
  25. writeOrReadBack(local, getData)
  26. }
  27. def getGames(local: os.Path, season: Int, teamIds: List[Int]) = {
  28. def getData() = {
  29. EndpointResponse.getGames(2021, teamIds)
  30. }
  31. writeOrReadBack(local, getData)
  32. }
  33. def getStats(local: os.Path, gameIds: List[Int]) = {
  34. if (!os.exists(local)) {
  35. val data = EndpointResponse.getStats(gameIds)
  36. Utils.writeToFile(PlayerGameStats.as_json_value(data), local)
  37. data
  38. } else {
  39. println(s"file $local already exists, reading back from it")
  40. Utils.readFromFile[List[PlayerGameStats]](local)
  41. }
  42. }
  43. }