example.yaml

This commit is contained in:
notplants 2024-01-26 22:07:24 -06:00
parent e97cb115dc
commit 162e91e662
3 changed files with 36 additions and 13 deletions

10
example.yaml Normal file
View File

@ -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"

35
main.py
View File

@ -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)

4
run.sh
View File

@ -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"