From e97cb115dc8e6d7578df6117b207433627a3b348 Mon Sep 17 00:00:00 2001 From: notplants Date: Fri, 26 Jan 2024 18:51:22 -0600 Subject: [PATCH] split script into two parts --- .gitignore | 1 + bookshelf.html | 2 +- main.py | 53 ++++++++++++++++++++++++++++++++++++++------------ run.sh | 6 +++++- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index c26f7aa..99bad6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea static dist +books-2023 diff --git a/bookshelf.html b/bookshelf.html index 6b71d43..a5ddc3d 100644 --- a/bookshelf.html +++ b/bookshelf.html @@ -23,7 +23,7 @@
- +
{{ book.title }} by {{ book.author }} {% if book.published_year %} ({{book.published_year}}) {% endif %} diff --git a/main.py b/main.py index 0ee17b4..10e0438 100644 --- a/main.py +++ b/main.py @@ -108,7 +108,7 @@ def process_list(list_url): print("cover: {}".format(cover_url)) image_title = book.get("title").replace(" ", "-") image_title = image_title.replace(":", "") - image_path = "static/{}.jpg".format(image_title) + image_path = "{}.jpg".format(image_title) # return item item = { "title": title, @@ -121,33 +121,62 @@ def process_list(list_url): return processed_items -def download_images(processed_items): +def download_images(processed_items, output_dir): for item in processed_items: image_url = item.get("cover_url") - image_path = item.get("cover_image_path") + image_path = os.path.join(output_dir, item.get("cover_image_path")) if image_url: print("++ downloading {}".format(image_url)) os.system('wget {} -O "{}"'.format(image_url, image_path)) +def load_csv(csv_path): + return { + "books": [] + } + + if __name__ == '__main__': + + # create parser parser = argparse.ArgumentParser( prog='bookshelf-generator', description='generates HTML for a web-page to display a bookshelf using bookwyrm or a csv as input', epilog='<3') - parser.add_argument('list_url') # bookwyrm list url to use as a source input - parser.add_argument('-t', '--template') # path to jinja file to use as a template for the html + # subparser for building website + subparsers = parser.add_subparsers(dest="subparser") + build_parser = subparsers.add_parser('generate-html') + bookwyrm_parser = subparsers.add_parser('bookwyrm-download') + # bookwyrm-download subparser + bookwyrm_parser.add_argument('list_url') # bookwyrm list url to use as a source input + bookwyrm_parser.add_argument('output_dir') # path where images should be downloaded and csv should be written + + # generate-html subparser + build_parser.add_argument('csv_path') # csv path to use as input + build_parser.add_argument('-t', '--template') # path to jinja file to use as a template for the html + + # parse args args = parser.parse_args() - print(args.list_url, args.template) - processed_items = process_list(args.list_url) - template_vars = { - "books": processed_items - } - # download_images(processed_items) - build_site(output_dir=OUTPUT_DIR, relative_template_path=args.template, template_vars=template_vars) + if args.subparser == "bookwyrm-download": + + # subargs = bookwyrm_parser.parse_args() + print(args.list_url) + print(args.output_dir) + + if not os.path.exists(args.output_dir): + os.makedirs(args.output_dir) + + processed_items = process_list(args.list_url) + download_images(processed_items, output_dir=args.output_dir) + # TODO: write processed items to csv + + else: + # TODO: load values from csv + template_vars = load_csv(args.csv_path) + build_site(output_dir=OUTPUT_DIR, relative_template_path=args.template, template_vars=template_vars) diff --git a/run.sh b/run.sh index a6c7ee6..ccc9b24 100755 --- a/run.sh +++ b/run.sh @@ -1 +1,5 @@ -python3 main.py "https://bookwyrm.social/list/2458/s/notplants-2023" --template="bookshelf.html" \ No newline at end of file +# download +python3 main.py bookwyrm-download "https://bookwyrm.social/list/2458/s/notplants-2023" "books-2023" + +# build +# python3 main.py generate-html "books-2023/books.csv" --template="bookshelf.html"