diff --git a/web/SimpleWebPage.py b/web/SimpleWebPage.py index 40f335f..a6ce4e3 100755 --- a/web/SimpleWebPage.py +++ b/web/SimpleWebPage.py @@ -7,16 +7,18 @@ import os import sys import time import string +import json import urllib.parse from glob import fnmatch import base64 import random +from pprint import pprint -VERSION = "20210128" +VERSION = "20210815" IMAGE_EXTENSIONS = ["png", "gif", "jpg", "jpeg", "tif", "tiff"] AUDIO_EXTENSIONS = ["wav", "mp3", "ogg"] VIDEO_EXTENSIONS = ["mp4", "ogg", "webm"] - +SAFE_OPTS = ("hidden","title","parent","recursive","images","media","includes") def setup(): """ Setup the command line options """ @@ -115,16 +117,22 @@ def setup(): return options -def setup2HTML(opts): - return '' % ";".join( - [ - "hidden=%s" % opts.hidden, - "parent=%s" % opts.parent, - "title=%s" % urllib.parse.quote(opts.title), - "images=%s" % opts.images, - "media=%s" % opts.media, - ] - ) +def print_setup(opts): + print("Current configuration:") + pprint(setup2safe(opts)) + + +def setup2safe(opts): + safe_opts = {} + opts_dict = vars(opts) + for key in opts_dict: + if key in SAFE_OPTS: + safe_opts[key] = opts_dict[key] + return safe_opts + + +def setup2JSON(opts): + return json.dumps(setup2safe(opts)) def HTML2setup(opts): @@ -134,24 +142,18 @@ def HTML2setup(opts): with open(os.path.join(opts.path, opts.filename), "rt") as f: for l in f.readlines(): if l.find(' -1: - content = l[l.find('name="SimpleWebPageSetup"') :] - for s in content.split('"')[3].split(";"): - (k, v) = s.split("=", 1) - if k == "hidden": - opts.hidden = v == "True" - if k == "parent": - opts.parent = v == "True" - if k == "title": - opts.title = urllib.parse.unquote(v) - if k == "images": - opts.images = v == "True" - if k == "media": - opts.media = v == "True" + content = l[l.find('content=') :].rstrip("'/>\n") + config = json.loads(content[9:]) + for key in config: + if key in SAFE_OPTS: + setattr(opts, key, config[key]) read_config = True - print("Reading options from existing " + opts.filename) + print("Read options from existing " + opts.filename) break return (opts, read_config) - except: + except Exception as e: + print("Error parsing configuration") + print(e) return (opts, False) @@ -187,7 +189,6 @@ def generate_index(opts): if opts.password != None: opts.password_filename = opts.filename opts.filename = generate_password_page(path, opts.filename, opts.password) - existing_config = False if opts.filename in files: opts, existing_config = HTML2setup(opts) if not existing_config and not opts.overwrite: @@ -196,6 +197,9 @@ def generate_index(opts): + " exists, and not generated with SimpleWebPage. Exiting." ) sys.exit(1) + # Re-read files, with probably changed opts + dirs, files, path = get_files_and_folders(opts) + print_setup(opts) files = [f for f in files if f != opts.filename] files = [f for f in files if f != opts.password_filename] files = match_files(files, opts.includes) @@ -212,7 +216,7 @@ def generate_index(opts): for fi in files: f.write(get_filelink(path, fi, opts.images, opts.media)) f.write(get_footer()) - f.write(get_wget_lines(files)) + f.write(get_download_lines(files, recursive = opts.recursive)) f.close() return @@ -276,13 +280,15 @@ def get_filelink(path, fname, images=False, media=False): ) -def get_wget_lines(files): - wget = "\n\n" - return wget + s += "#FILE %s\n" % (urllib.parse.quote(f),) + s += "#DL-CMD:\n#URL=[insert URL] && wget -qO - $URL | grep '^#FILE ' | cut -c7- | sed \"s,^,$URL,\" | xargs -n1 wget -Nc" + if recursive: + s += "x" + s += "\n-->\n" + return s def get_imagestr(fname): @@ -431,109 +437,9 @@ iframe { def get_header(opts): - header = ( - """ - - - - - - """ - + setup2HTML(opts) - + """ - """ - + opts.title - + """ - + js_code = ''' + ''' + css_style = ''' + +''' + header = ( + """ + + + + + + + + + {title} +{css_style} +{js_code}
-

""" - + opts.title - + """

+

{title}

""" + ).format( + title = opts.title, + config = opts_str, + program = "SimpleWebPage", + version = VERSION, + js_code = js_code, + css_style = css_style ) return header
NameSizeSize BModified