Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start Yahoo Implementation #336 #424

Merged
merged 16 commits into from
Jan 28, 2024
Merged

Conversation

caltonji
Copy link

@caltonji caltonji commented Dec 21, 2023

Starting work on: #336

This PR implements ff_connect, ff_userleagues, and ff_franchises for Yahoo.

To login, users will need to

  1. Obtain your league_id from any yahoo URL in your leagues. In this link it's 77275: https://football.fantasysports.yahoo.com/f1/77275/10
  2. Obtain an auth token by signing into Yahoo here https://lemon-dune-0cd4b231e.azurestaticapps.net/.

I built that website for grabbing the auth token as part of a past project https://introductory.medium.com/download-yahoo-fantasy-football-data-bc1e1db7ee49

I plan to finish the Yahoo implementation, but I'm starting small.

franchise_name <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//team/name"))
user_name <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//manager/nickname"))
user_id <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//manager/guid"))
co_owners <- NA # This is available in Yahoo but I'm bad at R so I can't tell what format it's supposed to be.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What format should the co_owners field be in?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Generally have provided just the co-manager IDs in the past as a nested vector, can do that here or provide more of these details collapsed into a nested list/tibble.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Studying the example provided here and I'm not sure your above code will work as intended because xml_find_all will strip every user (manager or co-manager) into a plain vector.

This might result in inaccurate output when combined into a dataframe later (i.e. if franchise_name is a vector of 12 names and user_name is a vector of 16 names, combining these back into a base R dataframe would result in recycling the shorter vector to match the longer one)

Can assist further if you provide the full XML output from xml_doc?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Hadn't tested with co_owners yet. I updated it to support co_owners as a nested list. I also did add an example xml_file tests/yahoo_example_responses/single-league-teams.xml

@tanho63 tanho63 changed the base branch from main to yahoo December 22, 2023 00:35
Copy link
Member

@tanho63 tanho63 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to see someone take this on! I've added a feature branch yahoo which we can use to merge reviewed progress to (so that any other platform PRs don't affect this one).

Have left a few comments, feel free to reply here or take it to discord DMs for discussions if preferred

R/yahoo_api.R Outdated Show resolved Hide resolved
R/yahoo_connect.R Outdated Show resolved Hide resolved
R/yahoo_connect.R Outdated Show resolved Hide resolved
R/yahoo_connect.R Outdated Show resolved Hide resolved
R/yahoo_api.R Outdated Show resolved Hide resolved
R/yahoo_connect.R Outdated Show resolved Hide resolved
R/yahoo_franchises.R Outdated Show resolved Hide resolved
franchise_name <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//team/name"))
user_name <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//manager/nickname"))
user_id <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//manager/guid"))
co_owners <- NA # This is available in Yahoo but I'm bad at R so I can't tell what format it's supposed to be.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Generally have provided just the co-manager IDs in the past as a nested vector, can do that here or provide more of these details collapsed into a nested list/tibble.

franchise_name <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//team/name"))
user_name <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//manager/nickname"))
user_id <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//manager/guid"))
co_owners <- NA # This is available in Yahoo but I'm bad at R so I can't tell what format it's supposed to be.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Studying the example provided here and I'm not sure your above code will work as intended because xml_find_all will strip every user (manager or co-manager) into a plain vector.

This might result in inaccurate output when combined into a dataframe later (i.e. if franchise_name is a vector of 12 names and user_name is a vector of 16 names, combining these back into a base R dataframe would result in recycling the shorter vector to match the longer one)

Can assist further if you provide the full XML output from xml_doc?

R/yahoo_userleagues.R Outdated Show resolved Hide resolved
Copy link
Author

@caltonji caltonji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review @tanho63! I tried to address all feedback.

R/yahoo_api.R Outdated Show resolved Hide resolved
R/yahoo_api.R Outdated Show resolved Hide resolved
R/yahoo_connect.R Outdated Show resolved Hide resolved
franchise_name <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//team/name"))
user_name <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//manager/nickname"))
user_id <- xml2::xml_text(xml2::xml_find_all(xml_doc, "//manager/guid"))
co_owners <- NA # This is available in Yahoo but I'm bad at R so I can't tell what format it's supposed to be.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Hadn't tested with co_owners yet. I updated it to support co_owners as a nested list. I also did add an example xml_file tests/yahoo_example_responses/single-league-teams.xml

R/yahoo_userleagues.R Outdated Show resolved Hide resolved
R/yahoo_userleagues.R Outdated Show resolved Hide resolved
R/yahoo_franchises.R Outdated Show resolved Hide resolved
@caltonji caltonji requested a review from tanho63 January 11, 2024 16:41
Copy link
Member

@tanho63 tanho63 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I added some test mocks to your PR branch so that we can start using some tests here and added some crude tests to tests/testthat/tests-yahoo.R. Might be easiest to do up the PR, add the tests, and then send me a token to do the mock-test-uploads 😀

Left a few style notes for a more "tidy" way to do this, mostly-optional so if you'd prefer I merge and we tackle that after we get a more complete first implementation let me know

R/yahoo_connect.R Outdated Show resolved Hide resolved
ff_franchises.yahoo_conn <- function(conn) {
glue::glue("leagues;league_keys={conn$league_key}/teams") %>%
yahoo_getendpoint(conn) %>%
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto with above re: getElement etc etc

}
}

.yahoo_process_franchises_response <- function(xml_doc) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Token you gave me expired before I could fully flesh out this example, but here's an alternative way to approach this problem - convert xml to list and use tidyr/dplyr/purrr to unnest out the dataframe you want

  x <- xml_doc %>%
    xml2::as_list() %>%
    purrr::pluck("fantasy_content", "leagues","league","teams") %>%
    tibble::tibble() %>%
    tidyr::unnest_wider(1) %>%
    dplyr::select(
      franchise_id = team_id,
      franchise_name = name,
      division_id = division_id,
      managers
    ) %>%
    tidyr::unnest_longer(c(franchise_id, franchise_name, division_id)) %>%
    dplyr::mutate(
      managers = purrr::map(managers, ~ tibble::tibble(.x) %>% tidyr::unnest_wider(1))
    ) %>%
    tidyr::hoist(
      managers,
      "user_name" = "nickname",
      "user_id" = "guid",
      "user_felo" = "felo_score"
    ) %>%
    tidyr::unnest_longer(c(user_name, user_id, user_felo)) %>%
    tidyr::unnest_longer(c(user_name, user_id, user_felo))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a few hours reading up on the tidyverse but I just can't for the life of me figure out how to work with the as_list xml output. I'm going to go with the xml_find_all solutions for now since they're super intuitive for the uninitated.

Copy link
Member

@tanho63 tanho63 Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the uninitated.

the un-tidy-doctrinated 😂

That's fine. Let's put in getElement in all the relevant places and call it the finish line for this PR?

Copy link
Member

@tanho63 tanho63 Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe useful future reading if you want to look at unnesting in the future: https://r4ds.hadley.nz/rectangling

@tanho63 tanho63 merged commit 4b9c680 into ffverse:yahoo Jan 28, 2024
0 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants