def download()

in download/src/main/scala/com/gu/footballtimemachine/Download.scala [33:63]


  def download(matchId: String): Future[Unit] = Future {

    val sdf = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss")
    val xmlPP = new PrettyPrinter(500, 2)

    List("info", "events").foreach { fileType =>
      val s3Path = s"match/$fileType/apiKey/$matchId"
      val versions = s3Client.listVersions(bucket, s3Path).getVersionSummaries.asScala.toList.sortBy(_.getLastModified)

      versions.foreach { version =>

        val gor = new GetObjectRequest(bucket, s3Path)
        gor.setVersionId(version.getVersionId)

        val s3Object = s3Client.getObject(gor)

        val content = xmlPP.format(XML.load(s3Object.getObjectContent))

        val dateFormat = sdf.format(version.getLastModified)

        val file = new File(s"files/$matchId/$dateFormat-$fileType.xml")
        file.getParentFile.mkdirs()

        val writer = new PrintWriter(file.getAbsolutePath)
        writer.write(content)
        writer.close()

        println(s"Downloaded $file")
      }
    }
  }