Exemplo n.º 1
0
 def get_week(self, date):
     sorted_weeks = sorted(self.league['week_from_date'])
     for start_date in sorted_weeks:
         if date < Date_Utils.string_to_date(start_date):
             date += timedelta(days=7)
         else:
             day_of_week = date.weekday()
             return self.league['week_from_date'][Date_Utils.date_to_string(
                 date - timedelta(days=day_of_week))]
Exemplo n.º 2
0
    def __init__(self):
        self.league = {}
        self.teams = {}
        self.players = {}
        self.weeks = {}
        self.NHL_teams = {}

        self.NHL_base_url = "https://statsapi.web.nhl.com/api/v1/"
        self.NHL_start_date = Date_Utils.string_to_date("2018-10-03")
    def set_week_from_date(self, start_date, end_date, long_weeks=None):
        week_from_date = {}
        my_date = Date_Utils.string_to_date(start_date)
        live_end_date = Date_Utils.string_to_date(end_date)
        my_week = 1

        while my_date < live_end_date:
            if long_weeks and my_week in long_weeks:
                # 1st half
                week_from_date[Date_Utils.date_to_string(
                    my_date)] = 'week_{}a'.format(my_week)
                # 2nd half
                my_date += timedelta(days=7)
                week_from_date[Date_Utils.date_to_string(
                    my_date)] = 'week_{}b'.format(my_week)
            else:
                week_from_date[Date_Utils.date_to_string(
                    my_date)] = 'week_{}'.format(my_week)
            my_week += 1
            my_date += timedelta(days=7)
            pass

        return week_from_date
    def check_tallied_dates(self, league_start_date, check_date):
        # build a list of all the tallied dates
        all_got_dates = []
        for week in self.crunch_dict['weeks']:
            all_got_dates.extend(self.crunch_dict['weeks'][week]['got_dates'])

        all_got_dates = [Date_Utils.string_to_date(date_string) for date_string in sorted(all_got_dates)]

        # build a list of all dates from league start date
        all_check_dates = Date_Utils.gen_dates_list(league_start_date, check_date+timedelta(days=1))

        # if there isn't an unbroken chain til the check_date, we can't tally cumulative data.
        # it doesn't really make sense to fill it all in, since that might not be what is desired.
        # so just raise an error and decide what to do after.
        for date_obj in all_check_dates:
            if date_obj not in all_got_dates:
                raise AssertionError("Not all dates up to {} have been tallied. Call data driver with date range {},{}".format(check_date, league_start_date, check_date))
    def __init__(self, **kwargs):

        self.oauth = self.get_oauth_session(
            "http://fantasysports.yahooapis.com/", kwargs['creds_json'])

        # the major components of the league are defined as object attributes that are dictionaries
        self.league = {}
        self.teams = {}
        self.players = {}
        self.weeks = {}

        # # we want to restore league from json file, so go to JSON object and do this league's restoral and return
        # if 'restoral' in kwargs:
        #     print('restoring Yahoo League object....')
        #     if 'JSON_object' not in kwargs:
        #         print('error, need to restore to json object - use JSON_object in kwargs')
        #         exit(1)
        #     my_JSON = kwargs['JSON_object']
        #     my_JSON.restore_data(self, type(self).__name__, my_JSON, basic=True)

        # to make it easy, assume that each object either initializes league, scoring categories, weeks, and teams, or loads those from stored JSON
        if 'initialize' in kwargs:
            print('initializing Yahoo League object....')
            if 'league_url' not in kwargs or 'fantasy_url' not in kwargs or 'creds_json' not in kwargs:
                print(
                    "error, need to initialize league with its URL, and yahoo's fantasy url, and a credentials file generated through OAuth."
                )
                exit(1)

            self.league.update({
                'league_url': kwargs['league_url'],
                'fantasy_url': kwargs['fantasy_url']
            })
            self.league.update({
                'league_info':
                self.parse_raw_league_data(kwargs['league_url']),
                'scoring_categories':
                self.parse_raw_scoring_categories()
            })

            # set start to usual Monday, rather than Wednesday
            # design: I store any dates as strings rather than live dates, for JSON.
            my_date = Date_Utils.string_to_date(
                self.league['league_info']['start_date']) - timedelta(days=2)
            self.league['start_date'] = Date_Utils.date_to_string(my_date)
            my_date = Date_Utils.string_to_date(
                self.league['league_info']['end_date'])
            self.league['end_date'] = Date_Utils.date_to_string(my_date)

            if 'long_weeks' in kwargs:
                self.league['week_from_date'] = self.set_week_from_date(
                    self.league['start_date'],
                    self.league['end_date'],
                    long_weeks=kwargs['long_weeks'])
            else:
                self.league['week_from_date'] = self.set_week_from_date(
                    self.league['start_date'], self.league['end_date'])

            self.league['date_from_week'] = {
                week: date
                for date, week in self.league['week_from_date'].items()
            }

            self.teams = self.parse_raw_league_teams()