1
1
import requests
2
2
3
3
from basketball_reference_web_scraper import http_client
4
-
5
4
from basketball_reference_web_scraper .errors import InvalidSeason , InvalidDate
6
- from basketball_reference_web_scraper .output import box_scores_to_csv , schedule_to_csv , players_season_totals_to_csv , players_advanced_season_totals_to_csv , team_box_scores_to_csv
7
5
from basketball_reference_web_scraper .output import output
8
- from basketball_reference_web_scraper .json_encoders import BasketballReferenceJSONEncoder
6
+ from basketball_reference_web_scraper .writers import CSVWriter , RowFormatter , \
7
+ BOX_SCORE_COLUMN_NAMES , SCHEDULE_COLUMN_NAMES , PLAYER_SEASON_TOTALS_COLUMN_NAMES , \
8
+ PLAYER_ADVANCED_SEASON_TOTALS_COLUMN_NAMES , TEAM_BOX_SCORES_COLUMN_NAMES , PLAY_BY_PLAY_COLUMN_NAMES
9
9
10
10
11
- def player_box_scores (day , month , year , output_type = None , output_file_path = None , output_write_option = None , json_options = None ):
11
+ def player_box_scores (day , month , year , output_type = None , output_file_path = None , output_write_option = None ,
12
+ json_options = None ):
12
13
try :
13
14
values = http_client .player_box_scores (day = day , month = month , year = year )
14
15
except requests .exceptions .HTTPError as http_error :
@@ -21,13 +22,16 @@ def player_box_scores(day, month, year, output_type=None, output_file_path=None,
21
22
output_type = output_type ,
22
23
output_file_path = output_file_path ,
23
24
output_write_option = output_write_option ,
24
- csv_writer = box_scores_to_csv ,
25
- encoder = BasketballReferenceJSONEncoder ,
25
+ csv_writer = CSVWriter (
26
+ column_names = BOX_SCORE_COLUMN_NAMES ,
27
+ row_formatter = RowFormatter (data_field_names = BOX_SCORE_COLUMN_NAMES )
28
+ ),
26
29
json_options = json_options ,
27
30
)
28
31
29
32
30
- def season_schedule (season_end_year , output_type = None , output_file_path = None , output_write_option = None , json_options = None ):
33
+ def season_schedule (season_end_year , output_type = None , output_file_path = None , output_write_option = None ,
34
+ json_options = None ):
31
35
try :
32
36
values = http_client .season_schedule (season_end_year )
33
37
except requests .exceptions .HTTPError as http_error :
@@ -41,13 +45,16 @@ def season_schedule(season_end_year, output_type=None, output_file_path=None, ou
41
45
output_type = output_type ,
42
46
output_file_path = output_file_path ,
43
47
output_write_option = output_write_option ,
44
- csv_writer = schedule_to_csv ,
45
- encoder = BasketballReferenceJSONEncoder ,
48
+ csv_writer = CSVWriter (
49
+ column_names = SCHEDULE_COLUMN_NAMES ,
50
+ row_formatter = RowFormatter (data_field_names = SCHEDULE_COLUMN_NAMES )
51
+ ),
46
52
json_options = json_options ,
47
53
)
48
54
49
55
50
- def players_season_totals (season_end_year , output_type = None , output_file_path = None , output_write_option = None , json_options = None ):
56
+ def players_season_totals (season_end_year , output_type = None , output_file_path = None , output_write_option = None ,
57
+ json_options = None ):
51
58
try :
52
59
values = http_client .players_season_totals (season_end_year )
53
60
except requests .exceptions .HTTPError as http_error :
@@ -60,13 +67,16 @@ def players_season_totals(season_end_year, output_type=None, output_file_path=No
60
67
output_type = output_type ,
61
68
output_file_path = output_file_path ,
62
69
output_write_option = output_write_option ,
63
- csv_writer = players_season_totals_to_csv ,
64
- encoder = BasketballReferenceJSONEncoder ,
70
+ csv_writer = CSVWriter (
71
+ column_names = PLAYER_SEASON_TOTALS_COLUMN_NAMES ,
72
+ row_formatter = RowFormatter (data_field_names = PLAYER_SEASON_TOTALS_COLUMN_NAMES )
73
+ ),
65
74
json_options = json_options ,
66
75
)
67
76
68
77
69
- def players_advanced_season_totals (season_end_year , output_type = None , output_file_path = None , output_write_option = None , json_options = None ):
78
+ def players_advanced_season_totals (season_end_year , output_type = None , output_file_path = None , output_write_option = None ,
79
+ json_options = None ):
70
80
try :
71
81
values = http_client .players_advanced_season_totals (season_end_year )
72
82
except requests .exceptions .HTTPError as http_error :
@@ -79,13 +89,16 @@ def players_advanced_season_totals(season_end_year, output_type=None, output_fil
79
89
output_type = output_type ,
80
90
output_file_path = output_file_path ,
81
91
output_write_option = output_write_option ,
82
- csv_writer = players_advanced_season_totals_to_csv ,
83
- encoder = BasketballReferenceJSONEncoder ,
92
+ csv_writer = CSVWriter (
93
+ column_names = PLAYER_ADVANCED_SEASON_TOTALS_COLUMN_NAMES ,
94
+ row_formatter = RowFormatter (data_field_names = PLAYER_ADVANCED_SEASON_TOTALS_COLUMN_NAMES )
95
+ ),
84
96
json_options = json_options ,
85
97
)
86
98
87
99
88
- def team_box_scores (day , month , year , output_type = None , output_file_path = None , output_write_option = None , json_options = None ):
100
+ def team_box_scores (day , month , year , output_type = None , output_file_path = None , output_write_option = None ,
101
+ json_options = None ):
89
102
try :
90
103
values = http_client .team_box_scores (day = day , month = month , year = year )
91
104
except requests .exceptions .HTTPError as http_error :
@@ -98,7 +111,31 @@ def team_box_scores(day, month, year, output_type=None, output_file_path=None, o
98
111
output_type = output_type ,
99
112
output_file_path = output_file_path ,
100
113
output_write_option = output_write_option ,
101
- csv_writer = team_box_scores_to_csv ,
102
- encoder = BasketballReferenceJSONEncoder ,
114
+ csv_writer = CSVWriter (
115
+ column_names = TEAM_BOX_SCORES_COLUMN_NAMES ,
116
+ row_formatter = RowFormatter (data_field_names = TEAM_BOX_SCORES_COLUMN_NAMES )
117
+ ),
118
+ json_options = json_options ,
119
+ )
120
+
121
+
122
+ def play_by_play (home_team , day , month , year , output_type = None , output_file_path = None , output_write_option = None ,
123
+ json_options = None ):
124
+ try :
125
+ values = http_client .play_by_play (home_team = home_team , day = day , month = month , year = year )
126
+ except requests .exceptions .HTTPError as http_error :
127
+ if http_error .response .status_code == requests .codes .not_found :
128
+ raise InvalidDate (day = day , month = month , year = year )
129
+ else :
130
+ raise http_error
131
+ return output (
132
+ values = values ,
133
+ output_type = output_type ,
134
+ output_file_path = output_file_path ,
135
+ output_write_option = output_write_option ,
136
+ csv_writer = CSVWriter (
137
+ column_names = PLAY_BY_PLAY_COLUMN_NAMES ,
138
+ row_formatter = RowFormatter (data_field_names = PLAY_BY_PLAY_COLUMN_NAMES )
139
+ ),
103
140
json_options = json_options ,
104
141
)
0 commit comments