nesticot commited on
Commit
63bce26
·
verified ·
1 Parent(s): 5e3b171

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -3
app.py CHANGED
@@ -140,12 +140,73 @@ level_dict_filter = {
140
  year_list = [2024]
141
 
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  from shiny import App, reactive, ui, render
144
  from shiny.ui import h2, tags
145
 
146
- # Define the UI layout for the app
147
- # Define the UI layout for the app
148
- app_ui = ui.page_sidebar(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  # Sidebar content
150
  ui.sidebar(
151
  # Row for selecting season and level
@@ -208,7 +269,40 @@ app_ui = ui.page_sidebar(
208
  )
209
  )
210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  def server(input, output, session):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  @render.ui
213
  @reactive.event(input.player_button,input.level_input,input.year_input,input.tabset, ignore_none=False)
214
  def player_select_ui():
 
140
  year_list = [2024]
141
 
142
 
143
+ import requests
144
+
145
+ import os
146
+ CAMPAIGN_ID = os.getenv("CAMPAIGN_ID")
147
+ ACCESS_TOKEN = os.getenv("ACCESS_TOKEN")
148
+ BACKUP_PW = os.getenv("BACKUP_PW")
149
+ ADMIN_PW = os.getenv("ADMIN_PW")
150
+
151
+ url = f"https://www.patreon.com/api/oauth2/v2/campaigns/{CAMPAIGN_ID}/members"
152
+
153
+ headers = {
154
+ "Authorization": f"Bearer {ACCESS_TOKEN}"
155
+ }
156
+
157
+ # Simple parameters, requesting the member's email and currently entitled tiers
158
+ params = {
159
+ "fields[member]": "full_name,email", # Request the member's email
160
+ "include": "currently_entitled_tiers", # Include the currently entitled tiers
161
+ "page[size]": 1000 # Fetch up to 1000 patrons per request
162
+ }
163
+
164
+ response = requests.get(url, headers=headers, params=params)
165
+
166
+
167
+ VALID_PASSWORDS = []
168
+ if response.status_code == 200:
169
+ data = response.json()
170
+ for patron in data['data']:
171
+ try:
172
+ tiers = patron['relationships']['currently_entitled_tiers']['data']
173
+ if any(tier['id'] == '9078921' for tier in tiers):
174
+ full_name = patron['attributes']['email']
175
+ VALID_PASSWORDS.append(full_name)
176
+ except KeyError:
177
+ continue
178
+ VALID_PASSWORDS.append(BACKUP_PW)
179
+ VALID_PASSWORDS.append(ADMIN_PW)
180
+
181
+
182
  from shiny import App, reactive, ui, render
183
  from shiny.ui import h2, tags
184
 
185
+ # Define the login UI
186
+ login_ui = ui.page_fluid(
187
+ ui.card(
188
+ ui.h2([
189
+ "TJStats MiLB Statcast App ",
190
+ ui.tags.a("(@TJStats)", href="https://twitter.com/TJStats", target="_blank")
191
+ ]),
192
+ ui.p(
193
+ "This App is available to Superstar Patrons and One-Time Purchasers. If you a Superstar Patron, please enter your Patreon email address in the box below. If you are a One-Time Buyer (or having trouble), please refer to the ",
194
+ ui.tags.a("Patreon post", href="https://www.patreon.com/posts/118363824", target="_blank"),
195
+ "."
196
+ ),
197
+ ui.input_password("password", "Enter Patreon Email:"),
198
+ ui.tags.input(
199
+ type="checkbox",
200
+ id="authenticated",
201
+ value=False,
202
+ disabled=True
203
+ ),
204
+ ui.input_action_button("login", "Login", class_="btn-primary"),
205
+ ui.output_text("login_message"),
206
+ )
207
+ )
208
+
209
+ main_ui = ui.page_sidebar(
210
  # Sidebar content
211
  ui.sidebar(
212
  # Row for selecting season and level
 
269
  )
270
  )
271
 
272
+ # Combined UI with conditional panel
273
+ app_ui = ui.page_fluid(
274
+ ui.tags.head(
275
+ ui.tags.script(src="script.js")
276
+ ),
277
+
278
+ ui.panel_conditional(
279
+ "!input.authenticated",
280
+ login_ui
281
+ ),
282
+ ui.panel_conditional(
283
+ "input.authenticated",
284
+ main_ui
285
+ )
286
+ )
287
+
288
+
289
  def server(input, output, session):
290
+
291
+ @reactive.Effect
292
+ @reactive.event(input.login)
293
+ def check_password():
294
+ if input.password() in VALID_PASSWORDS:
295
+ ui.update_checkbox("authenticated", value=True)
296
+ ui.update_text("login_message", value="")
297
+ else:
298
+ ui.update_text("login_message", value="Invalid password!")
299
+ ui.update_text("password", value="")
300
+
301
+ @output
302
+ @render.text
303
+ def login_message():
304
+ return ""
305
+
306
  @render.ui
307
  @reactive.event(input.player_button,input.level_input,input.year_input,input.tabset, ignore_none=False)
308
  def player_select_ui():