-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVegasOdds.R
71 lines (64 loc) · 1.66 KB
/
VegasOdds.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
stripRank <- function(name)
{
if(substr(name, 1, 1) != '#')
{
return(name)
}
pos <- regexpr(" ", name);
substr(name, pos+1, nchar(name))
}
processOddsHtml <- function(data)
{
odds <- NULL
game <- ''
Home <- ''
Away <- ''
for(i in 1:length(data))
{
row <- data[[i]]
classes <- xmlGetAttr(row, "class")
if(grepl(classes, "stathead"))
{
# New game
game <- xmlValue(row)
# strip ' - ...'
game <- substr(game, 0, regexpr(" - ", game)-1)[[1]]
Home <- unlist(strsplit(game, ' at '))[2]
Home <- stripRank(Home)
Away <- unlist(strsplit(game, ' at '))[1]
Away <- stripRank(Away)
}
else if(grepl(classes, "colhead"))
{
# Contains labels
}
else
{
vals <- xmlChildren(row)
if(length(vals) == 4)
{
Source <- xmlValue(vals[[1]])
#Spread <- xmlValue(vals[[2]])
Spread <- xpathSApply(vals[[2]],".//text()", xmlValue)[1]
#OU <- xmlValue(vals[[3]])
OU <- xpathSApply(vals[[3]],".//text()", xmlValue)[1]
OU <- gsub(" O/U", "", OU)
odds <- rbind(odds, data.frame(Source=Source, Home=Home, Away=Away, Spread=Spread, OU=OU))
}
}
}
odds$Spread[odds$Spread == "EVEN"] <- 0
odds$Spread <- as.numeric(odds$Spread)
odds <- substituteNames(odds, "Home")
odds <- substituteNames(odds, "Away")
odds
}
# type = 'nfl' 'ncf'
getOddsData <- function(type)
{
url <- paste('http://espn.go.com/', type, '/lines', sep='')
html <- htmlTreeParse(getURL(url), useInternalNodes=T)
data <- xpathApply(html, '//*[@id="my-teams-table"]/div/div[1]/table/tr')
processOddsHtml(data)
}
test1 <- getOddsData('nfl')