split script into two parts
This commit is contained in:
parent
6a4aa3643e
commit
e97cb115dc
|
@ -1,3 +1,4 @@
|
|||
.idea
|
||||
static
|
||||
dist
|
||||
books-2023
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
<div class="book-item">
|
||||
<div>
|
||||
<img src="{{book.cover_image_path}}" class="cover-image"/>
|
||||
<img src="static/{{book.cover_image_path}}" class="cover-image"/>
|
||||
</div>
|
||||
<div>
|
||||
{{ book.title }} by {{ book.author }} {% if book.published_year %} ({{book.published_year}}) {% endif %}
|
||||
|
|
53
main.py
53
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)
|
||||
|
||||
|
||||
|
||||
|
|
6
run.sh
6
run.sh
|
@ -1 +1,5 @@
|
|||
python3 main.py "https://bookwyrm.social/list/2458/s/notplants-2023" --template="bookshelf.html"
|
||||
# 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"
|
||||
|
|
Loading…
Reference in New Issue