Skip to content

Commit 16fe861

Browse files
authored
Merge pull request #158 from FantasyFootballAnalytics/add-fanduel
Added FanDuel scrape
2 parents 42e644d + abe8ba5 commit 16fe861

3 files changed

Lines changed: 342 additions & 3 deletions

File tree

R/scrape_funcs.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#' @param src the sources that data should be scraped from should be one or more
88
#' of \code{c("CBS", "ESPN", "FantasyData", "FantasyPros", "FantasySharks",
99
#' "FFToday", "FleaFlicker", "NumberFire", "Yahoo", "FantasyFootballNerd", "NFL",
10-
#' "RTSports","Walterfootball")}
10+
#' "RTSports","Walterfootball", "FanDuel)}
1111
#' @param pos the posistions that data should be scraped for. Should be one or more
1212
#' of \code{c("QB", "RB", "WR", "TE", "K", "DST", "DL", "LB", "DB")}
1313
#' @param season The seaon for which data should be scraped. Should be set to the
@@ -18,7 +18,7 @@
1818
scrape_data <- function(
1919
src = c("CBS", "ESPN", "FantasyPros", "FantasySharks", "FFToday",
2020
"FleaFlicker", "NumberFire", "FantasyFootballNerd", "NFL",
21-
"RTSports", "Walterfootball"),
21+
"RTSports", "Walterfootball", "FanDuel"),
2222
pos = c("QB", "RB", "WR", "TE", "K", "DST", "DL", "LB", "DB"),
2323
season = NULL, week = NULL, ...){
2424

@@ -32,7 +32,19 @@ scrape_data <- function(
3232
src = match.arg(src, several.ok = TRUE,
3333
c("CBS", "ESPN", "FantasyData", "FantasyPros", "FantasySharks", "FFToday",
3434
"FleaFlicker", "NumberFire", "FantasyFootballNerd", "NFL",
35-
"RTSports","Walterfootball"))
35+
"RTSports", "Walterfootball", "FanDuel"))
36+
37+
# Check for NumberFire in src and convert to FanDuel
38+
if("NumberFire" %in% src) {
39+
message("\nHeads up! NumberFire is now FanDuel... Using FanDuel for scrape")
40+
41+
src <- src[src != "NumberFire"]
42+
43+
if(!("FanDuel" %in% src)) {
44+
src <- c(src, "FanDuel")
45+
}
46+
}
47+
3648
pos = match.arg(pos, several.ok = TRUE,
3749
c("QB", "RB", "WR", "TE", "K", "DST", "DL", "LB", "DB"))
3850

R/source_objects.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,54 @@ fleaflicker_columns = c(
464464
"Availability % Own" = "pct_owned"
465465
)
466466

467+
# FanDuel columns ----
468+
fanduel_columns <- c(
469+
"player_numberFireId" = "src_id",
470+
"player_name" = "player",
471+
"player_position" = "pos",
472+
"team_numberFireId" = "team_id",
473+
"team_name" = "team_name",
474+
"team_abbreviation" = "team",
475+
"gameInfo_homeTeam_numberFireId" = "home_team_id",
476+
"gameInfo_homeTeam_name" = "home_team",
477+
"gameInfo_homeTeam_abbreviation" = "home_abbr",
478+
"gameInfo_awayTeam_numberFireId" = "away_team_id",
479+
"gameInfo_awayTeam_name" = "away_team",
480+
"gameInfo_awayTeam_abbreviation" = "away_abbr",
481+
"salary" = "salary",
482+
"value" = "value",
483+
"pass_comp" = "pass_comp",
484+
"pass_att" = "pass_att",
485+
"passingYards" = "pass_yds",
486+
"passingTouchdowns" = "pass_tds",
487+
"interceptionsThrown" = "pass_int",
488+
"rushingAttempts" = "rush_att",
489+
"rushingYards" = "rush_yds",
490+
"rushingTouchdowns" = "rush_tds",
491+
"receptions" = "rec",
492+
"targets" = "rec_tgt",
493+
"receivingYards" = "rec_yds",
494+
"receivingTouchdowns" = "rec_tds",
495+
"fantasy" = "site_pts",
496+
"positionRank" = "pos_rank",
497+
"overallRank" = "overall_rank",
498+
"opponentDefensiveRank" = "opp_def_rank",
499+
"pointsAllowed" = "dst_pts_allowed",
500+
"yardsAllowed" = "dst_yds_allowed",
501+
"sacks" = "dst_sacks",
502+
"interceptions" = "dst_int",
503+
"touchdowns" = "dst_td",
504+
"extraPointsAttempted" = "fg_att",
505+
"extraPointsMade" = "xp",
506+
"fieldGoalsAttempted" = "xp_att",
507+
"fieldGoalsMade" = "fg",
508+
"fieldGoalsMade0To19" = "fg_0019",
509+
"fieldGoalsMade20To29" = "fg_2029",
510+
"fieldGoalsMade30To39" = "fg_3039",
511+
"fieldGoalsMade40To49" = "fg_4049",
512+
"fieldGoalsMade50Plus" = "fg_50"
513+
)
514+
467515

468516
# NFL FastR calc player stats ----
469517

R/source_scrapes.R

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,285 @@ scrape_fantasydata = function(pos = NULL, season = NULL, week = NULL,
12781278
)
12791279
}
12801280

1281+
# FanDuel ----
1282+
scrape_fanduel <- function(pos = c("QB", "RB", "WR", "TE", "K", "DST"),
1283+
season = NULL, week = 0, draft = TRUE, weekly = FALSE) {
1284+
1285+
if(is.null(week)) {
1286+
season = get_scrape_year()
1287+
}
1288+
1289+
if(is.null(week)) {
1290+
week = get_scrape_week()
1291+
}
1292+
1293+
if(week > 0) {
1294+
proj_type = "WEEKLY"
1295+
} else {
1296+
proj_type = "REMAINING"
1297+
}
1298+
1299+
if(is.null(pos)) {
1300+
pos = c("QB", "RB", "WR", "TE", "K", "DST")
1301+
} else {
1302+
pos
1303+
}
1304+
1305+
message(paste0("\nScraping FanDuel projections for ", paste(pos, collapse = ", "), "..."))
1306+
1307+
query <- '
1308+
query GetProjections($input: ProjectionsInput!) {
1309+
getProjections(input: $input) {
1310+
... on NflSkill {
1311+
player {
1312+
numberFireId
1313+
name
1314+
position
1315+
}
1316+
team {
1317+
numberFireId
1318+
name
1319+
abbreviation
1320+
}
1321+
gameInfo {
1322+
homeTeam {
1323+
numberFireId
1324+
name
1325+
abbreviation
1326+
}
1327+
awayTeam {
1328+
numberFireId
1329+
name
1330+
abbreviation
1331+
}
1332+
gameTime
1333+
}
1334+
salary
1335+
value
1336+
completionsAttempts
1337+
passingYards
1338+
passingTouchdowns
1339+
interceptionsThrown
1340+
rushingAttempts
1341+
rushingYards
1342+
rushingTouchdowns
1343+
receptions
1344+
targets
1345+
receivingYards
1346+
receivingTouchdowns
1347+
fantasy
1348+
positionRank
1349+
overallRank
1350+
opponentDefensiveRank
1351+
}
1352+
... on NflKicker {
1353+
player {
1354+
numberFireId
1355+
name
1356+
position
1357+
}
1358+
team {
1359+
numberFireId
1360+
name
1361+
1362+
abbreviation
1363+
}
1364+
gameInfo {
1365+
homeTeam {
1366+
numberFireId
1367+
name
1368+
abbreviation
1369+
}
1370+
awayTeam {
1371+
numberFireId
1372+
name
1373+
abbreviation
1374+
}
1375+
gameTime
1376+
}
1377+
salary
1378+
value
1379+
extraPointsAttempted
1380+
extraPointsMade
1381+
fieldGoalsAttempted
1382+
fieldGoalsMade
1383+
fieldGoalsMade0To19
1384+
fieldGoalsMade20To29
1385+
fieldGoalsMade30To39
1386+
fieldGoalsMade40To49
1387+
fieldGoalsMade50Plus
1388+
fantasy
1389+
positionRank
1390+
opponentDefensiveRank
1391+
}
1392+
... on NflDefenseSt {
1393+
player {
1394+
numberFireId
1395+
name
1396+
position
1397+
}
1398+
team {
1399+
numberFireId
1400+
name
1401+
abbreviation
1402+
}
1403+
gameInfo {
1404+
homeTeam {
1405+
numberFireId
1406+
name
1407+
abbreviation
1408+
}
1409+
awayTeam {
1410+
numberFireId
1411+
name
1412+
abbreviation
1413+
}
1414+
gameTime
1415+
}
1416+
salary
1417+
value
1418+
pointsAllowed
1419+
yardsAllowed
1420+
sacks
1421+
interceptions
1422+
fumblesRecovered
1423+
touchdowns
1424+
fantasy
1425+
positionRank
1426+
opponentOffensiveRank
1427+
}
1428+
... on NflDefensePlayer {
1429+
player {
1430+
numberFireId
1431+
name
1432+
position
1433+
}
1434+
team {
1435+
numberFireId
1436+
name
1437+
abbreviation
1438+
}
1439+
gameInfo {
1440+
homeTeam {
1441+
numberFireId
1442+
name
1443+
abbreviation
1444+
}
1445+
awayTeam {
1446+
numberFireId
1447+
name
1448+
abbreviation
1449+
}
1450+
gameTime
1451+
}
1452+
tackles
1453+
sacks
1454+
interceptions
1455+
touchdowns
1456+
passesDefended
1457+
fumblesRecovered
1458+
opponentOffensiveRank
1459+
}
1460+
}
1461+
}
1462+
'
1463+
1464+
data_payload <- list(
1465+
query = query,
1466+
variables = list(
1467+
input = list(
1468+
type = proj_type,
1469+
position = "NFL_SKILL",
1470+
sport = "NFL"
1471+
)
1472+
),
1473+
operationName = "GetProjections"
1474+
)
1475+
1476+
req <- httr2::request("https://fdresearch-api.fanduel.com/graphql") %>%
1477+
httr2::req_headers(
1478+
Accept = "*/*",
1479+
"Content-Type" = "application/json",
1480+
"Sec-Fetch-Site" = "same-site",
1481+
Origin = "https://www.fanduel.com",
1482+
"Sec-Fetch-Dest" = "empty",
1483+
"Accept-Language" = "en-US,en;q=0.9",
1484+
"Sec-Fetch-Mode" = "cors",
1485+
"User-Agent" = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Safari/605.1.15",
1486+
"Accept-Encoding" = "gzip, deflate, br"
1487+
)
1488+
1489+
position_groups <- list(
1490+
NFL_SKILL = c("QB","RB","WR","TE"),
1491+
NFL_KICKER = c("K"),
1492+
NFL_D_ST = c("DST")
1493+
)
1494+
1495+
graphql_positions <- names(position_groups)[
1496+
vapply(position_groups, function(x) any(pos %in% x), logical(1))
1497+
]
1498+
1499+
all_dfs <- map(graphql_positions, function(graph_pos) {
1500+
data_payload$variables$input$position <- graph_pos
1501+
1502+
resp <- req %>%
1503+
httr2::req_body_json(data_payload) %>%
1504+
httr2::req_perform()
1505+
1506+
result <- httr2::resp_body_json(
1507+
resp,
1508+
simplifyDataFrame = TRUE,
1509+
flatten = TRUE
1510+
)
1511+
1512+
df <- tibble::as_tibble(result$data$getProjections)
1513+
1514+
names(df) <- gsub("\\.", "_", names(df))
1515+
names(df) <- rename_vec(names(df), fanduel_columns)
1516+
1517+
df <- df %>%
1518+
type.convert(as.is = TRUE) %>%
1519+
dplyr::mutate(
1520+
data_src = "FanDuel",
1521+
proj_type = proj_type,
1522+
pos = ifelse(pos == "D", "DST", pos),
1523+
id = get_mfl_id(df$src_id,
1524+
player_name = df$player,
1525+
team = df$team,
1526+
pos = df$pos),
1527+
src_id = as.character(src_id)
1528+
)
1529+
1530+
if ("completionsAttempts" %in% names(df)) {
1531+
df <- df %>%
1532+
tidyr::separate(
1533+
completionsAttempts,
1534+
into = c("pass_comp", "pass_att"),
1535+
sep = "/",
1536+
convert = TRUE
1537+
)
1538+
}
1539+
1540+
df %>% dplyr::filter(pos %in% position_groups[[graph_pos]])
1541+
})
1542+
1543+
out_df <- all_dfs %>%
1544+
dplyr::bind_rows() %>%
1545+
dplyr::select(id, src_id, player, pos, team, everything())
1546+
l_pos <- split(out_df, out_df$pos)
1547+
l_pos <- l_pos[pos[pos %in% names(l_pos)]]
1548+
l_pos <- purrr::map(l_pos, function(df) {
1549+
df %>%
1550+
dplyr::select(-salary, -value) %>%
1551+
dplyr::select(where(~ !all(is.na(.))))
1552+
})
1553+
1554+
attr(l_pos, "season") <- season
1555+
attr(l_pos, "week") <- week
1556+
1557+
l_pos
1558+
}
1559+
12811560
# Depreceated ----
12821561
scrape_yahoo = function(pos = NULL, season = NULL, week = NULL,
12831562
draft = TRUE, weekly = TRUE) {

0 commit comments

Comments
 (0)