"""
import boto3
from boto3.dynamodb.conditions import Key
+import os
import re
+import sys
+
+WEBROOT = "/srv/halibut.cworth.org/www"
website = "https://halibut.cworth.org/"
#change this if we're using AWS or some other subdomain instead
+def hunt_file(hunt, name):
+ """Return a path file 'name' within the given hunt.
+
+ This will be withing WEBROOT and in a hunt-specific path."""
+
+ return "{}/{}/{}".format(WEBROOT, hunt['channel_id'], name)
+
def filename_from_name(name):
"""Returns a string derived from name, but with all spaces and slashes
replaced with underscores, (for making a clean filename)"""
rounds = list(rounds)
rounds.sort()
- return puzzles, rounds
+ return hunt, puzzles, rounds
def round_stat(rnd, puzzles):
#Counts puzzles, solved, list of puzzles for a given round
-def overview(puzzles, rounds):
+def overview(hunt, puzzles, rounds):
#big board, main page. saves as index.html
start = ['<!DOCTYPE html>\n',
'<html>\n',
columns.append(' </div>\n')
end = ['</body>\n', '</html>\n']
html = start + expanding + columns + end
- file = "index.html"
+ file = hunt_file(hunt, "index.html")
f = open(file, "w")
for line in html:
f.write(line)
f.close()
return None
-def round_overview(rnd, puzzles):
+def round_overview(hunt, rnd, puzzles):
#inputs: round name, puzzles
#round overview page
#saves as (round name)_round.html, in case meta/round share names.
' </body>\n',
'</html>\n']
html = start + puzzle_list + end
- file = "{}_round.html".format(filename_from_name(rnd))
+ file = hunt_file(hunt, "{}_round.html".format(filename_from_name(rnd)))
f = open(file, "w")
for line in html:
f.write(line)
f.close()
return None
-def puzzle_overview(puzzle):
+def puzzle_overview(hunt, puzzle):
#overview page for individual puzzles. saves as (name of puzzle).html,
#with underscores rather than spaces
name = puzzle['name']
'\n',
'</body>\n',
'</html>\n']
- file = "{}.html".format(filename_from_name(name))
+ file = hunt_file(hunt, "{}.html".format(filename_from_name(name)))
f = open(file, "w")
for line in html:
f.write(line)
return None
-def puzzle_lists(puzzles, filt):
+def puzzle_lists(hunt, puzzles, filt):
#filt is one of "All", "Solved", "Unsolved" and spits out the appropriate list of puzzles
#generates pages for all puzzles, solved puzzles, unsolved puzzles
#saves as all/solved/unsolved.html, has a sidebar to link to each other and to the big board
meta = ''
slack_url = channel_url(puzzle['channel_id'])
if 'rounds' in puzzle:
- round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
+ round_url = link(website + filename_from_name(puzzle['rounds'][0]) + "_round.html", puzzle['rounds'][0])
else:
round_url = ''
#assuming one round per puzzle for now
meta = ''
slack_url = channel_url(puzzle['channel_id'])
if 'rounds' in puzzle:
- round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
+ round_url = link(website + filename_from_name(puzzle['rounds'][0]) + "_round.html", puzzle['rounds'][0])
else:
round_url = ''
#assuming one round per puzzle for now
' </body>\n',
'</html>\n']
if filt == "All":
- file1 = 'all.html'
+ file1 = hunt_file(hunt, 'all.html')
f = open(file1, "w")
for line in start + unsolved_code + solved_code + end:
f.write(line)
f.close()
elif filt == "Solved":
- file2 = 'solved.html'
+ file2 = hunt_file(hunt, 'solved.html')
f = open(file2, 'w')
for line in start + solved_code + end:
f.write(line)
f.close()
elif filt == "Unsolved":
- file3 = 'unsolved.html'
+ file3 = hunt_file(hunt, 'unsolved.html')
f = open(file3, 'w')
for line in start + unsolved_code + end:
f.write(line)
f.close()
return None
-# Initialize AWS resources to talk to database
+def generate_for_hunt_id(table, hunt_id):
+ hunt, puzzles, rounds = hunt_info(table, hunt_id)
+
+ # Create a directory for the hunt in the WEBROOT
+ root = hunt_file(hunt, "")
+ try:
+ os.mkdir(root)
+ except FileExistsError:
+ # We're happy as a clam if the directory already exists
+ pass
+
+ overview(hunt, puzzles, rounds)
+ for rnd in rounds:
+ round_overview(hunt, rnd, puzzles)
+ for puzzle in puzzles:
+ puzzle_overview(hunt, puzzle)
+ puzzle_lists(hunt, puzzles, "All")
+ puzzle_lists(hunt, puzzles, "Solved")
+ puzzle_lists(hunt, puzzles, "Unsolved")
+
+# Initialize AWS resources to talk to the database
db = boto3.resource('dynamodb')
table = db.Table("turbot")
-puzzles, rounds = hunt_info(table, "mh2021")
-
-overview(puzzles, rounds)
-for rnd in rounds:
- round_overview(rnd, puzzles)
-for puzzle in puzzles:
- puzzle_overview(puzzle)
-puzzle_lists(puzzles, "All")
-puzzle_lists(puzzles, "Solved")
-puzzle_lists(puzzles, "Unsolved")
+
+def usage():
+ print("Usage: {} hunt_id [...]")
+ print("")
+ print("Generates pages (under {}) ".format(WEBROOT))
+ print("for the specified hunt_id(s).")
+
+if len(sys.argv) < 2:
+ usage()
+ sys.exit(1)
+
+for hunt_id in sys.argv[1:]:
+ generate_for_hunt_id(table, hunt_id)