예제 #1
0
def play_by_play(home_team, day, month, year):
    add_0_if_needed = lambda s: "0" + s if len(s) == 1 else s

    # the hard-coded `0` in the url assumes we always take the first match of the given date and team.
    url = "{BASE_URL}/boxscores/pbp/{year}{month}{day}0{team_abbr}.html".format(
        BASE_URL=BASE_URL,
        year=year,
        month=add_0_if_needed(str(month)),
        day=add_0_if_needed(str(day)),
        team_abbr=TEAM_TO_TEAM_ABBREVIATION[home_team])
    response = requests.get(url=url)
    response.raise_for_status()
    page = PlayByPlayPage(html=html.fromstring(response.content))

    play_by_plays_parser = PlayByPlaysParser(
        period_details_parser=PeriodDetailsParser(regulation_periods_count=4),
        period_timestamp_parser=PeriodTimestampParser(
            timestamp_format=PLAY_BY_PLAY_TIMESTAMP_FORMAT),
        scores_parser=ScoresParser(scores_regex=PLAY_BY_PLAY_SCORES_REGEX))

    team_name_parser = TeamNameParser(team_names_to_teams=TEAM_NAME_TO_TEAM)

    return play_by_plays_parser.parse(
        play_by_plays=page.play_by_play_table.rows,
        away_team=team_name_parser.parse_team_name(
            team_name=page.away_team_name),
        home_team=team_name_parser.parse_team_name(
            team_name=page.home_team_name))
 def test_parse_period_type_is_quarter_when_in_regulation(
         self, mocked_is_overtime):
     mocked_is_overtime.return_value = False
     self.assertEqual(
         PeriodDetailsParser(regulation_periods_count=4).parse_period_type(
             period_count="some period count"),
         PeriodType.QUARTER,
     )
 def test_parse_period_type_is_overtime_when_in_overtime(
         self, mocked_is_overtime):
     mocked_is_overtime.return_value = True
     self.assertEqual(
         PeriodDetailsParser(regulation_periods_count=4).parse_period_type(
             period_count="some period count"),
         PeriodType.OVERTIME,
     )
 def test_parse_period_number_is_difference_between_period_count_and_regulation_periods_count(
         self, mocked_is_overtime):
     mocked_is_overtime.return_value = True
     self.assertEqual(
         PeriodDetailsParser(
             regulation_periods_count=4).parse_period_number(
                 period_count=5),
         1,
     )
 def test_parse_period_number_is_period_count_in_regulation(
         self, mocked_is_overtime):
     mocked_is_overtime.return_value = False
     self.assertEqual(
         PeriodDetailsParser(
             regulation_periods_count=4).parse_period_number(
                 period_count="some period count"),
         "some period count",
     )
    def __init__(self):
        self.team_abbreviation_parser = TeamAbbreviationParser(
            abbreviations_to_teams=TEAM_ABBREVIATIONS_TO_TEAM)
        self.league_abbreviation_parser = LeagueAbbreviationParser(
            abbreviations_to_league=LEAGUE_ABBREVIATIONS_TO_LEAGUE)
        self.location_abbreviation_parser = LocationAbbreviationParser(
            abbreviations_to_locations=LOCATION_ABBREVIATIONS_TO_POSITION, )
        self.outcome_abbreviation_parser = OutcomeAbbreviationParser(
            abbreviations_to_outcomes=OUTCOME_ABBREVIATIONS_TO_OUTCOME, )
        self.outcome_parser = PlayerBoxScoreOutcomeParser(
            outcome_abbreviation_parser=self.outcome_abbreviation_parser)
        self.period_details_parser = PeriodDetailsParser(
            regulation_periods_count=4)
        self.period_timestamp_parser = PeriodTimestampParser(
            timestamp_format=ParserService.PLAY_BY_PLAY_TIMESTAMP_FORMAT)
        self.position_abbreviation_parser = PositionAbbreviationParser(
            abbreviations_to_positions=POSITION_ABBREVIATIONS_TO_POSITION, )
        self.seconds_played_parser = SecondsPlayedParser()
        self.scores_parser = ScoresParser(
            scores_regex=ParserService.PLAY_BY_PLAY_SCORES_REGEX)
        self.search_result_name_parser = SearchResultNameParser()
        self.search_result_location_parser = ResourceLocationParser(
            resource_location_regex=ParserService.
            SEARCH_RESULT_RESOURCE_LOCATION_REGEX)
        self.team_name_parser = TeamNameParser(
            team_names_to_teams=TEAM_NAME_TO_TEAM)

        self.play_by_plays_parser = PlayByPlaysParser(
            period_details_parser=self.period_details_parser,
            period_timestamp_parser=self.period_timestamp_parser,
            scores_parser=self.scores_parser,
        )
        self.player_box_scores_parser = PlayerBoxScoresParser(
            team_abbreviation_parser=self.team_abbreviation_parser,
            location_abbreviation_parser=self.location_abbreviation_parser,
            outcome_abbreviation_parser=self.outcome_abbreviation_parser,
            seconds_played_parser=self.seconds_played_parser)
        self.player_data_parser = PlayerDataParser(
            search_result_location_parser=self.search_result_location_parser,
            league_abbreviation_parser=self.league_abbreviation_parser,
        )
        self.player_season_box_scores_parser = PlayerSeasonBoxScoresParser(
            team_abbreviation_parser=self.team_abbreviation_parser,
            location_abbreviation_parser=self.location_abbreviation_parser,
            outcome_parser=self.outcome_parser,
            seconds_played_parser=self.seconds_played_parser)
        self.player_season_totals_parser = PlayerSeasonTotalsParser(
            position_abbreviation_parser=self.position_abbreviation_parser,
            team_abbreviation_parser=self.team_abbreviation_parser,
        )
        self.player_advanced_season_totals_parser = PlayerAdvancedSeasonTotalsParser(
            team_abbreviation_parser=self.team_abbreviation_parser,
            position_abbreviation_parser=self.position_abbreviation_parser,
        )
        self.scheduled_start_time_parser = ScheduledStartTimeParser()
        self.scheduled_games_parser = ScheduledGamesParser(
            start_time_parser=self.scheduled_start_time_parser,
            team_name_parser=self.team_name_parser,
        )
        self.search_results_parser = SearchResultsParser(
            search_result_name_parser=self.search_result_name_parser,
            search_result_location_parser=self.search_result_location_parser,
            league_abbreviation_parser=self.league_abbreviation_parser,
        )
        self.team_totals_parser = TeamTotalsParser(
            team_abbreviation_parser=self.team_abbreviation_parser)
        self.division_name_parser = DivisionNameParser(divisions=Division)
        self.team_standings_parser = TeamStandingsParser(teams=Team)
        self.conference_division_standings_parser = ConferenceDivisionStandingsParser(
            division_name_parser=self.division_name_parser,
            team_standings_parser=self.team_standings_parser,
            divisions_to_conferences=DIVISIONS_TO_CONFERENCES,
        )
 def test_is_overtime_is_true_when_period_count_is_greater_than_regulation_periods_count(
         self):
     parser = PeriodDetailsParser(regulation_periods_count=4)
     self.assertTrue(parser.is_overtime(period_count=5))
 def test_is_overtime_is_false_when_period_count_is_less_than_regulation_periods_count(
         self):
     self.assertFalse(
         PeriodDetailsParser(regulation_periods_count=4).is_overtime(
             period_count=3))
 def test_is_overtime_is_false_when_period_count_is_equal_to_regulation_periods_count(
         self):
     parser = PeriodDetailsParser(regulation_periods_count=4)
     self.assertFalse(parser.is_overtime(period_count=4))