"""Builds html pages for frontend Assumes a directory called albums containing directories with base64 encoded album names. """ # TODO: update README.txt with info on what this script does import os from glob import glob import base64 import xml.etree.ElementTree as ElementTree from PIL import Image # uses the Pillow fork for Python 3 def __index_html_head(): return """ Project Apollo Archive - IPFS archive

Project Apollo Archive

""" def __index_html_tail(): return """ """ def __index_html_album(thumbnail_image, album_page_link, title): return """ """ def __html_head(album_title): return """ """ + album_title + """ """ def __html_tail(): return """ """ def __album_html_head(album_name): return """

""" + album_name + """

""" def __figure_html(photo_path, thumbnail_path, title, caption, width, height): return """
Image description
""" + title + ": " + caption + """
""" image_path = 'albums' frontend_path = 'frontend' thumbnail_path = frontend_path + '/' + 'thumbnails' thumbnail_size = 128, 128 # create directory for thumbnails if necessary if not os.path.exists(os.path.join(thumbnail_path)): os.makedirs(os.path.join(thumbnail_path)) # create directory for frontend pages if necessary if not os.path.exists(os.path.join(frontend_path, 'pages')): os.makedirs(os.path.join(frontend_path, 'pages')) index_file = open(os.path.join(frontend_path, 'frontend.html'), 'w') index_file.truncate() index_file.write(__index_html_head()) #files = [y for x in os.walk(image_path) for y in glob(os.path.join(x[0], '*.jpg'))] subdirs = [(x[0] + "/").replace('\\', '/').split('/')[1] for x in os.walk(image_path)] # sort albums by mission, then by magazine apollo_mission_numbers = [] magazine_numbers = [] for subdir in subdirs: title = str(base64.b64decode(subdir), 'utf-8') # populate mission number array if "Apollo" not in title: apollo_mission_numbers.append(int(0)) else: apollo_mission_numbers.append(int(title.split(' ')[1])) # populate magazine number array if "Magazine" not in title: magazine_numbers.append(int(0)) else: magazine_numbers.append(int(title.split(' ')[3].split('/')[0])) subdirs_sorted = list(zip(subdirs, apollo_mission_numbers, magazine_numbers)) subdirs_sorted = sorted(subdirs_sorted, key=lambda x: (x[1], x[2])) #subdirs.sort(key=base64.b64decode) # string sort the list of subdirectories for subdir in subdirs_sorted: album = subdir[0] if album == "": continue album_page = os.path.join(frontend_path, "pages", album + '.html') output_file = open(album_page, 'w') output_file.truncate() output_file.write(__html_head(str(base64.b64decode(album), 'utf-8'))) output_file.write(__album_html_head(str(base64.b64decode(album), 'utf-8'))) print("Album " + str(base64.b64decode(album), 'utf-8')) # get list of files in album files = [y for x in os.walk(os.path.join(image_path, album)) for y in glob(os.path.join(x[0], '*.jpg'))] titles = [[None] for i in range(len(files))] # sort file list based on title from xml image_counter = 0 for image in files: e = ElementTree.parse(image + '.xml').getroot() title = e.find('photo/title').text titles[image_counter] = title image_counter += 1 title_sort_index = sorted(range(len(titles)), key=titles.__getitem__) sorted_files = [files[i] for i in title_sort_index] image_counter = 0 for file in sorted_files: image_counter += 1 e = ElementTree.parse(file + '.xml').getroot() title = e.find('photo/title').text print("Processing image " + title) description = e.find('photo/description').text path = '../../' + file.replace('\\', '/') # generate thumbnail if it doesn't already exist thumbnail_filepath = os.path.join(thumbnail_path, os.path.basename(file) + "._t.jpg") width, height = 0, 0 try: im = Image.open(file) width, height = im.size except: continue if not os.path.isfile(thumbnail_filepath): try: im.thumbnail(thumbnail_size) im.save(thumbnail_filepath, "JPEG") except: print("could not create thumbnail for " + file) thumbnail_uri = '../' + thumbnail_filepath.replace('\\', '/') output_file.write(__figure_html(str(path), str('../' + thumbnail_uri), str(title), str(description), width, height)) if image_counter == 1: # use first thumbnail as thumbnail for album index_file.write(__index_html_album(str(thumbnail_uri), 'pages/' + album + '.html', str(base64.b64decode(album), 'utf-8'))) output_file.write(__album_html_tail()) output_file.write(__html_tail()) output_file.close() index_file.write(__index_html_tail()) index_file.close()