-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_handler.py
More file actions
139 lines (122 loc) · 4.97 KB
/
export_handler.py
File metadata and controls
139 lines (122 loc) · 4.97 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
132
133
134
135
136
137
138
139
# export_handler is responsible for exporting
import csv
import json
from datetime import datetime
from ui_handler import texts
now = datetime.now()
def export(sport, isCourtCV, filename=f'export_{now.strftime("%d%m%Y_%H%M%S")}.csv'):
if not isCourtCV:
if sport == 1:
try:
with open('basketball.json', 'r', encoding='utf-8') as f:
data = json.load(f)
except FileNotFoundError:
print(f"{texts["file_not_found"]}: basketball.json")
input(f'{texts["enter_to_continue"]}...')
return
except Exception as e:
print(f'ERROR: {e}')
input(f'{texts["enter_to_continue"]}...')
return
games = data['games']
if not games:
print(f'{texts["no_games"]}')
return
headers = ['Date', 'Opponent', 'POS', 'MIN', 'PTS', 'AST', '2PTA', '2PTM', '3PTA', '3PTM', 'REB', 'BLK', 'STL', 'PF',
'Missed Free Throws', 'Turnovers', 'Result']
key_mapping = {
'Date': 'date',
'Opponent': 'opponent',
'POS': 'position',
'MIN': 'minutes',
'PTS': 'points',
'AST': 'assists',
'2PTA': '2pt_attempts',
'2PTM': '2pt_shots_made',
'3PTA': '3pt_attempts',
'3PTM': '3pt_shots_made',
'REB': 'rebounds',
'BLK': 'blocks',
'STL': 'steals',
'PF': 'personal_fouls',
'Missed Free Throws': 'missed_free_throws',
'Turnovers': 'turnovers',
'Result': 'result'
}
with open(filename, 'w', newline='', encoding='utf-8') as csvf:
writer = csv.DictWriter(csvf, fieldnames=headers)
writer.writeheader()
for game in games:
row = {csv_key: game.get(json_key, '') for csv_key, json_key in key_mapping.items()}
writer.writerow(row)
if sport == 2:
try:
with open('soccer.json', 'r', encoding='utf-8') as f:
data = json.load(f)
except FileNotFoundError:
print(f"{texts["file_not_found"]}: soccer.json")
input(f'{texts["enter_to_continue"]}...')
return
except Exception as e:
print(f'ERROR: {e}')
input(f'{texts["enter_to_continue"]}...')
return
games = data['games']
if not games:
print(f'{texts["no_games"]}')
return
headers = ['Date', 'Opponent', 'POS', 'MIN', 'GOALS', 'AST', 'SHOTS', 'SAVES', 'STEALS', 'Yellow cards', 'Red cards', 'Fouls', 'Result']
key_mapping = {
'Date': 'date',
'Opponent': 'opponent',
'POS': 'position',
'MIN': 'minutes',
'GOALS': 'goals',
'AST': 'assists',
'SHOTS': 'shots',
'SAVES': 'saves',
'STEALS': 'steals',
'Yellow cards': 'yellow_cards',
'Red cards': 'red_cards',
'Fouls': 'fouls',
'Result': 'result'
}
with open(filename, 'w', newline='', encoding='utf-8') as csvf:
writer = csv.DictWriter(csvf, fieldnames=headers)
writer.writeheader()
for game in games:
row = {csv_key: game.get(json_key, '') for csv_key, json_key in key_mapping.items()}
writer.writerow(row)
print(f'{texts["export_finished"]} {filename}')
input(f'{texts["enter_to_continue"]}...')
else: #ccv mode
try:
with open('basketball.json', 'r', encoding='utf-8') as f:
data = json.load(f)
except FileNotFoundError:
print(f"{texts["file_not_found"]}: basketball.json")
input(f'{texts["enter_to_continue"]}...')
return
except Exception as e:
print(f'ERROR: {e}')
input(f'{texts["enter_to_continue"]}...')
return
games = data['games']
if not games:
print(f'{texts["no_games"]}')
return
headers = ['Date', 'Opponent', 'PTS', 'REB', 'AST', 'MIN']
key_mapping = {
'Date': 'date',
'Opponent': 'opponent',
'PTS': 'points',
'REB': 'rebounds',
'AST': 'assists',
'MIN': 'minutes',
}
with open('courtcv_input.csv', 'w', newline='', encoding='utf-8') as csvf:
writer = csv.DictWriter(csvf, fieldnames=headers)
writer.writeheader()
for game in games:
row = {csv_key: game.get(json_key, '') for csv_key, json_key in key_mapping.items()}
writer.writerow(row)