# Here is the Spotify analytics page for the track Evolution of the Innocent by Bart Wirtz- featuring Richie Goods.
# 1 Million + views over the last year
# ISRC NLB151100682
# Album iDreamer UPC 0608917332025


  .LOAD_FROM_BACKUP <- FALSE

  setScience("Spotify Playlist Webscrape", create=TRUE, subl=FALSE, load=.LOAD_FROM_BACKUP)
  options(warn = 1)
  options(verbose_minw = 80)
  verbose <- TRUE

  isrc.bart <- "NLB151100682"
  upc.bart <- 0608917332025
  tname.bart <- 'Evolution Of The Innocent'

  loadFromJesus("DT.pl")

  DT.pl[upc ==  as.numeric(upc.bart)]

  uri.bart <- DT.pl[track.name == tname.bart, uri]

  DT.orch_isrc_upc <- get_DT.orch_isrc_upc(refresh=FALSE)

  colsToPull.s2 <- c("download_date", "upc", "album_name", "isrc", "track_name", "source", "source_uri", "user_access", "user_country", "user_region", "partner", "product", "referral", "os", "device_type")
  DT.bart <- makeQry(tbl="staging_raw_spotify_v2", schema="production", colsToPull=colsToPull.s2, limit=NULL, whereIn=list(upc =upc.bart, track_name=tname.bart, conjunction="OR")) %>%
              runQry(cluster=9)

  DT.bart_wirtz_top_sources <- DT.bart[track_name == tname.bart, .N, keyby=list(source, source_uri)] [N > 100][order(N, decreasing=TRUE)]
  writeDT(DT.bart_wirtz_top_sources, to=c("Ben Oldfield"="boldfield@theorchard.com"))

  if (!exists("DT.pl")) loadFromJesus("DT.pl")
  DT.pl[.(uri.bart), list(EotI.popularity = track.popularity[track.name == tname.bart], Avg.popularity_playlist = round(mean(track.popularity))), keyby=list(uri, name, total_tracks_in_playlist)][, EOI_of_TOTAL := EotI.popularity / Avg.popularity_playlist][]



#  [1] "album_code"         "album_name"         "birth_year"         "cached"             "device_type"
#  [6] "download_date"      "gender"             "isrc"               "length"             "message"
# [11] "offline_timestamp"  "os"                 "partner"            "product"            "referral"
# [16] "row_id"             "source"             "source_uri"         "tmstamp"            "track_album_artist"
# [21] "track_artists"      "track_id"           "track_message"      "track_name"         "track_uri"
# [26] "upc"                "user_access"        "user_country"       "user_id"            "user_message"
# [31] "user_region"        "user_type"          "version"            "zipcode"