config now stored as json. dl commands only use wget, less deps

This commit is contained in:
Ville Rantanen
2021-08-15 11:22:27 +03:00
parent 09914119c5
commit b35b362a15

View File

@@ -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 '<meta name="SimpleWebPageSetup" content="%s"/>' % ";".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('<meta name="SimpleWebPageSetup"') > -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"
def get_download_lines(files, recursive = False):
s = "\n<!--\n"
for f in files:
wget += "#FILE %s\n" % (urllib.parse.quote(f),)
wget += "#WGET URL=[insert URL] && curl $URL | grep '^#FILE ' | cut -c7- | sed \"s,^,$URL,\" | xargs -n1 wget\n"
wget += "-->\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 = (
"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, height=device-height, viewport-fit=cover">
<meta name="generator" content="SimpleWebPage """
+ VERSION
+ """" />
"""
+ setup2HTML(opts)
+ """
<title>"""
+ opts.title
+ """</title>
<style>
/* Style modified from: https://css-tricks.com/snippets/php/display-styled-directory-contents/ */
* {
padding: 0;
margin: 0;
}
body {
color: #222;
font: 14px monospace;
padding: 0;
background: #CCC;
}
h1 {
padding: 20px 0 12px 0;
margin: 0 0 0 70px;
font-family: sans-serif;
}
.container {
margin: 20px;
box-shadow: 0 5px 10px -5px rgba(0,0,0,0.5);
position: relative;
background: #eee;
border-top-left-radius: 70px;
display: inline-block;
min-width: calc(100% - 40px);
}
table {
background-color: #F3F3F3;
border-collapse: collapse;
width: 100%;
margin: 15px 0;
}
th {
background-color: #EB6000;
color: #FFF;
cursor: pointer;
padding: 5px 10px;
}
td {
padding-right: 5px;
}
a {
text-decoration: none;
}
opts_str = setup2JSON(opts)
td a {
color: #EB6000;
display: block;
padding: 5px 10px;
}
td a.link_dir {
font-weight: bold;
}
th a {
padding-left: 0
}
td:first-of-type a {
padding-left: 5px;
}
th:first-of-type {
padding-left: 5px;
}
tr:nth-of-type(odd) {
background-color: #e1e1e1;
}
tr.row_dir:hover td {
background-color: #fbe6b3;
}
tr.row_file:hover td {
background-color: #f9cba2;
}
tr:hover td a {
color: #222;
}
.right {
text-align: right;
}
.bytes {
font-size: xx-small;
}
.audio {
width: 90%;
}
.video {
width: 320;
height: 240;
}
</style>
js_code = '''
<script type="text/javascript">
/*
Table sorting script by Joost de Valk, check it out at http://www.joostdevalk.nl/code/sortable-table/.
@@ -857,14 +763,123 @@ function alternate(table) {
}
</script>
'''
css_style = '''
<style>
/* Style modified from: https://css-tricks.com/snippets/php/display-styled-directory-contents/ */
* {
padding: 0;
margin: 0;
}
body {
color: #222;
font: 14px monospace;
padding: 0;
background: #CCC;
}
h1 {
padding: 20px 0 12px 0;
margin: 0 0 0 70px;
font-family: sans-serif;
}
.container {
margin: 20px;
box-shadow: 0 5px 10px -5px rgba(0,0,0,0.5);
position: relative;
background: #eee;
border-top-left-radius: 70px;
display: inline-block;
min-width: calc(100% - 40px);
}
table {
background-color: #F3F3F3;
border-collapse: collapse;
width: 100%;
margin: 15px 0;
}
th {
background-color: #EB6000;
color: #FFF;
cursor: pointer;
padding: 5px 10px;
}
td {
padding-right: 5px;
}
a {
text-decoration: none;
}
td a {
color: #EB6000;
display: block;
padding: 5px 10px;
}
td a.link_dir {
font-weight: bold;
}
th a {
padding-left: 0
}
td:first-of-type a {
padding-left: 5px;
}
th:first-of-type {
padding-left: 5px;
}
tr:nth-of-type(odd) {
background-color: #e1e1e1;
}
tr.row_dir:hover td {
background-color: #fbe6b3;
}
tr.row_file:hover td {
background-color: #f9cba2;
}
tr:hover td a {
color: #222;
}
.right {
text-align: right;
}
.bytes {
font-size: xx-small;
}
.audio {
width: 90%;
}
.video {
width: 320;
height: 240;
}
</style>
'''
header = (
"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, height=device-height, viewport-fit=cover">
<meta name="generator" content="{program}"/>
<meta name="SimpleWebPageVersion" content="{version}"/>
<meta name="SimpleWebPageSetup" content='{config}'/>
<title>{title}</title>
{css_style}
{js_code}
</head>
<body>
<div class="container">
<h1>"""
+ opts.title
+ """</h1>
<h1>{title}</h1>
<table class="sortable" id="fileList"><thead><tr><th>Name</th><th class="right">Size</th><th class="right">Size B</th><th class="right">Modified</th></tr></thead><tbody>
"""
).format(
title = opts.title,
config = opts_str,
program = "SimpleWebPage",
version = VERSION,
js_code = js_code,
css_style = css_style
)
return header