-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseball_api_client.py
More file actions
131 lines (115 loc) · 5.99 KB
/
baseball_api_client.py
File metadata and controls
131 lines (115 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from utils.io import *
from utils.endpoints import *
def main():
# Parse command line arguments
args = get_command_line_args()
# Load/validate configuration
config = load_config(config_path = args.config)
# Validate arguments and determine request type
request_type = None
if(args.date is not None):
if not validate_date(args.date):
raise ValueError(f"Invalid date format: {args.date}. Use YYYY-MM-DD.")
if(args.season is not None):
if not validate_season(args.season):
raise ValueError(f"Invalid season format: {args.season}. Use a four-digit year like 2024.")
if(args.data_type is not None):
if not validate_data_type(args.data_type, config):
raise ValueError(f"Invalid data_type: {args.data_type}. Must be one of: {', '.join(sorted(validate_data_type.__defaults__[1]['ENDPOINTS'].keys()))}")
request_type = get_request_type(args, config)
if request_type is None:
# Manually create the data request via interactive menu
selection = ''
while True:
# Display main menu and get selection
display_main_menu(config)
selection = input("Enter selection by number (Q to quit): ")
if selection.lower() == 'q':
print("Exiting.")
return
# Validate selection
if not selection.isdigit():
print("Invalid selection. Please enter a number.")
continue
request_type, data_type = get_selection(selection, config)
if request_type is None or data_type is None:
print("Invalid selection. Please try again.")
continue
if not validate_data_type(data_type, config):
print(f"Invalid data_type: {data_type}. Must be one of: {', '.join(sorted(validate_data_type.__defaults__[1]['ENDPOINTS'].keys()))}")
continue
# Handle Daily Requests
if request_type == 'Daily':
year, month, day = get_date_input()
if year is None or month is None or day is None:
print("Invalid date input. Required format is YYYY-MM-DD.")
continue
print(f"{request_type} Request for {data_type}: {year}-{month}-{day}:")
df = get_daily_data(year, month, day, config, data_type)
print(df)
output_file = f"output/{output_filename(data_type, year, month, day)}.csv"
# Handle Transactions Requests
elif request_type == 'Transactions':
year, month, day = get_date_input()
if year is None or month is None or day is None:
print("Invalid date input. Required format is YYYY-MM-DD.")
continue
print(f"{request_type} Request for {data_type}: {year}-{month}-{day}:")
df = get_transactions_data(year, month, day, config)
print(df)
output_file = f"output/{output_filename(data_type, year, month, day)}.csv"
# Handle League Requests
elif request_type == 'League':
print(f"{request_type} Request for {data_type}:")
df = get_league_data(config, data_type)
print(df)
output_file = f"output/{output_filename(data_type)}.csv"
# Handle Seasonal Requests
elif request_type == 'Seasonal':
season, season_type = get_season_input()
if season is None or season_type is None:
print("Invalid season input. Required format is a four-digit year like 2024. Season type must be either 'REG' or 'PRE'.")
continue
print(f"{request_type} Request for {data_type}: {season} {season_type}")
df = get_season_data(season, config, data_type)
print(df)
output_file = f"output/{output_filename(data_type, season=season, season_type=season_type)}.csv"
df.to_csv(output_file, index=False)
print(f"Data exported to {output_file}")
else:
# Use command line arguments to make request
if request_type == 'Daily':
data_type = args.data_type
year, month, day = args.date.split("-")
print(f"{request_type} Request for {args.data_type}: {year}-{month}-{day}:")
df = get_daily_data(year, month, day, config, data_type)
print(df)
output_file = f"output/{output_filename(data_type, year, month, day)}.csv"
elif request_type == 'Transactions':
data_type = args.data_type
year, month, day = args.date.split("-")
print(f"{request_type} Request for {args.data_type}: {year}-{month}-{day}:")
df = get_transactions_data(year, month, day, config)
print(df)
output_file = f"output/{output_filename(data_type, year, month, day)}.csv"
elif request_type == 'League':
data_type = args.data_type
print(f"{request_type} Request for {args.data_type}:")
df = get_league_data(config, data_type)
print(df)
output_file = f"output/{output_filename(data_type, year, month, day)}.csv"
elif request_type == 'Seasonal':
data_type = args.data_type
season = args.season
season_type = args.season_type
print(f"{request_type} Request for {args.data_type}: {season} {season_type}")
df = get_season_data(season, config, data_type)
print(df)
output_file = f"output/{output_filename(data_type, season=season, season_type=season_type)}.csv"
else:
raise ValueError(f"Could not determine request type for data_type: {args.data_type}")
# Output to CSV
df.to_csv(output_file, index=False)
print(f"Data exported to {output_file}")
if __name__ == "__main__":
main()