# Write a program in several steps to graph the pairs
#   (rushing yardage, receiving yardage)
# across the 32 NFL teams.
# 
# 1. Start by reading a single team's statistics page.
# 2. Start again by getting a vector of links to team websites
#    (e.g. "https://www.packers.com/").
# 3. Combine (1) and (2), enclosing (1) in a loop.

rm(list=ls())

if (!require("XML")) { # for readHTMLTable()
    install.packages("XML")
    stopifnot(require("XML"))
}
if (!require("curl")) { # for curl()
    install.packages("curl")
    stopifnot(require("curl"))
}

TEAMS = "https://www.nfl.com/teams"
## (With browser, view page source (HTML) for TEAMS page, search for
## "packers", note "data-link_url" that distinguishes the second occurrence.)

lines = readLines(TEAMS) # many lines of HTML
team.line = grep(pattern='data-link_url="http.*://www', x=lines, value=TRUE)
team.lines.list = strsplit(x=team.line, split='CTA View Full Site\" data-link_url=\"')
team.lines = team.lines.list[[1]]
team.lines = team.lines[-1] # exclude first line
team.links = sub(pattern='(https://www[a-z0-9.]+)/".*', replacement="\\1", x=team.lines)

## Get best rusher's yardage and best receiver's yardage for each team.
n.teams = length(team.links)
rushing = numeric(n.teams) # 32 zeros
receiving = numeric(n.teams)
for (i in seq_len(n.teams)) {
    ## assemble a link like "https://www.packers.com/team/stats/2021/REG"
    link = paste0(team.links[i], "/team/stats/2021/REG")
    tables = readHTMLTable(readLines(curl(link), warn=FALSE))
    rushing.table.number = 8 # The rushing table is #8 for some teams,
    rushing[i] = sum(as.numeric(tables[[rushing.table.number]][ , 3]))
    receiving.table.number = rushing.table.number + 1
    receiving[i] = sum(as.numeric(tables[[receiving.table.number]][ , 3]))
    cat(sep="", "i=", i, ", link=", link, ", rushing=", rushing[i],
        ", receiving=", receiving[i], "\n")
}

print(rushing)
print(receiving)
