import requests
from nflpool.data.dbsession import DbSessionFactory
from nflpool.data.activeplayers import ActiveNFLPlayers
import nflpool.data.secret as secret
from requests.auth import HTTPBasicAuth
from nflpool.data.seasoninfo import SeasonInfo


'''After updating the season to a new year, get all active NFL players and add to the database to be
used by NFLPool players to choose from when submitting their picks.  The Try / Except is needed for 
players who may not have a position assigned yet.'''


class ActivePlayersService:
    @classmethod
    def add_active_nflplayers(cls, season: int, team_id: int, firstname: str, lastname: str,
                              position: str, player_id: int):

        session = DbSessionFactory.create_session()

        season_row = session.query(SeasonInfo).filter(SeasonInfo.id == '1').first()
        season = season_row.current_season

        response = requests.get('https://api.mysportsfeeds.com/v1.1/pull/nfl/' + str(season) +
                                '-regular/active_players.json',
                                auth=HTTPBasicAuth(secret.msf_username, secret.msf_pw))

        player_info = response.json()
        player_list = player_info["activeplayers"]["playerentry"]

        for players in player_list:
            try:
                firstname = players["player"]["FirstName"]
                lastname = players["player"]["LastName"]
                player_id = players["player"]["ID"]
                team_id = players["team"]["ID"]
                position = players["player"]["Position"]
            except KeyError:
                continue

            active_players = ActiveNFLPlayers(firstname=firstname, lastname=lastname, player_id=player_id,
                                              team_id=team_id, position=position, season=season)

            session.add(active_players)

            session.commit()

@@ -889,16 +889,18 @@
 %0A       
+ #
  respons
@@ -979,32 +979,33 @@
 ason) +%0A        
+#
                 
@@ -1044,32 +1044,33 @@
 .json',%0A        
+#
                 
@@ -1134,16 +1134,188 @@
 f_pw))%0A%0A
+        response = requests.get('https://api.mysportsfeeds.com/v2.0/pull/nfl/players.json?season=' +%0A                                str(season) + '&rosterstatus=ROSTER')%0A%0A
         
@@ -1379,22 +1379,16 @@
 r_info%5B%22
-active
 players%22
@@ -1392,23 +1392,8 @@
 rs%22%5D
-%5B%22playerentry%22%5D
 %0A%0A  
@@ -1490,17 +1490,17 @@
 ayer%22%5D%5B%22
-F
+f
 irstName
@@ -1548,17 +1548,17 @@
 ayer%22%5D%5B%22
-L
+l
 astName%22
@@ -1602,26 +1602,26 @@
 %5B%22player%22%5D%5B%22
-ID
+id
 %22%5D%0A         
@@ -1654,14 +1654,22 @@
 team
+AsOfDate
 %22%5D%5B%22
-ID
+id
 %22%5D%0A 
@@ -1713,16 +1713,23 @@
 ayer%22%5D%5B%22
+primary
 Position

Update ActiveNFLPlayers service to use MSF API v2