Esempio n. 1
0
def player_box_scores(day,
                      month,
                      year,
                      output_type=None,
                      output_file_path=None,
                      output_write_option=None,
                      json_options=None):
    try:
        http_service = HTTPService(parser=ParserService())
        values = http_service.player_box_scores(day=day,
                                                month=month,
                                                year=year)
    except requests.exceptions.HTTPError as http_error:
        if http_error.response.status_code == requests.codes.not_found:
            raise InvalidDate(day=day, month=month, year=year)
        else:
            raise http_error

    options = OutputOptions.of(
        file_options=FileOptions.of(path=output_file_path,
                                    mode=output_write_option),
        output_type=output_type,
        json_options=json_options,
        csv_options={"column_names": BOX_SCORE_COLUMN_NAMES})
    output_service = OutputService(
        json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder),
        csv_writer=CSVWriter(value_formatter=format_value))
    return output_service.output(data=values, options=options)
Esempio n. 2
0
def regular_season_player_box_scores(player_identifier,
                                     season_end_year,
                                     output_type=None,
                                     output_file_path=None,
                                     output_write_option=None,
                                     json_options=None):
    try:
        http_service = HTTPService(parser=ParserService())
        values = http_service.regular_season_player_box_scores(
            player_identifier=player_identifier,
            season_end_year=season_end_year,
        )
    except requests.exceptions.HTTPError as http_error:
        if http_error.response.status_code == requests.codes.internal_server_error \
                or http_error.response.status_code == requests.codes.not_found:
            raise InvalidPlayerAndSeason(player_identifier=player_identifier,
                                         season_end_year=season_end_year)
        else:
            raise http_error
    options = OutputOptions.of(
        file_options=FileOptions.of(path=output_file_path,
                                    mode=output_write_option),
        output_type=output_type,
        json_options=json_options,
        csv_options={"column_names": PLAYER_SEASON_BOX_SCORE_COLUMN_NAMES})
    output_service = OutputService(
        json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder),
        csv_writer=CSVWriter(value_formatter=format_value))
    return output_service.output(data=values, options=options)
Esempio n. 3
0
def players_advanced_season_totals(season_end_year,
                                   include_combined_values=False,
                                   output_type=None,
                                   output_file_path=None,
                                   output_write_option=None,
                                   json_options=None):
    try:
        http_service = HTTPService(parser=ParserService())
        values = http_service.players_advanced_season_totals(
            season_end_year, include_combined_values=include_combined_values)
    except requests.exceptions.HTTPError as http_error:
        if http_error.response.status_code == requests.codes.not_found:
            raise InvalidSeason(season_end_year=season_end_year)
        else:
            raise http_error
    options = OutputOptions.of(file_options=FileOptions.of(
        path=output_file_path, mode=output_write_option),
                               output_type=output_type,
                               json_options=json_options,
                               csv_options={
                                   "column_names":
                                   PLAYER_ADVANCED_SEASON_TOTALS_COLUMN_NAMES
                               })
    output_service = OutputService(
        json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder),
        csv_writer=CSVWriter(value_formatter=format_value))
    return output_service.output(data=values, options=options)
Esempio n. 4
0
def search(term,
           output_type=None,
           output_file_path=None,
           output_write_option=None,
           json_options=None):
    http_service = HTTPService(parser=ParserService())
    values = http_service.search(term=term)
    options = OutputOptions.of(
        file_options=FileOptions.of(path=output_file_path,
                                    mode=output_write_option),
        output_type=output_type,
        json_options=json_options,
        csv_options={"column_names": SEARCH_RESULTS_COLUMN_NAMES})
    output_service = OutputService(
        json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder),
        csv_writer=SearchCSVWriter(value_formatter=format_value))
    return output_service.output(data=values, options=options)
Esempio n. 5
0
def standings(season_end_year,
              output_type=None,
              output_file_path=None,
              output_write_option=None,
              json_options=None):
    try:
        http_service = HTTPService(parser=ParserService())
        values = http_service.standings(season_end_year=season_end_year)
    except requests.exceptions.HTTPError as http_error:
        if http_error.response.status_code == requests.codes.not_found:
            raise InvalidSeason(season_end_year=season_end_year)
        else:
            raise http_error
    options = OutputOptions.of(
        file_options=FileOptions.of(path=output_file_path,
                                    mode=output_write_option),
        output_type=output_type,
        json_options=json_options,
        csv_options={"column_names": STANDINGS_COLUMNS_NAMES})
    output_service = OutputService(
        json_writer=JSONWriter(value_formatter=BasketballReferenceJSONEncoder),
        csv_writer=CSVWriter(value_formatter=format_value))
    return output_service.output(data=values, options=options)
Esempio n. 6
0
 def setUp(self):
     self.mock_encoder = mock.Mock()
     self.mock_data = ["some data"]
     self.writer = JSONWriter(value_formatter=self.mock_encoder)
Esempio n. 7
0
class TestJSONWriter(TestCase):
    def setUp(self):
        self.mock_encoder = mock.Mock()
        self.mock_data = ["some data"]
        self.writer = JSONWriter(value_formatter=self.mock_encoder)

    @mock.patch("json.dumps")
    def test_writing_to_memory_with_default_options(self, json_dumps):
        options = mock.Mock(
            formatting_options={},
            file_options=mock.Mock(
                path="some path",
                mode=OutputWriteOption.WRITE,
                should_write_to_file=False,
            ),
        )

        self.writer.write(data=self.mock_data, options=options)
        json_dumps.assert_called_once_with(
            self.mock_data,
            cls=self.mock_encoder,
            sort_keys=True,
            indent=4,
        )

    @mock.patch("json.dumps")
    def test_writing_to_memory_with_custom_options(self, json_dumps):
        options = mock.Mock(
            formatting_options={
                "jae": "baebae",
                "bae": "jadley",
            },
            file_options=mock.Mock(
                path="some path",
                mode=OutputWriteOption.WRITE,
                should_write_to_file=False,
            )
        )

        self.writer.write(data=self.mock_data, options=options)
        json_dumps.assert_called_once_with(
            self.mock_data,
            cls=self.mock_encoder,
            sort_keys=True,
            indent=4,
            jae="baebae",
            bae="jadley",
        )

    @mock.patch("json.dump")
    def test_writing_to_file_with_default_options(self, json_dump):
        with mock.patch("builtins.open", mock.mock_open()) as mock_file:
            options = mock.Mock(
                formatting_options={},
                file_options=mock.Mock(
                    path="some path",
                    mode=OutputWriteOption.WRITE,
                    should_write_to_file=True,
                ),
            )

            self.writer.write(data=self.mock_data, options=options)
            mock_file.assert_called_once_with("some path", OutputWriteOption.WRITE.value, newline="", encoding="utf8")
            json_dump.assert_called_once_with(
                self.mock_data,
                mock_file(),
                cls=self.mock_encoder,
                sort_keys=True,
                indent=4,
            )

    @mock.patch("json.dump")
    def test_writing_to_file_with_custom_options(self, json_dump):
        with mock.patch("builtins.open", mock.mock_open()) as mock_file:
            options = mock.Mock(
                formatting_options={
                    "jae": "baebae",
                    "bae": "jadley",
                },
                file_options=mock.Mock(
                    path="some path",
                    mode=OutputWriteOption.WRITE,
                    should_write_to_file=True,
                ),
            )
            self.writer.write(data=self.mock_data, options=options)
            mock_file.assert_called_once_with("some path", OutputWriteOption.WRITE.value, newline="", encoding="utf8")
            json_dump.assert_called_once_with(
                self.mock_data,
                mock_file(),
                cls=self.mock_encoder,
                sort_keys=True,
                indent=4,
                jae="baebae",
                bae="jadley",
            )