From a9140666764923fb6c5280de915821714de04e9f Mon Sep 17 00:00:00 2001 From: rra Date: Wed, 1 Sep 2021 19:55:58 +0200 Subject: [PATCH] images now parsed and downloaded --- event_feed.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/event_feed.py b/event_feed.py index 0be6e6f..d662b5a 100644 --- a/event_feed.py +++ b/event_feed.py @@ -67,13 +67,10 @@ def create_metadata(event): 'end': event.end.format(), 'duration': date.compress(event.duration), 'location': event.location, - 'uid': event.uid + 'uid': event.uid, + 'images' : find_imageURLS(event.description) # currently not used in template } - #TODO: 1 find image urls in description - # 2 download them to hugo post - # 3 replace image url with local path - return event_metadata def localize_time(date): @@ -109,11 +106,24 @@ def localize_time(date): def create_event_post(post_dir, event): - event_metadata = create_metadata(event) - if not os.path.exists(post_dir): os.mkdir(post_dir) + event_metadata = create_metadata(event) + + for img in event_metadata['images']: + img_name = img.split('/')[-1] + local_image = os.path.join(post_dir, img_name) + #TODO: handle the fact that someone might update a post + #and delete / replace an image + if not os.path.exists(local_image): + #download preview image + response = requests.get(img, stream=True) + with open(local_image, 'wb') as img_file: + shutil.copyfileobj(response.raw, img_file) + print('Downloaded event image for event "{}"'.format(event.name)) + event_metadata['description'] = event_metadata['description'].replace(img, '![]({})'.format(img_name)) + with open(os.path.join(post_dir,'index.md'),'w') as f: post = template.render(event = event_metadata) f.write(post)