library(readr)
library(natserv)
library(taxize)

nameList <- read_csv("nameList.csv")
nameList$IDnum <- NULL
nameList$IDnum <- NULL

out2 <- get_natservid(nameList$sciName, ask = FALSE)
out2a <- as.data.frame(out2)
nameList$IDnum <- NULL
nameList$IDnum <- out2a$ids

nameList_clean <- na.omit(nameList)
outputData <- nameList_clean
outputData$rank <- NULL


for (i in 1:nrow(outputData)) {
  s <- try(ns_search_spp(text = as.character(outputData$sciName[i])))
  outputData[i, 'rank'] <- s[["results"]][["roundedGRank"]]
  outputData[i, 'UID'] <- s[["results"]][["uniqueId"]]
}

today <- Sys.Date()
todayFomat <- format(today, "%d %b %Y")
todayFomat1 <- format(today, "%Y-%m-%d")


outputData$statusRank <-
  paste('| status = ',
        outputData$rank,
        sep = "")

outputData$system <- "| status_system = TNC"

outputData$URL <-
  paste(
    '| status_ref = <ref name="TNC status ',
    todayFomat,
    '">{{Cite web|date=',
    todayFomat1,
    ' |url=  https://explorer.natureserve.org/Taxon/',
    outputData$UID,
    "|access-date=",
    todayFomat,
    " |website=NatureServe Explorer ",
    outputData$sciName ,
    "|title = NatureServe Explorer - ",
    outputData$sciName ,
    " |publisher=NatureServe}}</ref>",
    sep = ""
  )
outputData$wikiLink <-
  paste("https://en.wikipedia.org/wiki/",
        outputData$sciName,
        sep = "")

write.csv(outputData, file = "conservationStatusFile.csv")