Jon Solow commited on
Commit
136bc13
·
1 Parent(s): 7a38cbd

Wire up login component to save user guid

Browse files
Files changed (2) hide show
  1. src/login_component.py +5 -0
  2. src/yahoo_client.py +19 -10
src/login_component.py CHANGED
@@ -3,6 +3,8 @@ import streamlit as st
3
  from streamlit_oauth import OAuth2Component
4
  import os
5
 
 
 
6
  # # Load environment variables from .env file
7
  # from dotenv import load_dotenv
8
  # load_dotenv()
@@ -37,12 +39,15 @@ def get_authorization_button():
37
  if result and "token" in result:
38
  # If authorization successful, save token in session state
39
  st.session_state.token = result.get("token")
 
 
40
  st.rerun()
41
  else:
42
  # # If token exists in session state, allow logout
43
  st.session_state["token"]
44
  if st.button("Logout"):
45
  del st.session_state.token
 
46
  st.rerun()
47
  # # If token exists in session state, show the token
48
  # token = st.session_state["token"]
 
3
  from streamlit_oauth import OAuth2Component
4
  import os
5
 
6
+ from yahoo_client import YahooFantasyClient
7
+
8
  # # Load environment variables from .env file
9
  # from dotenv import load_dotenv
10
  # load_dotenv()
 
39
  if result and "token" in result:
40
  # If authorization successful, save token in session state
41
  st.session_state.token = result.get("token")
42
+ yahoo_con = YahooFantasyClient(oauth2, st.session_state.token)
43
+ st.session_state.logged_in_guid = yahoo_con.get_guid_for_logged_in_user()
44
  st.rerun()
45
  else:
46
  # # If token exists in session state, allow logout
47
  st.session_state["token"]
48
  if st.button("Logout"):
49
  del st.session_state.token
50
+ del st.session_state.logged_in_guid
51
  st.rerun()
52
  # # If token exists in session state, show the token
53
  # token = st.session_state["token"]
src/yahoo_client.py CHANGED
@@ -1,9 +1,10 @@
 
1
  import pandas as pd
2
  from pydantic import BaseModel
3
  import re
4
  from typing import List, Mapping, Tuple, Union
5
  import xml.etree.ElementTree as ET
6
- from yahoo_oauth import OAuth2
7
 
8
 
9
  class LeagueSettings(BaseModel):
@@ -50,20 +51,22 @@ class LeagueSettings(BaseModel):
50
 
51
 
52
  class YahooFantasyClient:
53
- def get_new_oauth_from_json(self) -> OAuth2:
54
- return OAuth2(None, None, from_file=self.client_json_path)
55
-
56
- def __init__(self, client_json_path: str):
57
- self.client_json_path = client_json_path
58
- self.oauth: OAuth2 = self.get_new_oauth_from_json()
59
 
60
  def authorize_yahoo_from_client_json(self) -> None:
61
- if not self.oauth.token_is_valid():
62
- self.oauth.refresh_access_token()
63
 
64
  def yahoo_request_to_xml(self, url: str) -> Tuple[ET.Element, str]:
65
  self.authorize_yahoo_from_client_json()
66
- r = self.oauth.session.get(url)
 
 
 
 
 
 
67
 
68
  xmlstring = r.text
69
  xmlstring = re.sub(' xmlns="[^"]+"', "", xmlstring, count=1)
@@ -78,6 +81,12 @@ class YahooFantasyClient:
78
  )
79
  return league_keys
80
 
 
 
 
 
 
 
81
  def parse_matchup(self, matchup: ET.Element, match_index: int) -> List[Mapping[str, Union[str, float]]]:
82
  matchup_info = Matchup.from_xml(matchup, match_index)
83
  return matchup_info.to_list_team_dict()
 
1
+ import asyncio
2
  import pandas as pd
3
  from pydantic import BaseModel
4
  import re
5
  from typing import List, Mapping, Tuple, Union
6
  import xml.etree.ElementTree as ET
7
+ from streamlit_oauth import OAuth2Component
8
 
9
 
10
  class LeagueSettings(BaseModel):
 
51
 
52
 
53
  class YahooFantasyClient:
54
+ def __init__(self, oauth: OAuth2Component, token):
55
+ self.oauth: OAuth2Component = oauth
56
+ self.token = token
 
 
 
57
 
58
  def authorize_yahoo_from_client_json(self) -> None:
59
+ self.token = self.oauth.refresh_token(self.token)
 
60
 
61
  def yahoo_request_to_xml(self, url: str) -> Tuple[ET.Element, str]:
62
  self.authorize_yahoo_from_client_json()
63
+ client = self.oauth.client.get_httpx_client()
64
+ r = asyncio.run(
65
+ client.get(
66
+ url,
67
+ headers={"Authorization": f"Bearer {self.token.get('access_token')}"},
68
+ )
69
+ )
70
 
71
  xmlstring = r.text
72
  xmlstring = re.sub(' xmlns="[^"]+"', "", xmlstring, count=1)
 
81
  )
82
  return league_keys
83
 
84
+ def get_guid_for_logged_in_user(self) -> str:
85
+ url = "https://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;game_keys=nfl/teams"
86
+ root, _ = self.yahoo_request_to_xml(url)
87
+ user_guid = root.findtext("./users/user/guid")
88
+ return user_guid
89
+
90
  def parse_matchup(self, matchup: ET.Element, match_index: int) -> List[Mapping[str, Union[str, float]]]:
91
  matchup_info = Matchup.from_xml(matchup, match_index)
92
  return matchup_info.to_list_team_dict()