@@ -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(" \n Scraping 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 ----
12821561scrape_yahoo = function (pos = NULL , season = NULL , week = NULL ,
12831562 draft = TRUE , weekly = TRUE ) {
0 commit comments