Add offline availability of w3css

This commit is contained in:
KnugiHK
2023-02-13 12:23:43 +08:00
parent a275a0f40c
commit 26320413e8
5 changed files with 54 additions and 11 deletions

View File

@@ -103,6 +103,11 @@ def main():
default=False,
action='store_true',
help="Move the media directory to output directory if the flag is set, otherwise copy it")
parser.add_option(
"--offline",
dest="offline",
default=None,
help="Relative path to offline static files")
(options, args) = parser.parse_args()
if options.android and options.iphone:
@@ -194,7 +199,7 @@ def main():
messages(db, data)
media(db, data, options.media)
vcard(db, data)
create_html(data, options.output, options.template, options.embedded)
create_html(data, options.output, options.template, options.embedded, options.offline)
else:
print(
"The message database does not exist. You may specify the path "

View File

@@ -431,7 +431,7 @@ def vcard(db, data):
print(f"Gathering vCards...({index + 1}/{total_row_number})", end="\r")
def create_html(data, output_folder, template=None, embedded=False):
def create_html(data, output_folder, template=None, embedded=False, offline_static=False):
if template is None:
template_dir = os.path.dirname(__file__)
template_file = "whatsapp.html"
@@ -450,6 +450,18 @@ def create_html(data, output_folder, template=None, embedded=False):
if not os.path.isdir(output_folder):
os.mkdir(output_folder)
w3css = "https://www.w3schools.com/w3css/4/w3.css"
if offline_static:
import urllib.request
static_folder = os.path.join(output_folder, offline_static)
if not os.path.isdir(static_folder):
os.mkdir(static_folder)
w3css_path = os.path.join(static_folder, "w3.css")
if not os.path.isfile(w3css_path):
with urllib.request.urlopen(w3css) as resp:
with open(w3css_path, "wb") as f: f.write(resp.read())
w3css = os.path.join(offline_static, "w3.css")
for current, contact in enumerate(data):
if len(data[contact]["messages"]) == 0:
continue
@@ -474,7 +486,8 @@ def create_html(data, output_folder, template=None, embedded=False):
name=name,
msgs=data[contact]["messages"].values(),
my_avatar=None,
their_avatar=f"WhatsApp/Avatars/{contact}.j"
their_avatar=f"WhatsApp/Avatars/{contact}.j",
w3css=w3css
)
)
if current % 10 == 0:

View File

@@ -228,7 +228,7 @@ def vcard(db, data):
print(f"Gathering vCards...({index + 1}/{total_row_number})", end="\r")
def create_html(data, output_folder, template=None, embedded=False):
def create_html(data, output_folder, template=None, embedded=False, offline_static=False):
if template is None:
template_dir = os.path.dirname(__file__)
template_file = "whatsapp.html"
@@ -247,6 +247,18 @@ def create_html(data, output_folder, template=None, embedded=False):
if not os.path.isdir(output_folder):
os.mkdir(output_folder)
w3css = "https://www.w3schools.com/w3css/4/w3.css"
if offline_static:
import urllib.request
static_folder = os.path.join(output_folder, offline_static)
if not os.path.isdir(static_folder):
os.mkdir(static_folder)
w3css_path = os.path.join(static_folder, "w3.css")
if not os.path.isfile(w3css_path):
with urllib.request.urlopen(w3css) as resp:
with open(w3css_path, "wb") as f: f.write(resp.read())
w3css = os.path.join(offline_static, "w3.css")
for current, contact in enumerate(data):
if len(data[contact]["messages"]) == 0:
continue
@@ -272,7 +284,8 @@ def create_html(data, output_folder, template=None, embedded=False):
name=name,
msgs=data[contact]["messages"].values(),
my_avatar=None,
their_avatar=f"WhatsApp/Avatars/{contact}.j"
their_avatar=f"WhatsApp/Avatars/{contact}.j",
w3css=w3css
)
)
if current % 10 == 0:

View File

@@ -458,7 +458,7 @@ def vcard(db, data):
print(f"Gathering vCards...({index + 1}/{total_row_number})", end="\r")
def create_html(data, output_folder, template=None, embedded=False):
def create_html(data, output_folder, template=None, embedded=False, offline_static=False):
if template is None:
template_dir = os.path.dirname(__file__)
template_file = "whatsapp.html"
@@ -477,6 +477,18 @@ def create_html(data, output_folder, template=None, embedded=False):
if not os.path.isdir(output_folder):
os.mkdir(output_folder)
w3css = "https://www.w3schools.com/w3css/4/w3.css"
if offline_static:
import urllib.request
static_folder = os.path.join(output_folder, offline_static)
if not os.path.isdir(static_folder):
os.mkdir(static_folder)
w3css_path = os.path.join(static_folder, "w3.css")
if not os.path.isfile(w3css_path):
with urllib.request.urlopen(w3css) as resp:
with open(w3css_path, "wb") as f: f.write(resp.read())
w3css = os.path.join(offline_static, "w3.css")
for current, contact in enumerate(data):
if len(data[contact].messages) == 0:
continue
@@ -501,7 +513,8 @@ def create_html(data, output_folder, template=None, embedded=False):
name=name,
msgs=data[contact].messages.values(),
my_avatar=None,
their_avatar=f"WhatsApp/Avatars/{contact}.j"
their_avatar=f"WhatsApp/Avatars/{contact}.j",
w3css=w3css
)
)
if current % 10 == 0:

View File

@@ -2,11 +2,10 @@
<html>
<head>
<title>Whatsapp - {{ name }}</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<meta charset="UTF-8">
<link rel="stylesheet" href="{{w3css}}">
<style>
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+HK:wght@300;400&display=swap');
html {
font-family: 'Noto Sans HK', sans-serif;
html, body {
font-size: 12px;
scroll-behavior: smooth;
}