This repository has been archived by the owner on Apr 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
entry_functions.R
174 lines (147 loc) · 6.76 KB
/
entry_functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# get_entry()
#' get_entry
#'
#' This function fetches detailed information for a Fantasy Premier League entry given the entry ID(s).
#' @param entryid The entry ID(s). Can be found on the FPL website under 'Gameweek history' in the URL - https://fantasy.premierleague.com/entry/XXXXXX/history.
#' @keywords entry
#' @export
#' @examples
#' get_entry(1076)
get_entry <- function(entryid = NULL){
if(is.null(entryid)) stop("You'll need to input at least one entry ID, mate.")
if(length(entryid) != 1) stop("One at a time, please")
{
entry <- jsonlite::fromJSON(paste("https://fantasy.premierleague.com/api/entry/",entryid,"/",sep=""))
return(entry)
}
}
# get_entry_hist()
#' get_entry_hist
#'
#' This function fetches historical statistics for a Fantasy Premier League entry given the entry ID(s).
#' @param entryid The entry ID(s). Can be found on the FPL website under 'Gameweek history' in the URL - https://fantasy.premierleague.com/entry/XXXXXX/history. You can list multiple, e.g. with c().
#' @keywords entry
#' @export
#' @examples
#' get_entry_hist(1076)
get_entry_hist <- function(entryid = NULL){
if(is.null(entryid)) stop("You'll need to input at least one entry ID, mate.")
{
entryhistory2 <- data.frame()
for (i in 1:length(entryid)){
entry <- jsonlite::fromJSON(paste("https://fantasy.premierleague.com/api/entry/",entryid[i],"/",sep=""))
entryhistory <- jsonlite::fromJSON(paste("https://fantasy.premierleague.com/api/entry/",entryid[i],"/history","/",sep=""))
if(length(entryhistory$past)==0) {entryhistory$past <- data.frame(season_name="",total_points="",rank="")}
entryhistory2 <- rbind(entryhistory2,data.frame(entryhistory$past,name=paste(entry$player_first_name,entry$player_last_name)))
}
return(entryhistory2)
}
}
# get_entry_season()
#' get_entry_season
#'
#' This function fetches in-season statistics for a Fantasy Premier League entry given the entry ID(s).
#' @param entryid The entry ID(s). Can be found on the FPL website under 'Gameweek history' in the URL - https://fantasy.premierleague.com/entry/XXXXXX/history. You can list multiple, e.g. with c().
#' @keywords entry
#' @export
#' @examples
#' get_entry_season(1076)
get_entry_season <- function(entryid = NULL){
if(is.null(entryid)) stop("You'll need to input at least one entry ID, mate.")
{
entryseason2 <- data.frame()
for (i in 1:length(entryid)){
entry <- jsonlite::fromJSON(paste("https://fantasy.premierleague.com/api/entry/",entryid[i],"/",sep=""))
entryseason <- jsonlite::fromJSON(paste("https://fantasy.premierleague.com/api/entry/",entryid[i],"/history","/",sep=""))
entryseason2 <- rbind(entryseason2,data.frame(entryseason$current,name=paste(entry$player_first_name,entry$player_last_name)))
}
return(entryseason2)
}
}
# get_entry_picks()
#' get_entry_picks
#'
#' This function fetches player picks for a Fantasy Premier League entry given the entry ID and GW.
#' @param entryid The entry ID. Can be found on the FPL website under 'Gameweek history' in the URL - https://fantasy.premierleague.com/entry/XXXXXX/history.
#' @param gw The GW for which player picks is requested.
#' @keywords entry
#' @export
#' @examples
#'
#' get_entry_picks(1076,1)
get_entry_picks <- function(entryid = NULL, gw = NULL){
if(is.null(entryid)) stop("You'll need to input an entry ID, mate.")
if(is.null(gw)) stop("You'll need to input a gameweek, mate.")
picks <- jsonlite::fromJSON(paste("https://fantasy.premierleague.com/api/entry/",entryid,"/event/",gw,"/picks","/",sep=""))
return(picks)
}
# get_entry_player_picks()
#' get_entry_player_picks()
#'
#' This function fetches player picks for a Fantasy Premier League entry given the entry ID(s) and GW(s).
#' @param entryid The entry ID(s). Can be found on the FPL website under 'Gameweek history' in the URL - https://fantasy.premierleague.com/entry/XXXXXX/history.
#' @param gw The gameweek(s) for which player picks are requested.
#' @keywords entry
#' @export
#' @examples
#' get_entry_player_picks(1076,1)
get_entry_player_picks <- function(entryid = NULL, gw = NULL){
if(is.null(entryid)) stop("You'll need to input an entry ID, mate.")
if(is.null(gw)) stop("You'll need to input a gameweek, mate.")
picks3 <- data.frame()
for (j in 1:length(entryid)) {
picks2 <- data.frame()
gw_started <- get_entry(entryid[j])[["started_event"]]
gw_thisentry <- gw[gw >= gw_started]
for (i in 1:length(gw_thisentry)) {
picks_list <- jsonlite::fromJSON(paste("https://fantasy.premierleague.com/api/entry/",entryid[j],"/event/",gw_thisentry[i],"/picks","/",sep=""))
picks <- picks_list$picks
picks$event <- gw_thisentry[i]
picks2 <- rbind(picks2, picks)
}
picks2$entry <- entryid[j]
picks3 <- rbind(picks3, picks2)
}
player_names <- get_player_name(unique(picks3$element))
picks3 <- merge(player_names, picks3, by.x = "id", by.y = "element")
names(picks3) <- c("id", "playername", "position", "multiplier", "is_captain", "is_vice_captain", "event", "entry")
picks3 <- picks3[order(picks3$entry, picks3$event), ]
return(picks3)
}
# get_entry_captain()
#' get_entry_captain
#'
#' This function fetches captain information for a Fantasy Premier League entry given the entry ID(s) and GW(s).
#' @param entryid The entry ID(s). Can be found on the FPL website under 'Gameweek history' in the URL - https://fantasy.premierleague.com/entry/XXXXXX/history.
#' @param gw The gameweek(s) for which captain is requested.
#' @keywords entry
#' @export
#' @examples
#' get_entry_captain(1076,1)
get_entry_captain <- function(entryid = NULL, gw = NULL){
if(is.null(entryid)) stop("You'll need to input an entry ID, mate.")
if(is.null(gw)) stop("You'll need to input a gameweek, mate.")
captain3 <- data.frame()
for (j in 1:length(entryid)) {
captain2 <- data.frame()
gw_started <- get_entry(entryid[j])[["started_event"]]
gw_thisentry <- gw[gw >= gw_started]
for (i in 1:length(gw_thisentry)) {
picks <- jsonlite::fromJSON(paste("https://fantasy.premierleague.com/api/entry/",entryid[j],"/event/",gw_thisentry[i],"/picks","/",sep=""))
captain <- data.frame("id" = picks$picks[picks$picks$multiplier %in% c(2, 3), "element"])
if(nrow(captain) == 0) {
captain <- data.frame("id" = picks$picks[picks$picks$is_captain == TRUE, "element"])
}
captain$event <- gw_thisentry[i]
captain2 <- rbind(captain2, captain)
}
captain2$entry <- entryid[j]
captain3 <- rbind(captain3, captain2)
}
player_names <- get_player_name(unique(captain3$captain))
captain3 <- merge(captain3, player_names, by = "id")
captain3 <- captain3[c(1, 4, 3, 2)]
names(captain3) <- c("id", "playername", "entry", "event")
captain3 <- captain3[order(captain3$entry,captain3$event), ]
return(captain3)
}