diff --git a/example.yaml b/example.yaml new file mode 100644 index 0000000..c543d80 --- /dev/null +++ b/example.yaml @@ -0,0 +1,10 @@ +- title: "Nature In The City: Bengaluru in the Past, Present and Future" + author: "Harini Nagendra" + published_year: "2016" + cover_url: "https://bookwyrm-social.sfo3.digitaloceanspaces.com/images/covers/nature-in-the-city.jpeg" + cover_image_path: "Nature-In-The-City.jpg" +- name: "The Future Is Disabled: Prophecies, Love Notes and Mourning Songs" + author: + published_year: "Leah Lakshmi Piepzna-Samarasinha" + cover_url: "https://bookwyrm-social.sfo3.digitaloceanspaces.com/images/covers/3db732ea-49d6-4960-93c9-31f46db82f3d.jpeg" + cover_image_path: "The-Future-Is-Disabled.jpg" \ No newline at end of file diff --git a/main.py b/main.py index 10e0438..0e49d39 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import requests import argparse import os import jinja2 +import yaml # LIST_URL = "https://bookwyrm.social/list/2458/s/notplants-2023" @@ -20,10 +21,10 @@ if not os.path.exists(STATIC_DIR): os.makedirs(STATIC_DIR) -def build_site(output_dir, relative_template_path, template_vars): +def build_site(output_dir, images_dir, relative_template_path, template_vars): # copy over static files - input_static_dir = os.path.join(STATIC_DIR) + input_static_dir = images_dir output_static_dir = os.path.join(output_dir, 'static') if os.path.exists(output_static_dir): os.system('rm -r {}'.format(output_static_dir)) @@ -130,10 +131,20 @@ def download_images(processed_items, output_dir): os.system('wget {} -O "{}"'.format(image_url, image_path)) -def load_csv(csv_path): - return { - "books": [] - } +def write_yaml(items, output_dir): + yaml_path = os.path.join(output_dir, "books.yaml") + output = yaml.dump(items, Dumper=yaml.Dumper) + with open(yaml_path, 'w') as f: + f.write(output) + + +def load_yaml(yaml_path): + with open(yaml_path) as f: + contents = f.read() + books = yaml.load(contents, Loader=yaml.Loader) + return { + "books": books + } if __name__ == '__main__': @@ -154,7 +165,8 @@ if __name__ == '__main__': 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('images_dir') # path to folder containing cover images + build_parser.add_argument('yaml_path') # yaml 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 @@ -171,12 +183,13 @@ if __name__ == '__main__': processed_items = process_list(args.list_url) download_images(processed_items, output_dir=args.output_dir) - # TODO: write processed items to csv + write_yaml(processed_items, output_dir=args.output_dir) 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) + yaml_path = args.yaml_path + images_dir = args.images_dir + template_vars = load_yaml(yaml_path) + build_site(output_dir=OUTPUT_DIR, images_dir=images_dir, relative_template_path=args.template, template_vars=template_vars) diff --git a/run.sh b/run.sh index ccc9b24..b1ee6ee 100755 --- a/run.sh +++ b/run.sh @@ -1,5 +1,5 @@ # download -python3 main.py bookwyrm-download "https://bookwyrm.social/list/2458/s/notplants-2023" "books-2023" +#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" + python3 main.py generate-html "example.yaml" --template="bookshelf.html"