# get_json_from_spotify_url_OLD.r

#   
#   This is the old version of get_json_from_spotify_url()
#   I am keeping it because it might still be needed for the original "feature playlist" scrape
#   Specifically, the parsing, etc. 
#   
#   I tried to add the iteration here for the "get next items", but it ended up being a sloppy mess 
#   and so I abandoned this one and wrote get_json_from_spotify_url
#   
#   If it turns out that this one is still needed, you will need to check the git commits for 
#   get_playlists.r
#   

if (FALSE)
get_json_from_spotify_url <- function(url, token=get_spotify_API_token(), verbose=TRUE, url_type="", parse=TRUE, warn_for_url_type=TRUE) {
## JSON returns only 100 tracks at a time. Need to iterate. 
## The JSON will have an item labeled "next" usually under "tracks"

  if (!is.character(url))
    stop("url should be a character")
  if (any(grepl("^(https?://)?open.spotify.com", url)))
    warning ("\nit looks like some playlists url inputs are of the form\n\thttps://open.spotify.com/...  -- ie the spotify uri link\nThey should instead be of the form\n\thttps://api.spotify.com/... -- ie the spotify DDEVELOPER API link\n\nParsing will likely fail")

  ## url_type indicates whether we are downloading tracks, playlists or other
  url_type %<>% match_url_type

  token_string <- get_token_string(token, Authorization=FALSE)
  headers <- c(Authorization=token_string)
  raw_json <- RCurl::getURL(url=url, httpheader=headers)

  if (grepl("400 Bad request", raw_json)) {
    warning ("400 Bad Request for url  '", url, "'", call.=FALSE)
    return(setNames(nm=url, obj=raw_json))
  }

  if (!parse)
    return(raw_json)

  browser(expr=inDebugMode("get_json_from_spotify_url"), text="in get_json_from_spotify_url() after getURL before parsing")

  ## Parse the JSON.  If fails, return the raw json
  parsed_json <- try(jsonlite::fromJSON(raw_json))
  if (isErr(parsed_json)) {
    warning ("Failed to parse JSON for url  '", url, "'", call.=FALSE)
    return(setNames(nm=url, obj=raw_json))
  }

  if (length(parsed_json) == 1 && names(parsed_json) == "error") {
    warning ("Curl returned error for  '", url, "'", call.=FALSE)
    print(parsed_json)
    return(raw_json)
  }

  ## Check the expected_fields
  check_expected_fields(parsed_json, url_type=url_type)

  rapply(parsed_json, function(x) length(x))
  has_next <- function(J) sapply(J, function(x) any(unlist(names(x) == "next", recursive=TRUE)))

  wh_has_next <- parsed_json %>% has_next %>% nwhich

  ## If there is a 'next' value, get those
  ## If not, make sure that none of the sub-
  if (length(wh_has_next)) {
    .total <- parsed_json[[wh_has_next]]$total
    url_next <- parsed_json[[wh_has_next]]$`next`

    collection <- parsed_json[wh_has_next]
    while (!is.null(url_next)) {
      json_next <- get_json_from_spotify_url(url_next, url_type=url_type, token=token, parse=FALSE)
      ## should have been parsed
      if (is.char_of_length1(json_next, showWarnings=FALSE))
        try({json_next <- jsonlite::fromJSON(json_next)}, silent=FALSE)
      ## if name is not in there, thats a problem
      if (wh_has_next %ni% names(json_next))
          warning ("\nCheck the JSON for  url  =  '", url_next, "'\nIn the recursive portion of get_json_from_spotify_url() it was expected to find a field named '", wh_has_next, "' but it was not preset\n", call.=FALSE)
      else
        collection <- c(collection)
      url_next <-

      identical(parsed_json[[wh_has_next]], json_next[[wh_has_next]])
      identical(parsed_json[[wh_has_next]])
      json_next[[wh_has_next]]$`next`

    } # // Ends while loop

  #   ## recurse, so long as url_next is not NULL
  #   if (!is.null(url_next)) {
  #     json_next <- get_json_from_spotify_url(url_next)
  #     if (is.char_of_length1(json_next))
  #       try({json_next <- jsonlite::fromJSON(raw_json)}, silent=FALSE)
  #     if (wh_has_next %ni% names(json_next))
  #       warning ("\nCheck the JSON for  url  =  '", url_next, "'\nIn the recursive portion of get_json_from_spotify_url() it was expected to find a field named '", wh_has_next, "' but it was not preset\n", call.=FALSE)
  #     json_next[[wh_has_next]]
  #   }
  # } else 
  # {
  #   sub_has_next <- parsed_json %>% sapply(has_next) %>% unlist
  #   sub_sub_has_next <- parsed_json %>% sapply(sapply, has_next) %>% unlist
  #   sub_has_next %<>% c(sub_sub_has_next)
  #   if (any(sub_has_next)) {
  #     stop (warningCols("None of the immediate sub-elements of parsed_json has a 'next' value.\nHowever there IS a 'next' element in the following element(s)   [dots are '$' to parsed_json]", sub_has_next))
  #   }
  
  } # // end length(wh_has_next)

  ## Grab the next URL
  if (url_type %ni% names(parsed_json)) {
    warning ("url_type, '", url_type, "' is not a name in parsed_json -- NOTE TO SELF. Not sure if this warning is necessary", call.=FALSE)
    url_type <- nwhich(sapply(parsed_json, function(x) "next" %in% names(x)))
  }

  ## Find url_next -- it is either a direct element of the json or of the next layer in
  if (!length(url_type) && "next" %in% names(parsed_json))
    url_next <- parsed_json[["next"]]
  else 
    url_next <- parsed_json[[url_type]]$`next`

  if (!is.null(url_next)) {
    verboseMsg(verbose, sprintf("Pulling next iteration from url '%s' ", url_next), seconds=TRUE)
    return(setNames(nm=c(url, url_next), obj=c(raw_json, get_json_from_spotify_url(url=url_next, token=token, verbose=verbose, url_type=url_type, warn_for_url_type=FALSE))))
    ## confirm track count is less than limit
  } else {
      ## Note that when pulling the original url, the items are under parsed_json$tracks$items, but 
      ##    when pulling any subsequent url, the items are under parsed_json$items
      if (!is.null(parsed_json$tracks$items) && nrow(parsed_json$tracks$items) > parsed_json$tracks$limit)
          warning ("tracks$items count is ", nrow(parsed_json$tracks$items), " for url '", url, "'", call.=FALSE)
      else if (!is.null(parsed_json$items) && nrow(parsed_json$items) > parsed_json$limit)
          warning ("items count is ", nrow(parsed_json$items), " for url '", url, "'", call.=FALSE)
  }
  return(setNames(nm=url, obj=raw_json))
}
