## get_known_users.r

if (FALSE) 
{
  DT.usermeta <- get_all_known_users()
}
get_all_known_users <- function(jesus=TRUE, verbose=TRUE, load_from_backup=TRUE) {

  if (!exists("DT.playlist_metadata"))
    stop("DT.playlist_metadata cannot be found.\nget_all_known_users() (which itself is generally used to create DT.usermeta) depends on it.", call.=FALSE)

  user.id.all <- DT.playlist_metadata[, c(playlist.owner.id, playlist.track.addedby.id)] %>% unique %>% removeNA %>% {.[. != '']}

  ## have "ours" go first
  if (exists("DT.ours")) {
    user.id.ours <- DT.ours[order(perc, decreasing=TRUE), unique(owner.id)] %>% removeNA %>% {.[. != '']}
    user.id.all %<>% c(user.id.ours, .) %>% unique
  }

  L.before <- length(user.id.all)
  verboseMsg(verbose, "There are", L.before, " user ids to process")

  if (!exists("DT.usermeta") && load_from_backup) {
    loadFromJesus("DT.usermeta", envir=globalenv())
  }
  if (exists("DT.usermeta")) {
    user.id.all %<>% setdiff(DT.usermeta$user.id)
  }

  L.after <- length(user.id.all)
  verboseMsg(verbose && (L.after != L.before), "There are", L.after, "remaining ids to process after checking against existing")

  ## remove blanks again
  user.id.all %<>% removeNA %>% {.[. != '']}


  ## expand to URI
  user.uri.all <- get_user_uri_from_id(user.id.all)

  ## get the data for each one
  ## This just calls get_user_data(user_uri), but iterates using lapply and checks for error using valueIfErr
  ll.DT.usermeta <- lapply(user.uri.all, function(uri) valueIfErr(get_user_data(uri=uri, jesus=FALSE, gitcommit=FALSE), {warning("Could not get user data for '", uri, "'", call.=FALSE); NULL}))

  ## Add the original one 
  if (exists("DT.usermeta"))
    ll.DT.usermeta <- c(list(DT.usermeta), ll.DT.usermeta)

  ## flatten
  DT.usermeta <- rbindlist(ll.DT.usermeta, fill=TRUE, use.names=TRUE)


  if ("user.followers.href" %in% names(DT.usermeta) && all(is.na(DT.usermeta$user.followers.href)))
    DT.usermeta[, user.followers.href := NULL]
  
  if (jesus) {
    e <- environment()
    jesusForData(DT.usermeta, envir=e, verbose=TRUE) %>% print
  }

  return(DT.usermeta)
}



if (FALSE) {
  userid <- "121788790"
  get_list_of_playlists_for_user(userid)
}

get_list_of_playlists_for_user <- function(userid) {
  if (length(userid) > 1)
    stop ("for now, just one userid at a time")

  stop ("This function is not completed")
  
  url <- userid %>% sprintf('https://api.spotify.com/v1/users/%s/playlists', .)
  raw_json <- lapply(url, get_json_from_spotify_url, url_type="list_of_items")

}

##   Getting      users data for URL = 'https://api.spotify.com/v1/users/1113004802'         -- [06:11:38 EDT]
##   
##   $display_name
##   [1] "Radial France"
##   
##   $external_urls
##   $external_urls$spotify
##   [1] "https://open.spotify.com/user/1113004802"
##   
##   
##   $followers
##   $followers$href
##   NULL
##   
##   $followers$total
##   [1] 3327
##   
##   
##   $href
##   [1] "https://api.spotify.com/v1/users/1113004802"
##   
##   $id
##   [1] "1113004802"
##   
##   $type
##   [1] "user"
##   
##   $uri
##   [1] "spotify:user:1113004802"
##   
##   $api.timestamp
##   [1] "2015-06-15T10:11:38Z"


