TimStats commited on
Commit
54dd201
·
verified ·
1 Parent(s): b439491

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +96 -1
app.R CHANGED
@@ -8,7 +8,44 @@ library(dplyr) # For data manipulation
8
  library(httr)
9
  library(patchwork) # For combining plots
10
 
11
- # [Previous functions remain the same: download_private_csv, heatMap, and data loading]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  download_private_csv <- function(repo_id, filename) {
13
  url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
14
  response <- GET(url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
@@ -108,6 +145,64 @@ ui <- page_fluid(
108
 
109
  # [Server code remains exactly the same]
110
  server <- function(input, output, session) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  pname <- reactiveVal()
112
 
113
  plot_data <- reactive({
 
8
  library(httr)
9
  library(patchwork) # For combining plots
10
 
11
+ check_patreon_access <- function(email) {
12
+ campaign_id <- Sys.getenv("PATREON_CAMPAIGN_ID")
13
+ access_token <- Sys.getenv("PATREON_ACCESS_TOKEN")
14
+
15
+ base_url <- paste0("https://www.patreon.com/api/oauth2/v2/campaigns/", campaign_id, "/members")
16
+
17
+ params <- list(
18
+ `include` = "user,currently_entitled_tiers",
19
+ `fields[member]` = "patron_status,email",
20
+ `fields[tier]` = "title,amount_cents"
21
+ )
22
+
23
+ response <- GET(
24
+ base_url,
25
+ query = params,
26
+ add_headers(
27
+ `Authorization` = paste("Bearer", access_token),
28
+ `User-Agent` = "R/httr"
29
+ )
30
+ )
31
+
32
+ content <- fromJSON(rawToChar(response$content))
33
+
34
+ # Check if email exists and has correct access
35
+ if (!is.null(content$data$attributes$email)) {
36
+ user_email <- content$data$attributes$email
37
+ patron_status <- content$data$attributes$patron_status
38
+ tier <- content[["included"]][["attributes"]][["title"]][2]
39
+
40
+ # Check conditions
41
+ if (tolower(user_email) == tolower(email) &&
42
+ patron_status == "active_patron" &&
43
+ (tier == "Veteran" || tier == "Hall of Fame")) {
44
+ return(TRUE)
45
+ }
46
+ }
47
+ return(FALSE)
48
+ }
49
  download_private_csv <- function(repo_id, filename) {
50
  url <- paste0("https://huggingface.co/datasets/", repo_id, "/resolve/main/", filename)
51
  response <- GET(url, add_headers(Authorization = paste("Bearer", Sys.getenv("GETCSV"))))
 
145
 
146
  # [Server code remains exactly the same]
147
  server <- function(input, output, session) {
148
+ is_authenticated <- reactiveVal(FALSE)
149
+
150
+ # Render either login page or main app
151
+ output$page_content <- renderUI({
152
+ if (!is_authenticated()) {
153
+ # Login page
154
+ card(
155
+ card_header("Authentication Required"),
156
+ card_body(
157
+ textInput("email", "Enter your Patreon email:"),
158
+ actionButton("check_access", "Access App", class = "btn-primary"),
159
+ textOutput("auth_message")
160
+ )
161
+ )
162
+ } else {
163
+ # Main app content (your existing UI)
164
+ card(
165
+ card_header("2020-2024 Filterable Pitcher Heatmaps"),
166
+ card(
167
+ layout_columns(
168
+ col_widths = c(4, 4, 4),
169
+ card(
170
+ selectInput("league", "Select League:", choices = c("MLB","AAA","FSL")),
171
+ selectInput("player", "Select Player:", choices = c("")),
172
+ sliderInput("concen", "Heatmap Bandwidth Factor", value = .75, step = 0.05, min = .5, max = 1.5)
173
+ ),
174
+ card(
175
+ selectInput("hand1", "Batter Handedness", choices = c("All Batters","LHH","RHH")),
176
+ selectInput("pitch1", "Pitch:", choices = c("")),
177
+ dateRangeInput("date1", "Date Range:", start = "2024-02-09", end = "2024-09-05")
178
+ ),
179
+ card(
180
+ selectInput("hand2", "Batter Handedness", choices = c("All Batters","LHH","RHH")),
181
+ selectInput("pitch2", "Pitch:", choices = c("")),
182
+ dateRangeInput("date2", "Date Range:", start = "2024-02-09", end = "2024-09-05")
183
+ )
184
+ )
185
+ ),
186
+ card(
187
+ downloadButton("download", "Download Plot", class = "btn-primary mb-3"),
188
+ plotOutput("combined_graph", height = "600px")
189
+ )
190
+ )
191
+ }
192
+ })
193
+
194
+ # Handle authentication
195
+ observeEvent(input$check_access, {
196
+
197
+ if (check_patreon_access(input$email)) {
198
+ is_authenticated(TRUE)
199
+ } else {
200
+ output$auth_message <- renderText({
201
+ "Access denied. Please ensure you are an active Veteran or Hall of Fame tier patron."
202
+ })
203
+ }
204
+ })
205
+
206
  pname <- reactiveVal()
207
 
208
  plot_data <- reactive({