-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_meets_html.py
More file actions
257 lines (207 loc) · 9.24 KB
/
Copy pathcsv_to_meets_html.py
File metadata and controls
257 lines (207 loc) · 9.24 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import csv
import os
def csv_to_html(csv_filename, output_folder):
# Derive the HTML filename by replacing the CSV extension with '.html' in the meets folder
html_filename = os.path.join(output_folder, os.path.splitext(os.path.basename(csv_filename))[0] + '.html')
# try:
with open(csv_filename, mode='r', newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
rows = list(reader)
# Ensure there are at least 5 rows for valid HTML generation
if len(rows) < 5:
print("CSV file must have at least 5 rows.")
return
# Extract values from the first five rows
link_text = rows[0][0]
h2_text = rows[1][0]
link_url = rows[2][0]
summary_text = rows[3][0]
# Initialize HTML content
html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{link_text}</title>
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<a href = "#main">Skip to Main Content</a>
<nav>
<ul>
<li><a href="index.html">Home Page</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#team-results">Team Results</a></li>
<li><a href="#individual-results">Individual Results</a></li>
<li><a href="#gallery">Gallery</a></li>
</ul>
</nav>
<header>
<!--Meet Info-->
<h1><a href="{link_url}">{link_text}</a></h1>
<h2>{h2_text}</h2>
</header>
<main id = "main">
<section class="summary" id = "summary">
<h2>Race Summary</h2>
{summary_text}
</section>
"""
# Start container for individual results
html_content += """<section id="team-results">\n
<h2>Team Results</h2>"""
# Process the remaining rows (after the first five)
html_content += """<table>\n"""
table_start = True
for row in rows[4:]:
# For rows that are 3 columns wide, add to the team places list
if len(row) == 3:
if row[0] == "Place":
html_content += f"<tr><th>{row[0]}</th><th>{row[1]}</th><th>{row[2]}</th></tr>\n"
else:
html_content += f"<tr><td>{row[0]}</td><td>{row[1]}</td><td> {row[2]}</td></tr>\n"
# For rows that are 8 columns wide and contain 'Ann Arbor Skyline' in column 6
elif len(row) == 8 and row[5].strip().lower() == 'ann arbor skyline':
if table_start == True:
table_start = False
html_content += "</table>\n"
html_content += """</section>\n
<section id="individual-results">\n
<h2>Individual Results</h2>"""
place = row[0]
grade = row[1]
name = row[2]
time = row[4]
profile_pic = row[7]
# Add the athlete div
html_content += f"""
<div class="athlete">
<figure>
<img src="../images/profiles/{profile_pic}" width="200" alt="Profile picture of {name}">
<figcaption>{name}</figcaption>
</figure>
<details>
<summary>Athlete Stats</summary>
<p><strong>Place:</strong> {place}</p>
<p><strong>Time:</strong> {time}</p>
<p><strong>Grade:</strong> {grade}</p>
</details>
</div>
"""
html_content += """</section>\n
<section id = "gallery">
<h2>Gallery</h2>
<details>
<summary>View Gallery</summary>
<img src="../images/meets/holly/09-14 Holly Invitational 01.jpg" alt="Group shot of Ann Arbor Skyline Men's Division at Holly Invitational before race begins.">
<img src="../images/meets/holly/09-14 Holly Invitational 02.jpg" alt="Group shot of Ann Arbor Skyline Men's Division running at Holly Invitational.">
<img src="../images/meets/holly/09-14 Holly Invitational 03.jpg" alt="Close up of a determined athlete running at Holly Invitational.">
<img src="../images/meets/holly/09-14 Holly Invitational 04.jpg" alt="Two athletes running at Holly Invitational, faces concentrated.">
<img src="../images/meets/holly/09-14 Holly Invitational 05.jpg" alt="Another athlete overtakes two Skyline athletes at the Holly Invitational.">
<img src="../images/meets/holly/20240914_124622_002_01.jpg" alt="Women's team running at Holly Invitational. Athlete in the lead rounds the corner of a tree. ">
<img src="../images/meets/holly/20240914_124642_004_01.jpg" alt="Many athletes quicken their pace to catch up at the Women's Holly Invitational.">
<img src="../images/meets/holly/20240914_124642_019_01.jpg" alt="Close up of athletes running. One athlete's hair flows behind her as she runs.">
<img src="../images/meets/holly/20240914_124622_002_01.jpg" alt="A determined athlete runs at the women's Holly Invitational.">
<img src="../images/meets/holly/IMG_9357.jpg" alt="Three athletes run in a grassy field at the Holly Invitational.">
</details>
"""
#html_content += create_meet_image_gallery(url)
# Close the HTML document
html_content += """
</section>
</main>
<footer>
<p>
Skyline High School<br>
<address>
2552 North Maple Road<br>
Ann Arbor, MI 48103<br><br>
</address>
<a href = "https://sites.google.com/aaps.k12.mi.us/skylinecrosscountry2021/home">XC Skyline Page</a><br>
Follow us on Instagram <a href = "https://www.instagram.com/a2skylinexc/" aria-label="Instagram"><i class="fa-brands fa-instagram"></i> </a>
</footer>
</body>
</html>
"""
import re
html_content = re.sub(r'<time>', '<span class="time">', html_content)
html_content = re.sub(r'</time>', '</span>', html_content)
# Save HTML content to a file in the meets folder
with open(html_filename, 'w', encoding='utf-8') as htmlfile:
htmlfile.write(html_content)
print(f"HTML file '{html_filename}' created successfully.")
# except Exception as e:
# print(f"Error processing file: {e}")
def process_meet_files():
# Set the meets folder path
meets_folder = os.path.join(os.getcwd(), "meets")
# Search for all CSV files in the meets folder
csv_files = [f for f in os.listdir(meets_folder) if f.endswith('.csv')]
if not csv_files:
print(f"No CSV files found in folder: {meets_folder}")
return
# Process each CSV file in the meets folder
for csv_file in csv_files:
csv_file_path = os.path.join(meets_folder, csv_file)
csv_to_html(csv_file_path, meets_folder)
import re
import os
import random
# Step 1: Extract the meet ID from the URL
def extract_meet_id(url):
# Regex to extract the meet ID, which is the number right after '/meet/'
match = re.search(r"/meet/(\d+)", url)
print(f"The meet id is {match}")
if match:
print(f"REturning {match.group(1)}")
return match.group(1)
else:
raise ValueError("Meet ID not found in URL.")
# Step 2: Select 12 random photos from the folder
def select_random_photos(folder_path, num_photos=25):
# List all files in the folder
print(f"Checking {folder_path}")
all_files = os.listdir(folder_path)
# Filter out non-image files if necessary (assuming .jpg, .png, etc.)
image_files = [f for f in all_files if f.endswith(('.png', '.jpg', '.jpeg', '.gif'))]
# Ensure we have enough images to select
if len(image_files) < num_photos:
return ""
raise ValueError(f"Not enough images in the folder. Found {len(image_files)} images.")
# Select 12 random images
return random.sample(image_files, num_photos)
# Step 3: Generate HTML image tags
def generate_image_tags(image_files, folder_path):
img_tags = []
for img in image_files:
img_path = os.path.join(folder_path, img)
# print(f"The image_path is {img_path}")
img_tags.append(f'<img src=../{img_path} width = "200" alt="">')
return "\n".join(img_tags)
# Putting it all together
def create_meet_image_gallery(url):
meet_id = extract_meet_id(url)
# Define the folder path for images based on the meet ID
folder_path = f'images/meets/{meet_id}/'
print(f"The folder path is {folder_path}")
if not os.path.exists(folder_path):
return ""
raise FileNotFoundError(f"The folder {folder_path} does not exist.")
# Select 12 random photos
selected_photos = select_random_photos(folder_path)
# Generate image tags
html_image_tags = generate_image_tags(selected_photos, folder_path)
return html_image_tags
# Example usage
url = "https://www.athletic.net/CrossCountry/meet/235827/results/943367"
html_gallery = create_meet_image_gallery(url)
print(html_gallery)
if __name__ == "__main__":
# Check if meets folder exists
meets_folder = os.path.join(os.getcwd(), "meets")
if not os.path.exists(meets_folder):
print(f"Folder '{meets_folder}' does not exist.")
else:
process_meet_files()