# Can we identify when a user added a song to their collection?  
# Then what?   
# 
# How often do they listen in the collection?
# how often do they listen in a playlist or other? 


setScience("Spotify_Stream_Count", create=TRUE, load=FALSE, subl=FALSE)
sourceSupportFns(srcOther("Spotify Playlist Webscrape"))

run_the_query <- TRUE


tbl <- "staging_raw_spotify_v2"
schema <- "production"
cluster <- 9


headDB("staging_raw_spotify_v2") %>% print

runQry("SELECT DISTINCT user_access FROM staging_raw_spotify_v2 limit 100", all.pfm=T)

colsToPull <- c("download_date"
              , "source", "source_uri"
              , "user_id", "user_access", user_account_type="user_type"
              , "gender", "user_country", "birth_year", "device_type", "os"
              ## TRACK INFO
              , "upc", "isrc"
              , "track_uri"  ## we are going to want UPC, ISRC and Milliseconds
              ) #"upc", "track_album_artist", "isrc")
minDate <- NULL
dateCol <- "download_date"
whereIn <- NULL

bucket <- s3_p("spotify", "v2", "streams_per_user_per_track")

if (run_the_query && !exists("DT.user_stream_counts_by_source")) {
        makeQry(tbl=tbl
              , schema=schema
              , colsToAgg=c(streams="*")
              , colsToPull=colsToPull
              , whereIn=whereIn
              , minDate=minDate
              , dateCol=dateCol
              , limit = NULL
            ) %>% 
        unloadQry(bucket=bucket)
}
