## This file only downloads the RAW JSON for each datetime-country's FEATURED PLAYLISTS; 
##   ...... and at that, only the first one. No pagination.
##
## The raw json contains a list of which playlists were featured that datetime in that country
## (pagination contains the other lists, if there are more)
##
## We still need to
##   (1) Parse the data
##   (2) Get Paginated when necessary
##   (3) Extract list of playlists
##   (4) Get data for those playlists


stop ("Dont source this file directly")
source("~/git/orch/src/Spotify Playlist Webscrape/00 Featured Playlist Setup.r")


last_known_date_downloaded <- as.Date("2015-05-30")

rm(datetimes_using, dates_using, startingDates, startingTimes)
{
  startingDates <- seq.Date(as.Date("2013-12-31"), length.out=7, by="+1 day")
  startingTimes <- sprintf("%02i:00:00 UTC", c(0, 3, 7, 7, 10, 20))

  ## TEMP
  # startingDates <- startingDates[startingDates != "2014-01-03"]
  dates_using <- lapply(startingDates, seq.Date, to=today(), by="+1 week") %>% unlistDateTime %>% sort

  datetimes_using <- lapply(startingTimes, function(tim) paste(dates_using, tim)) %>% unlist %>% as.POSIXct
  selfname_(datetimes_using)
  datetimes_using <- as.iso8601(datetimes_using) %>% sort(decreasing=TRUE)

  datetimes_using <- datetimes_using[datetimes_using > last_known_date_downloaded]
}

for (dte in datetimes_using) { 
  stop("2015-06-15 -- see note in get_featured_playlists()")
  for (country in countries_using) {
    try({
      dte.clean <- cleanString.iso8601(dte)
      datestamp <- format(as.Date(dte), "%Y%m%d")

      ## Check if already downloaded
      existing_files <- dir(data.p(subfolder_for_featuredplaylists, datestamp, dte.clean))
      pat.filename <- sprintf("featuredPlaylists+country_%s+timestamp_%s+part_%02i", if(nchar(country)) country else "GLOBAL", dte.clean, 1)
      if (any(grepl(pat.filename, existing_files, fixed=TRUE))) {
        verboseMsg(verbose, sprintf("Skipping featured playlists for country = '%s'  &  date = %s", country, dte), seconds=TRUE, func="message", call.=FALSE)
        next;
      }

      ## PARSE                     
      verboseMsg(verbose, sprintf("Getting  featured playlists for country = '%s'  &  date = %s", country, dte), seconds=TRUE)

      ## refresh token if needed
      if (token$time_end < now() + 300)
        token <- get_spotify_API_token()
      ## Pull raw jsons
      # jsons <- get_url_for_spotify_playlist_data(user_id=user_id, playlist=playlist, token=token, verbose=FALSE)
      jsons <- get_featured_playlists(token=token, country=country, timestamp=dte)
      if (is.null(country))
          country <- "GLOBAL"
      if (!length(jsons))
          warning (sprintf("No JSON downloaded for country = '%s'  &  date = %s", country, as.character(dte)), call.=FALSE)

      file_nms <- sprintf("featuredPlaylists+country_%s+timestamp_%s+part_%02i_of_%02i", if(nchar(country)) country else "GLOBAL", dte.clean, seq_along(jsons), length(jsons))
      files_out <- data.p(subfolder_for_featuredplaylists, datestamp, dte.clean, file_nms, ext=".json")

      if (!file.exists(dirname(files_out)))
        dir.create(dirname(files_out), recursive=TRUE, showWarnings=FALSE)

      ## Write to disk.  use mapply if there is more than one JSONS
      if (length(jsons) > 1) {
          cat("  ^^^^^^^ ~~~~~~~~~~~~~~~~~~~ {{  Using mapply }} ~~~~~~~~~~~~ ^^^^^^^ \n")
          mapply(writeLinesUTF8, text=jsons, con=files_out)
      } else
          writeLinesUTF8(text=jsons, con=files_out)
    })
  }
}






