Browse Source

add deletion logic

pull/1/head
rra 3 years ago
parent
commit
431e8472c9
  1. 37
      event_feed.py

37
event_feed.py

@ -7,6 +7,7 @@ from ics import Calendar
import requests
import jinja2
import os
import shutil
from slugify import slugify
from natural import date
from event_feed_config import calendar_url, output_dir
@ -40,6 +41,7 @@ tzs = [
('Wellington', 'Pacific/Auckland')
]
for event in list(cal.events):
localized_begins =[]
@ -47,7 +49,7 @@ for event in list(cal.events):
localized_begins.append( #javascript formatting because of string creation from hell
'__{}__ {}'.format(
str(i[0]),
str(event.begin.to(i[1]).format("YYYY-MM-DD HH:mm"))
str(event.begin.to(i[1]).format("YYYY-MM-DD __HH:mm__"))
)
)
@ -56,19 +58,32 @@ for event in list(cal.events):
'name':event.name,
'created':event.created.format(),
'description': event.description,
'begin': '        '.join(localized_begins), #non-breaking space characters to defeat markdown
'begin': '           '.join(localized_begins), #non-breaking space characters to defeat markdown
'end': event.end.format(),
'duration': date.compress(event.duration),
'location': event.location
}
#FIXME: There is no deletion logic
#FIXME: figure out how to use legible yet unique post names
#FIXME: An update logic?
post_dir = os.path.join(output_dir, slugify(event.name)) + '-' + slugify(event.begin.format("YYYY-MM-DD"))
if not os.path.exists(post_dir):
os.mkdir(post_dir)
with open(os.path.join(post_dir,'index.md'),'w') as f:
post = template.render(event = event_metadata)
f.write(post)
if event.uid not in existing_posts: #if there is an event we dont already have, make it
post_dir = os.path.join(output_dir, event.uid )
if not os.path.exists(post_dir):
os.mkdir(post_dir)
with open(os.path.join(post_dir,'index.md'),'w') as f:
post = template.render(event = event_metadata)
f.write(post)
print('created post for', event.name, '({})'.format(event.uid))
elif event.uid in existing_posts: #if we already have it, update
existing_posts.remove(event.uid) # create list of posts which have not been returned by the calendar
#FIXME: An update logic? e.g. exists but creation date is altered.
print(event.uid, 'already exists')
for post in existing_posts: #remove events that have been deleted
print('deleted', post) #rm posts not returned
shutil.rmtree(os.path.join(output_dir,post))

Loading…
Cancel
Save