I'm playing around with some Fantasy Premier League data available through an API.
Desired result, nested dataframes, is achieved when running the following code:
library(jsonlite)
url <- "https://fantasy.premierleague.com/drf/bootstrap-static"
fromJSON(url)
More data is obtainable if you log in. I can't get the httr
package working with the API, read somewhere that it stopped working after an SSL change a while back.
I'm able to log in and fetch data by going through Python, but I end up with different json. The code below illustrates the problem. After going through Python, values are placed in arrays (note the brackets in the bottom json output):
library(tidyverse)
library(jsonlite)
# No authentication
# API url
bootstrapUrl <- "https://fantasy.premierleague.com/drf/bootstrap-static"
# Desired result
fromJSON(bootstrapUrl) %>%
toJSON() %>%
substr(., 1, 100)
#> {"phases":[{"id":1,"name":"Overall","num_winners":3,"start_event":1,"stop_event":38},{"id":2,"name":
# With authentication
library(reticulate)
use_python("/usr/bin/python3.6")
source_python("~/Projects/pl/api_request.py")
### api_request.py
#
# import requests
#
# def py_get_json(subdomain):
# session = requests.session()
# apiUrl = "https://fantasy.premierleague.com/drf/"
# loginUrl = 'https://users.premierleague.com/accounts/login/'
# payload = {
# 'password': '',
# 'login': '',
# 'redirect_uri': 'https://fantasy.premierleague.com/a/login',
# 'app': 'plfpl-web'
# }
# session.post(loginUrl, data=payload)
# targetUrl = apiUrl + subdomain
#
# return session.get(targetUrl).json()
#
###
subdomain <- "bootstrap-static"
json <- py_get_json(subdomain)
json %>%
toJSON() %>%
substr(., 1, 100)
#> {"phases":[{"id":[1],"name":["Overall"],"num_winners":[3],"start_event":[1],"stop_event":[38]},{"id"
I'm guessing this has to do with how the Python object is returned to R via reticulate
. I'm looking for an easy fix, is there any functionality within jsonlite
that can sort this out?