Skip to content

Commit

Permalink
add team, nation and league
Browse files Browse the repository at this point in the history
  • Loading branch information
danielredondo committed Sep 16, 2021
1 parent f1325b3 commit 691d1f5
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 124 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Package: rfutbin
Type: Package
Title: Searchs FIFA Ultimate Team players to get stats and prices in Futbin
Version: 1.0.1
Version: 1.0.2
Authors@R: person("Daniel", "Redondo-Sanchez", email = "danielredondosanchez@hotmail.com",
role = c("aut", "cre"))
Maintainer: Daniel Redondo Sánchez <danielredondosanchez@hotmail.com>
Description: This package provides an R interface to Futbin,
obtaining the live prices of FIFA Ultimate Team players, as well as
other interesting information (stats, versions and positions, popularity, ...).
other interesting information (stats, version, position, popularity, nation...).
It also provides interactive radar plots to easily visualize the main stats
of the players.
License: MIT + file LICENSE
Expand All @@ -18,4 +18,4 @@ Imports: dplyr, rvest, httr, magrittr, xml2, radarchart
Suggests:
URL: https://github.com/danielredondo/rfutbin/
BugReports: https://github.com/danielredondo/rfutbin/issues
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Daniel Redondo
Copyright (c) 2021 Daniel Redondo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
67 changes: 58 additions & 9 deletions R/futbin_scrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,63 @@ futbin_scrap <- function(url, platform = "ps4", sleep_time = 5, verbose = TRUE)
if (verbose == T) print(paste0("Reading... ", url_to_scrap))

# Web scraping
web <- httr::GET(url_to_scrap,
httr::set_cookies(platform = platform))
data_page <- web %>%
httr::content() %>%
rvest::html_nodes(xpath = "//table") %>%
magrittr::extract(1) %>%
rvest::html_table() %>%
magrittr::extract2(1)

if (i == 1) {
tabla <- httr::GET(url_to_scrap,
httr::set_cookies(platform = platform)) %>%
tabla <- data_page

# Add team, nation and league of each player
web %>%
httr::content() %>%
rvest::html_nodes(xpath = "//table") %>%
magrittr::extract(1) %>%
rvest::html_table() %>%
magrittr::extract2(1)
rvest::html_elements(".players_club_nation") %>%
xml2::xml_contents() %>%
xml2::xml_attr("data-original-title") %>%
stats::na.omit() %>%
as.vector() -> table_team_nation_league

tabla$team <- NA
tabla$nation <- NA
tabla$league <- NA

for(j in 1:nrow(tabla)){
tabla$team[j] <- table_team_nation_league[3 * j - 2]
tabla$nation[j] <- table_team_nation_league[3 * j - 1]
tabla$league[j] <- table_team_nation_league[3 * j]
}
} else {
Sys.sleep(sleep_time)
tabla.i <- httr::GET(url_to_scrap,
httr::set_cookies(platform = platform)) %>%
tabla.i <- data_page

# Add team, nation and league of each player
web %>%
httr::content() %>%
rvest::html_nodes(xpath = "//table") %>%
magrittr::extract(1) %>%
rvest::html_table() %>%
magrittr::extract2(1)
rvest::html_elements(".players_club_nation") %>%
xml2::xml_contents() %>%
xml2::xml_attr("data-original-title") %>%
stats::na.omit() %>%
as.vector() -> table_team_nation_league

tabla.i$team <- NA
tabla.i$nation <- NA
tabla.i$league <- NA

for(j in 1:nrow(tabla.i)){
tabla.i$team[j] <- table_team_nation_league[3 * j - 2]
tabla.i$nation[j] <- table_team_nation_league[3 * j - 1]
tabla.i$league[j] <- table_team_nation_league[3 * j]
}

if(tabla.i[1, 1] != "No Results") tabla <- rbind(tabla, tabla.i)
}

Expand All @@ -64,7 +104,7 @@ futbin_scrap <- function(url, platform = "ps4", sleep_time = 5, verbose = TRUE)
colnames(tabla) <- c(
"name", "rating", "position", "version", "price", "skills", "weak_foot", "work_rate",
"pac", "sho", "pas", "dri", "def", "phy",
"hei", "popularity", "base_stats", "in_game_stats"
"hei", "popularity", "base_stats", "in_game_stats", "team", "nation", "league"
)

# Work rates (attack and defense)
Expand All @@ -88,6 +128,15 @@ futbin_scrap <- function(url, platform = "ps4", sleep_time = 5, verbose = TRUE)
if (aux_M[i] != -1) tabla$price[i] <- tabla$price[i] * 1000000
}

# Reorder variables
tabla <- tabla %>%
dplyr::select("name", "rating", "position", "version", "price", "skills", "weak_foot",
"pac", "sho", "pas", "dri", "def", "phy",
"hei", "popularity", "base_stats", "in_game_stats", "wr_attack", "wr_defense",
"wei", "team", "nation", "league"
)

return(tabla)
}


24 changes: 23 additions & 1 deletion R/futbin_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ futbin_search <- function(name = "", platform = "ps4", version = NULL,verbose =
url <- paste0("https://www.futbin.com/21/players?page=1&search=", name)

# Web scraping
tabla <- httr::GET(url, httr::set_cookies(platform = platform)) %>%
web <- httr::GET(url, httr::set_cookies(platform = platform)) # To download only once
tabla <- web %>%
httr::content() %>%
rvest::html_nodes(xpath = "//table") %>%
magrittr::extract(1) %>%
Expand Down Expand Up @@ -83,6 +84,27 @@ futbin_search <- function(name = "", platform = "ps4", version = NULL,verbose =
if (aux_M[i] != -1) tabla$price[i] <- tabla$price[i] * 1000000
}

# Add team, nation and league of each player
web %>%
httr::content() %>%
rvest::html_nodes(xpath = "//table") %>%
magrittr::extract(1) %>%
rvest::html_elements(".players_club_nation") %>%
xml2::xml_contents() %>%
xml2::xml_attr("data-original-title") %>%
stats::na.omit() %>%
as.vector() -> table_team_nation_league

tabla$team <- NA
tabla$nation <- NA
tabla$league <- NA

for(i in 1:nrow(tabla)){
tabla$team[i] <- table_team_nation_league[3 * i - 2]
tabla$nation[i] <- table_team_nation_league[3 * i - 1]
tabla$league[i] <- table_team_nation_league[3 * i]
}

# Filter version of the player
if (is.null(version) == FALSE){
version_selected <- version
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ If you use this package, you can cite it as:


```
Redondo-Sanchez, Daniel (2021). rfutbin (v1.0.1): R package to get price and stats of FIFA Ultimate Team players in Futbin. https://github.com/danielredondo/rfutbin
Redondo-Sanchez, Daniel (2021). rfutbin (v1.0.2): R package to get price and stats of FIFA Ultimate Team players in Futbin. https://github.com/danielredondo/rfutbin
```
Loading

0 comments on commit 691d1f5

Please sign in to comment.