From 5d4edaaf2332d170243d34c56844b3e13fa3a896 Mon Sep 17 00:00:00 2001 From: Castro0o Date: Sun, 9 Feb 2020 12:52:33 +0100 Subject: [PATCH] cleaning static_html dir beefore creating new html --- README.md | 3 +++ functions.py | 10 +++++++++- query2html.py | 8 ++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a4c53d6..0c2430a 100644 --- a/README.md +++ b/README.md @@ -59,3 +59,6 @@ Run scripts together with `./run.sh` ## TODO * remove HTML files at each new query +* revise `def unpack_response()` so that it returns the values of all properties printed out +* revise template so that they include the values of all properties printed out \ +and do not break on missing values \ No newline at end of file diff --git a/functions.py b/functions.py index e8be7f4..c6742bf 100644 --- a/functions.py +++ b/functions.py @@ -51,7 +51,7 @@ def unpack_response(response): else: val = list(prop_val.values())[0] d[prop] = val - return(d) + return d def update_json(imgsjson_fn, img_dict, img_fn): @@ -86,6 +86,14 @@ def update_json(imgsjson_fn, img_dict, img_fn): return download +def clean_dir(dirfullpath): + for f in os.listdir(dirfullpath): + f = os.path.join(dirfullpath, f) + if os.path.isfile(f): + os.remove(f) + + + class Colors: HEADER = '\033[95m' BLUE = '\033[94m' diff --git a/query2html.py b/query2html.py index ed367bb..265c817 100644 --- a/query2html.py +++ b/query2html.py @@ -2,7 +2,7 @@ import os, json, sys, urllib from mwclient import Site from pprint import pprint from jinja2 import Template -from functions import pandoc, page_props, unpack_response +from functions import pandoc, page_props, unpack_response, clean_dir from functions import Colors import argparse @@ -48,13 +48,14 @@ if args.dry is True: site = Site(host=args.host, path=args.path) -wd = os.path.dirname(os.path.abspath(__file__)) # working directory +wd = os.path.dirname(os.path.abspath(__file__)) # working directory imgdir = os.path.join(wd, 'images') imgsjson_fn = os.path.join(wd, 'images.json') with open(imgsjson_fn, 'r') as imgsjson_file: images_info = json.load(imgsjson_file) static_html = os.path.join(wd, 'static_html') -os.makedirs(static_html, exist_ok=True) # create images/ dir +os.makedirs(static_html, exist_ok=True) # create static_html/ dir +clean_dir(static_html) # if static_html exists and has files or dirs: clean it with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd loginlines = login.read() @@ -104,7 +105,6 @@ for answer in site.ask(query): # print(pub_html, '\n') pub_parts_html = '' # Reset pub_parts_html -# TODO: queries without Part? # TODO: include Creator Property value # TODO: address Dates: