# Path: nflpool/data/dbsession.py
# class DbSessionFactory:
#     factory = None
# 
#     # Start a database session at app startup
#     @staticmethod
#     def global_init(db_file):
#         if DbSessionFactory.factory:
#             return
# 
#         if not db_file or not db_file.strip():
#             raise Exception("You must specify a data file.")
# 
#         conn_str = "sqlite:///" + db_file
#         print("Connecting to db with conn string: {}".format(conn_str))
# 
#         engine = sqlalchemy.create_engine(conn_str, echo=False)
# 
#         SqlAlchemyBase.metadata.create_all(engine)
#         DbSessionFactory.factory = sqlalchemy.orm.sessionmaker(bind=engine)
# 
#     @staticmethod
#     def create_session():
#         return DbSessionFactory.factory()
# 
# Path: nflpool/data/weekly_nflplayer_stats.py
# class WeeklyNFLPlayerStats(SqlAlchemyBase):
#     __tablename__ = "WeeklyNFLPlayerStats"
#     primary_key = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     season = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     week = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     player_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("ActiveNFLPlayers.player_id")
#     )
#     passyds = sqlalchemy.Column(sqlalchemy.Integer)
#     rushyds = sqlalchemy.Column(sqlalchemy.Integer)
#     recyds = sqlalchemy.Column(sqlalchemy.Integer)
#     sacks = sqlalchemy.Column(sqlalchemy.REAL)
#     interceptions = sqlalchemy.Column(sqlalchemy.Integer)
# 
# Path: nflpool/data/seasoninfo.py
# class SeasonInfo(SqlAlchemyBase):
#     __tablename__ = "SeasonInfo"
#     id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, autoincrement=True)
#     current_season = sqlalchemy.Column(sqlalchemy.Integer)
#     season_start_date = sqlalchemy.Column(sqlalchemy.String)
#     home_team = sqlalchemy.Column(sqlalchemy.String)
#     away_team = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/data/weekly_team_stats.py
# class WeeklyTeamStats(SqlAlchemyBase):
#     __tablename__ = "WeeklyTeamStats"
#     primary_key = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     season = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     week = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     team_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("TeamInfo.team_id")
#     )
#     division_rank = sqlalchemy.Column(sqlalchemy.Integer)
#     conference_rank = sqlalchemy.Column(sqlalchemy.Integer)
#     points_for = sqlalchemy.Column(sqlalchemy.Integer)
#     tiebreaker_td = sqlalchemy.Column(sqlalchemy.Integer)
#     tiebreaker_team = sqlalchemy.Column(sqlalchemy.Integer)
# 
# Path: nflpool/services/time_service.py
# class TimeService:
#     @staticmethod
#     def get_time():
#         """Create a service to get the time - there were too many instances of getting the current time in
#         the codebase making testing difficult."""
#         # Change now_time for testing
#         # Use this one for production:
#         now_time = pendulum.now(tz=pendulum.timezone("America/New_York"))
#         # Use this one for testing:
#         # now_time = pendulum.datetime(2018, 9, 18, 6, 21, tz='America/New_York')
# 
#         return now_time
# 
#     @staticmethod
#     def get_week():
#         session = DbSessionFactory.create_session()
#         season_row = session.query(SeasonInfo).filter(SeasonInfo.id == "1").first()
# 
#         season_start = pendulum.parse(season_row.season_start_date)
# 
#         diff = TimeService.get_time() - season_start
#         week = int((diff.days / 7) + 1)
# 
#         return week


from nflpool.data.dbsession import DbSessionFactory
from nflpool.data.weekly_nflplayer_stats import WeeklyNFLPlayerStats
from requests.auth import HTTPBasicAuth
from nflpool.data.seasoninfo import SeasonInfo
from nflpool.data.weekly_team_stats import WeeklyTeamStats
from nflpool.services.time_service import TimeService
import requests
import nflpool.data.secret as secret
            session.commit()

    @staticmethod
    def get_sack_stats():

        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/v2.0/pull/nfl/"
            + str(season)
            + "-regular/player_stats_totals.json?position=DE,DT,ILB,LB,MLB,NT,OLB&stats=Sacks",
            auth=HTTPBasicAuth(secret.msf_api, secret.msf_v2pw),
        )

        player_json = response.json()
        player_data = player_json["playerStatsTotals"]

        for players in player_data:
            try:
                player_id = players["player"]["id"]
                sacks = players["stats"]["tackles"]["sacks"]

            except KeyError:
                continue

            week = TimeService.get_week()


# Path: nflpool/data/dbsession.py
# class DbSessionFactory:
#     factory = None
# 
#     # Start a database session at app startup
#     @staticmethod
#     def global_init(db_file):
#         if DbSessionFactory.factory:
#             return
# 
#         if not db_file or not db_file.strip():
#             raise Exception("You must specify a data file.")
# 
#         conn_str = "sqlite:///" + db_file
#         print("Connecting to db with conn string: {}".format(conn_str))
# 
#         engine = sqlalchemy.create_engine(conn_str, echo=False)
# 
#         SqlAlchemyBase.metadata.create_all(engine)
#         DbSessionFactory.factory = sqlalchemy.orm.sessionmaker(bind=engine)
# 
#     @staticmethod
#     def create_session():
#         return DbSessionFactory.factory()
# 
# Path: nflpool/data/weekly_nflplayer_stats.py
# class WeeklyNFLPlayerStats(SqlAlchemyBase):
#     __tablename__ = "WeeklyNFLPlayerStats"
#     primary_key = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     season = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     week = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     player_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("ActiveNFLPlayers.player_id")
#     )
#     passyds = sqlalchemy.Column(sqlalchemy.Integer)
#     rushyds = sqlalchemy.Column(sqlalchemy.Integer)
#     recyds = sqlalchemy.Column(sqlalchemy.Integer)
#     sacks = sqlalchemy.Column(sqlalchemy.REAL)
#     interceptions = sqlalchemy.Column(sqlalchemy.Integer)
# 
# Path: nflpool/data/seasoninfo.py
# class SeasonInfo(SqlAlchemyBase):
#     __tablename__ = "SeasonInfo"
#     id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, autoincrement=True)
#     current_season = sqlalchemy.Column(sqlalchemy.Integer)
#     season_start_date = sqlalchemy.Column(sqlalchemy.String)
#     home_team = sqlalchemy.Column(sqlalchemy.String)
#     away_team = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/data/weekly_team_stats.py
# class WeeklyTeamStats(SqlAlchemyBase):
#     __tablename__ = "WeeklyTeamStats"
#     primary_key = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     season = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     week = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     team_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("TeamInfo.team_id")
#     )
#     division_rank = sqlalchemy.Column(sqlalchemy.Integer)
#     conference_rank = sqlalchemy.Column(sqlalchemy.Integer)
#     points_for = sqlalchemy.Column(sqlalchemy.Integer)
#     tiebreaker_td = sqlalchemy.Column(sqlalchemy.Integer)
#     tiebreaker_team = sqlalchemy.Column(sqlalchemy.Integer)
# 
# Path: nflpool/services/time_service.py
# class TimeService:
#     @staticmethod
#     def get_time():
#         """Create a service to get the time - there were too many instances of getting the current time in
#         the codebase making testing difficult."""
#         # Change now_time for testing
#         # Use this one for production:
#         now_time = pendulum.now(tz=pendulum.timezone("America/New_York"))
#         # Use this one for testing:
#         # now_time = pendulum.datetime(2018, 9, 18, 6, 21, tz='America/New_York')
# 
#         return now_time
# 
#     @staticmethod
#     def get_week():
#         session = DbSessionFactory.create_session()
#         season_row = session.query(SeasonInfo).filter(SeasonInfo.id == "1").first()
# 
#         season_start = pendulum.parse(season_row.season_start_date)
# 
#         diff = TimeService.get_time() - season_start
#         week = int((diff.days / 7) + 1)
# 
#         return week


# Path: nflpool/services/weekly_msf_data.py
from nflpool.data.dbsession import DbSessionFactory
from nflpool.data.weekly_nflplayer_stats import WeeklyNFLPlayerStats
from requests.auth import HTTPBasicAuth
from nflpool.data.seasoninfo import SeasonInfo
from nflpool.data.weekly_team_stats import WeeklyTeamStats
from nflpool.services.time_service import TimeService
import requests
import nflpool.data.secret as secret

            session.commit()

    @staticmethod
    def get_sack_stats():

        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/v2.0/pull/nfl/"
            + str(season)
            + "-regular/player_stats_totals.json?position=DE,DT,ILB,LB,MLB,NT,OLB&stats=Sacks",
            auth=HTTPBasicAuth(secret.msf_api, secret.msf_v2pw),
        )

        player_json = response.json()
        player_data = player_json["playerStatsTotals"]

        for players in player_data:
            try:
                player_id = players["player"]["id"]
                sacks = players["stats"]["tackles"]["sacks"]

            except KeyError:
                continue

            week = TimeService.get_week()


            weekly_player_stats = WeeklyNFLPlayerStats(