GunRunMaps/create_index.py

133 lines
4.6 KiB
Python
Executable File

#!/usr/bin/python3
import glob, fileinput, os, re, shutil, sys, tempfile, time
import pprint
year = 2023
webfile_dir = "~/public_html/GunRun{}/".format(year)
print("NOTE: year is {}".format(year))
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
webfile_dir = os.path.expanduser(webfile_dir)
for root, dirs, files in os.walk(webfile_dir):
documents=[]
for file in files:
if re.search('.(pdf|png|jpg|jpeg|htm|html|mp4|pptx)$', file) and not re.search('index.html', file):
try:
documents.append(os.path.join(file))
except OSError:
pass
canonical=[]
archive=[]
marshals=[]
for file in documents:
name=os.path.basename(file)
if re.search('^\d{8}', name):
archive.append(name)
elif re.search('_marshal_map_', name):
marshals.append(name)
else:
canonical.append(name)
most_recent = {}
for file in natural_sort(archive):
m = re.search("^(\d{{8}}) - (Gun Run {} - .*?)(?:\s*\d+(?:\.\d+)?)?(\.\S+)$".format(year), file, re.IGNORECASE)
if m:
date = m.group(1)
name = m.group(2) + m.group(3)
current = most_recent.get(name)
if current is None or date >= current.get('date'):
most_recent[name] = { 'date': date, 'file': file }
for k, v in most_recent.items():
canon = os.path.join(webfile_dir, k)
#file = os.path.join(webfile_dir, v.get('file'))
# No need for directory, since it's in the same directory
file = v.get('file')
#print("Creating symlink from {} to {}".format(file, canon))
if os.path.isfile(canon):
os.unlink(canon)
os.symlink(file, canon)
if k not in canonical:
canonical.append(k)
indexfile = open(os.path.join(webfile_dir, root, "index.html"), "w")
output = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Gun Run {} Maps</title>
</head>
<body>
<h1>Gun Run {} Maps</h1>
""".format(year, year)
if canonical:
output += """<p>Maps</p><ul>"""
for link in natural_sort(canonical):
overview=re.search("Marshal overview", link)
if overview is not None:
continue
type=re.search("^Gun Run {} - (.*?)\.\S+$".format(year), link, re.IGNORECASE)
if type is not None:
name=type.group(1)
else:
name=link
output += """<li><a href="{}">{}</a></li>""".format(link, name)
output += """</ul>"""
if dirs:
output +="""<p>Clubs</p><ul>"""
for link in natural_sort(dirs):
if not os.path.islink(link):
output += """<li><a href="{}">{}</a></li>""".format(link, link)
output += """</ul>"""
if archive:
archive=[i.split(' - ') for i in archive]
#archive.sort(key=lambda x: (x[2], x[0]))
archive.sort(key=lambda x: (re.split('\s*v?[\d\.]+\w+$', x[2])[0], x[0]))
archive = [' - '.join(i) for i in archive]
output +="""<p>Source Maps</p><ul>"""
for link in archive:
output += """<li><a href="{}">{}</a></li>""".format(link, link)
output += """</ul>"""
if marshals:
output +="""<p>Marshal maps</p><table style="border-collapse: collapse;">"""
marshals.sort(key=lambda x: re.sub('^.*_(\d{2}\w)\..{3,6}?$', '\g<1>', x))
positions={}
for link in natural_sort(marshals):
m = re.search('^(.*?).(pdf|png|jpg|jpeg)$', link)
if m is not None:
positions.setdefault(m.group(1), {})[m.group(2)] = link
for name, ext in positions.items():
day = re.sub('^(\d+)_.*', '\g<1>', name)
num = re.sub('(?:\d+_)\D+', '', name)
if day == "1":
day = "Saturday"
else:
day = "Sunday"
output += """<tr style="border-bottom: 1pt solid black">"""
output += """<td style="padding: 4px 12px;">{}</td>""".format(day)
output += """<td style="padding: 4px 12px;">{}</td>""".format(num)
for link in ext.values():
output += """<td style="padding: 4px 12px;"><a href="{}">{}</a></td>""".format(link, link)
output += """<tr>"""
output += """<table>"""
output += """</body></html>"""
indexfile.write(output)
indexfile.close()
# vim: set expandtab shiftwidth=4 softtabstop=4 :