<commit_before>package services.migration.r2ToFlexConversion

import com.lambdaworks.jacks.JacksMapper
import play.Logger


object R2ToFlexAudioConversion {

  def jsonMap(json : String) : Map[String, Any] = {
    JacksMapper.readValue[Map[String, Any]](json)
  }

  def parseLiveData(json : Map[String, Any]) = {
    val parsed = new R2ToFlexAudioConversion(json, true)
    Logger.debug(s"Produced live audio content XML:\n" + parsed.xml.toString())
    parsed
  }
  def parseDraftData(json : Map[String, Any]) = {
    val parsed = new R2ToFlexAudioConversion(json, false)
    Logger.debug(s"Produced draft audio content XML:\n" + parsed.xml.toString())
    parsed
  }
}

class R2ToFlexAudioConversion(jsonMap : Map[String, Any], parseLiveData : Boolean)
  extends R2ToFlexContentConversion(jsonMap, parseLiveData){

  import scala.language.postfixOps

  override lazy val live = getFacetFromMap("live")

  override lazy val draft = getFacetFromMap("draft")

  private def audioDuration : Option[(String, String)] = {
    for( mins <- getAs[Int]("durationMinutes"); secs <- getAs[Int]("durationSeconds"))
      yield (mins.toString, secs.toString)
  }


  private def blockVideoAds : Option[String] =
    getAsString("blockAds").map(_.toLowerCase match {
      case "true" =>  "yes"
      case _ =>       "no"
    })

  private def audioSource = getAsString("source")

  private def getFormatFromFile(path : String) =
    if(path.toLowerCase.endsWith(".mp3")) "audio/mp3"
    else throw new IllegalArgumentException(s"Unrecognised extension ${path}")


  private def mapFilePath(path : String) = "http://static.guim.co.uk/" + path

  private def encodings : List[Map[String, String]] = {
    val encodings = getAs[Map[String, Any]]("audioFile", liveOrDraft)
    encodings.map(e => {    e.get("path").map(p => "url" -> mapFilePath(p.toString)) ++
                            e.get("length").map("sizeInBytes" -> _.toString) ++
                            e.get("path").map(p => "format" -> getFormatFromFile(p.toString))
                          }.toMap).toList
  }

  private def stillImageUrl = getAsString("stillImageUrl")


  private def embeddable = getAsString("embeddable").map(_.toBoolean)


  private def ukOnly = getAsString("ukOnly")

  private def syndicateEncodings = getAsString("syndicateEncodings").map(_.toBoolean)

  private def explicit = getAsString("explicit").map(_.toBoolean).map(_.toString)
  private def clean = getAsString("clean").map(_.toBoolean).map(_.toString)<commit_msg>audio mime type: use audio/mpeg<commit_after>package services.migration.r2ToFlexConversion

import com.lambdaworks.jacks.JacksMapper
import play.Logger


object R2ToFlexAudioConversion {

  def jsonMap(json : String) : Map[String, Any] = {
    JacksMapper.readValue[Map[String, Any]](json)
  }

  def parseLiveData(json : Map[String, Any]) = {
    val parsed = new R2ToFlexAudioConversion(json, true)
    Logger.debug(s"Produced live audio content XML:\n" + parsed.xml.toString())
    parsed
  }
  def parseDraftData(json : Map[String, Any]) = {
    val parsed = new R2ToFlexAudioConversion(json, false)
    Logger.debug(s"Produced draft audio content XML:\n" + parsed.xml.toString())
    parsed
  }
}

class R2ToFlexAudioConversion(jsonMap : Map[String, Any], parseLiveData : Boolean)
  extends R2ToFlexContentConversion(jsonMap, parseLiveData){

  import scala.language.postfixOps

  override lazy val live = getFacetFromMap("live")

  override lazy val draft = getFacetFromMap("draft")

  private def audioDuration : Option[(String, String)] = {
    for( mins <- getAs[Int]("durationMinutes"); secs <- getAs[Int]("durationSeconds"))
      yield (mins.toString, secs.toString)
  }


  private def blockVideoAds : Option[String] =
    getAsString("blockAds").map(_.toLowerCase match {
      case "true" =>  "yes"
      case _ =>       "no"
    })

  private def audioSource = getAsString("source")

  private def getFormatFromFile(path : String) =
    if(path.toLowerCase.endsWith(".mp3")) "audio/mpeg"
    else throw new IllegalArgumentException(s"Unrecognised extension ${path}")


  private def mapFilePath(path : String) = "http://static.guim.co.uk/" + path

  private def encodings : List[Map[String, String]] = {
    val encodings = getAs[Map[String, Any]]("audioFile", liveOrDraft)
    encodings.map(e => {    e.get("path").map(p => "url" -> mapFilePath(p.toString)) ++
                            e.get("length").map("sizeInBytes" -> _.toString) ++
                            e.get("path").map(p => "format" -> getFormatFromFile(p.toString))
                          }.toMap).toList
  }

  private def stillImageUrl = getAsString("stillImageUrl")


  private def embeddable = getAsString("embeddable").map(_.toBoolean)


  private def ukOnly = getAsString("ukOnly")

  private def syndicateEncodings = getAsString("syndicateEncodings").map(_.toBoolean)

  private def explicit = getAsString("explicit").map(_.toBoolean).map(_.toString)
  private def clean = getAsString("clean").map(_.toBoolean).map(_.toString)
{'fraction_non_alphanumeric': 0.13412935323383085, 'fraction_numerical': 0.0029850746268656717, 'mean_word_length': 3.832692307692308, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '983414', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1576, 'n_words': 319}