
## ------------------------ ##
##         START HERE       ## 
## ------------------------ ##

## source("~/git/orch/src/Spotify Playlist Webscrape/00 Featured Playlist Setup.r")

setScience("Spotify Playlist Webscrape", create=TRUE, subl=FALSE, load=FALSE)
sourceSupportFns(proj="Spotify_Metadata_2016", recursive=TRUE)

options(warn = 1)
options(verbose_minw = 99)

wh <- getWH_already_on(default="cron_jobs_large")
dbname <- "prod"
setSnowflake(wh=wh, dbname=dbname)

verbose <- TRUE
TEST_RUN <- TRUE

subfolder_for_featuredplaylists <- "featured_playlists_raw_json"

## for each uri, write the output to file
token <- get_spotify_API_token()

if (!file.exists(data.p(subfolder_for_featuredplaylists)))
  dir.create(data.p(subfolder_for_featuredplaylists), showWarnings=FALSE, recursive=TRUE)


if (TEST_RUN) {
  loadFromJesus("country_codes.spotify", dont.fail=TRUE)
} else {
  s.t({countries_using <- getCountryCodes(cluster=4, V2=TRUE, refresh=FALSE, snowflake_inuse=getOption("snowflake_inuse", FALSE))}, title="refresh list of distinct Country Codes used in the Spotify SoS table")
  selfname_(countries_using)
  countries_using <- c(countries_using, "") %>% unique %>% sort
  
  ## UPATE 2015-11-28 --- names get lost after taking "unique";  I am not sure if this will introduce any bugs
  selfname_(countries_using)

  country_codes.spotify <- copy(countries_using)
  jesusForData(country_codes.spotify)
}
