  source("~rsaporta/git/misc/rscripts/utilsRS.r")
  .us()
  fresh()

  science_error <- try(setScience("Spotify_Streams_Per_Top_Artist", create=FALSE, subl=FALSE, load=FALSE), silent=TRUE)

  if (isErr(science_error)) {
    projName <- "Spotify_Streams_Per_Top_Artist"
    dataDir <- "~rsaporta/git/orch/data/Spotify_Streams_Per_Top_Artist"
    srcDir <- "~rsaporta/git/orch/src/Spotify_Streams_Per_Top_Artist"
    outDir <- "~rsaporta/git/orch/out/Spotify_Streams_Per_Top_Artist"
  }
  if (!exists("DT.nms"))
    loadFromJesus(file="~rsaporta/git/orch/data/Spotify_Streams_Per_Top_Artist/DT.nms+with raw data-20150113_1051-5310x5.RDS")

{

  stopifnot(require(rjson))

  times_info <- "Local_Box"
  times_info <- "Rbox_original"
  times_info <- "Rbox_new"
  times <- list()
  i <- 0

  for (iter in 1:5) {
    suppressWarnings(rm(errors))
    message("Iteration # ", iter)

    i <- i + 1
    times[[i]] <- list()


    times[[i]][["Info"]] <- times_info
    times[[i]][["Start"]] <- now()

    tracks.flat <- unlist(DT.nms[date != Sys.Date() & substr(ret_json, 1, 27) != "<!DOCTYPE html>\n<html lang=", 
                      setNames(nm=nms, obj=ret_json)])
    if (!exists("errors") || is.null(errors)) {errors <- character()}


    times[[i]][["Unlisting"]] <- now()


    ## There is an issue of un-escaped quotes
    tracks.flat <-  gsub('Axwell /. Ingrosso', 'Axwell Ingrosso',  tracks.flat)
    tracks.flat <-  gsub('Nat "King" Cole', 'Nat \\\\"King\\\\" Cole',  tracks.flat)
    tracks.flat <-  gsub('Immortals - From "Big Hero 6.', 'Immortals - From \\\\"BigHero6\\\\"',  tracks.flat)
    tracks.flat <-  gsub('From "The Hunger Games: Mockingjay Part 1" Soundtrack', 'From \\\\"The Hunger Games: Mockingjay Part 1\\\\" Soundtrack',  tracks.flat)
    tracks.flat <-  gsub('From "The Hunger Games: Catching Fire" Soundtrack', 'From \\\\"The Hunger Games: Catching Fire\\\\" Soundtrack',  tracks.flat)
    tracks.flat <-  gsub('"Elastic Heart . From "The Hunger Games. Catching Fire".Soundtrack"', '"Elastic Heart - From \\\\"The Hunger Games: Catching Fire\\\\"/Soundtrack"',  tracks.flat)
    tracks.flat <-  gsub('"Happy . From "Despicable Me 2""', '"Happy - From \\\\"Despicable Me 2\\\\""',  tracks.flat)
    tracks.flat <-  gsub('"Happy .From "Despicable Me 2"."', '"Happy (From \\\\"Despicable Me 2\\\\")"',  tracks.flat)
    tracks.flat <-  gsub('""Riptide""', '"\\\\"Riptide\\\\""',  tracks.flat)
    tracks.flat <-  gsub('""TIME FLIES" EP 2010"', '"\\\\"TIME FLIES\\\\" EP 2010"',  tracks.flat)
    tracks.flat <-  gsub('"Back in Time . featured in "Men In Black 3""', '"Back in Time - featured in \\\\"Men In Black 3\\\\""',  tracks.flat)
    tracks.flat <-  gsub('The" Fifty Shades Of Grey" Soundtrack', 'The\\\\" Fifty Shades Of Grey\\\\" Soundtrack',  tracks.flat)


    tracks.flat <-  gsub('Dian Shi Ju "Yi Jian Bu Zh?ong Qing" ', 'Dian Shi Ju \\\\"Yi Jian Bu Zhong Qing\\\\" ', tracks.flat)
    # tracks.flat <-  gsub('Dian Shi Ju "Yi Jian Bu Zhong Qing" Cha Qu', 'Dian Shi Ju \\\\"Yi Jian Bu Zhong Qing\\\\" Cha Qu', tracks.flat)
    # tracks.flat <-  gsub('Dian Shi Ju "Yi Jian Bu Zong Qing" Pian Wei', 'Dian Shi Ju \\\\"Yi Jian Bu Zong Qing\\\\" Pian Wei', tracks.flat)
    tracks.flat <-  gsub('劇"一見不鍾情"', '劇\\\\"一見不鍾情\\\\"', tracks.flat)
    # tracks.flat <-  gsub('劇"一見不鍾情" 插', '劇\\\\"一見不鍾情\\\\" 插', tracks.flat)
    # tracks.flat <-  gsub('劇"一見不鍾情"片',  '劇\\\\"一見不鍾情\\\\"片',  tracks.flat)



    tracks.flat <-  gsub('From "Teenage Mutant Ninja Turtles""', 'From \\\\"Teenage Mutant Ninja Turtles\\\\""',  tracks.flat)
    tracks.flat <-  gsub('"Tito "El Bambino" El Patr', '"Tito \\\\"El Bambino\\\\" El Patr',  tracks.flat)
    tracks.flat <-  gsub('"Black Pearl "He\'s A Pirate"', '"Black Pearl \\\\"He\'s A Pirate\\\\"',  tracks.flat)
    tracks.flat <-  gsub('"Never Seen Anything "Quite Like You""', '"Never Seen Anything \\\\"Quite Like You\\\\""',  tracks.flat)
    # tracks.flat <-  gsub('', '',  tracks.flat)
    # tracks.flat <-  gsub('', '',  tracks.flat)


    times[[i]][["Gsubbing"]] <- now()


    DTs.list <- emptylist(tracks.flat)

    verboseMsg(verbose, "Beginning Parsing", time=TRUE)
    for (tx.nm in names(tracks.flat[!is.na(tracks.flat)])) {
      cat("\n  Parsing ", tx.nm, "")
      tx <- tracks.flat[[tx.nm]]
      txl <- try( fromJSON(tx)[["tracks"]] )
      if (isErr(txl))  {
        warning ("ERROR: ", tx.nm)
        errors <- c(errors, tx.nm)
      } else
        DTs.list[[tx.nm]] <-  do.call(rbind, lapply(txl, function(x) {x[sapply(x, is.null)] <- ""; as.data.table(x)} ))
    }
    errors <- unique(errors)

    verboseMsg(verbose, "\nDone Parsing", time=TRUE)


    times[[i]][["end"]] <- now()

  }

  jesusForData(times, info=times_info)
}

if (FALSE)
{
  lib(ggplot)
  TIMES <- list()
  files <- c("~rsaporta/git/orch/data/Spotify_Streams_Per_Top_Artist/times+Rbox_new-20150116_1754-5_len.RDS"
            , "~rsaporta/git/orch/data/Spotify_Streams_Per_Top_Artist/times+Rbox_original-20150113_1310-5_len.RDS"
            )
  for (f in files) {
    loadFromJesus(f, over=TRUE)
    TIMES <- c(TIMES, times)
  }
  DT.Times <- rbindlist(lapply(TIMES, as.data.table))
  DT.Times[, TotalTime := end - Start]

  Plot.rbox_comparison <- qplot(x=DT.Times$Info, fill=DT.Times$Info, color=DT.Times$Info, y=as.numeric(DT.Times$TotalTime), geom=c("boxplot")) +  geom_jitter(color="black") + ylim(4.5, 10) + labs(x="", y="Time to Complete Task", title="New RBox vs. Old Rbox\n-----------------------\nSmaller Values are better\n") + nolegend()
  ggsave.out(plot=Plot.rbox_comparison, outDir=plots.p(proj="scratch"))

}

notify("Done with Time Test")
