readSpotifyFiles <- function(files, append.file_name=FALSE, append.content_owner=TRUE, verbose=FALSE) {
## Reads the file, appends the date, extracted from file title
## Checks if iris, if so, extracts date from header info

  if (!length(files))
    stop("the 'files' argument sent to readSpotifyFiles() is empty.\n\nHint: Check the extract() statements in file  ", src.p('02 Ingest Spotify Files.r'))


  cat(sprintf("Processing '%s' (%i files)\n", capture.output(substitute(files)), length(files)))

  ## Files on or after Oct 2015 have one additional header line
  files_to_skip_one_line <- files[dateFromSpotifyFile(files) >= "2015-10-01" & grepl("revshare", basename(files))]

  ret <- 
    do.call(rbind, c(lapply(files, function(f) {
        verboseMsg(verbose, f, minw=75)

        skip <- 0 + (f %in% files_to_skip_one_line)

        ret2 <- fread(f, skip=skip, header=TRUE)


        ## This one file has a bad date col, arg!
        if (basename(f) == "spotify-revshare-for-theorchard-20112.txt") {
          ret2[`Report start date` == "11/01/02", `Report start date` := "2011-02-01"]
          ret2[`Report end date`   == "11/01/02", `Report end date`   := "2011-02-01"]
        }

        ## Ensure that "Product" is character
        if ("Product" %in% names(ret2) && !is.character(ret2[["Product"]]))
          ret2[, Product := as.character(Product)]

        if ("Total tracks" %in% names(ret2))
          ret2[, ("Total tracks") := as.character(`Total tracks`)]

        ## Extract header info
        header.info <- extractReportInfo(f, skip=skip, nrows=1, header=TRUE, justDateCols=TRUE, prepend.Report=TRUE)

        ret2[, date_file := dateFromSpotifyFile(f)]

        if (nrow(header.info) && all(names(header.info) %ni% names(ret2)))
          ret2[, names(header.info) := as.list(header.info)]

        if (append.content_owner)
          ret2[, content_owner := extractContentOwnerFromSpotifyFile(f)]

        if (append.file_name)
          ret2[, file := basename(f)]

        ret2
      })
    , fill=TRUE))

  ## There is more cleaning to be done
  ##   which will be handled by a separate function
  simpleClean_of_spotify_DT_(ret)
}

extractContentOwnerFromSpotifyFile <- function(f, orchard_equivalent) {
  pat.for <- "for\\-?.+?\\b"

  ## Alternative is to read the header information in the contents of the file and look for a recepient field
  ## This of course only works for flat text files, where there is a recepient column
  if (FALSE)
    extractReportInfo(f, skip=0, nrows=1, header=TRUE, justDateCols=FALSE, prepend.Report=TRUE) %>% { .[["Recipient"]] } %>% valueIfNull(NA_character_)  %>% removeNA(repl="orchard") %>% removeText("the", .) %>% stylizeContentOwner

  ## Instead, simply extract the information from the file name.
  f %>% basename %>% tolower %>% removeText(pat="for-downloads", .) %>% {stringr::str_extract(., pat=pat.for)} %>% removeText("for\\-?", .) %>% removeNA(repl="orchard") %>% removeText("the", .) %>% stylizeContentOwner
}

