summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Franke <nobody@nowhere.ws>2016-04-07 10:28:29 -0300
committerChristian Franke <nobody@nowhere.ws>2016-04-07 10:28:29 -0300
commit16c06102404d7c0ca35167f47312343e513582fe (patch)
tree99557bbcedfc28e303df6faab9c25c49cb29ed41
parent5d8c422adbda5f4bfb0c69dbe8a8df9a35122388 (diff)
Fix calendar script so that website is rebuilt when day changes
-rw-r--r--.gitignore2
-rw-r--r--scripts/get_calendar.py24
2 files changed, 21 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 7759601..22f369b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,10 @@
*.pyc
/scripts/sublab-doorstatus.last
/public/*.html
+/public/calendar.ics
/public/css/*.status.css
/public/api
*~
/template/calendar.json
/template/tabcalendar.json
+/scripts/get_calendar.today
diff --git a/scripts/get_calendar.py b/scripts/get_calendar.py
index 8db09c9..89daf07 100644
--- a/scripts/get_calendar.py
+++ b/scripts/get_calendar.py
@@ -1,4 +1,4 @@
-from datetime import datetime, timedelta, time
+from datetime import datetime, timedelta, time, date
from dateutil.relativedelta import relativedelta
import urllib2
@@ -34,7 +34,21 @@ def fetch_calendar(logger):
export_file.write(content)
os.rename(CALENDARIUM_EXPORT + '.new', CALENDARIUM_EXPORT)
- return icalendar.Calendar.from_ical(content)
+ try:
+ with open(TODAY_EXPORT, 'r') as today_file:
+ cached_today = today_file.read()
+ except Exception:
+ cached_today = None
+
+ real_today = str(date.today()) + '\n'
+ if real_today != cached_today:
+ with open(TODAY_EXPORT, 'w') as today_file:
+ today_file.write(real_today)
+ regen = True
+ else:
+ regen = False
+
+ return (regen,icalendar.Calendar.from_ical(content))
def get_events(calendar, after, before):
"""Yields all events in calendar as dictionary.
@@ -112,11 +126,11 @@ base_path = os.path.dirname(os.path.abspath(__file__))
#CALENDARIUM_IMPORT_URL = 'https://sublab.org:5232/calendars/events'
CALENDARIUM_IMPORT_URL = 'https://posteo.de/calendars/ics/ruku1tibpa2b2evfwsxrbvwcy2n60j9g'
CALENDARIUM_EXPORT = os.path.join(base_path, '../public/calendar.ics')
-
+TODAY_EXPORT = os.path.join(base_path, 'get_calendar.today')
if __name__ == '__main__':
logging.basicConfig(stream=sys.stderr, level=logging.ERROR)
logger = logging.getLogger('calendar_feed')
- calendar = fetch_calendar(logger)
+ regen, calendar = fetch_calendar(logger)
now = datetime.now()
after = now - relativedelta(days=1)
before = now + relativedelta(months=+1)
@@ -132,5 +146,5 @@ if __name__ == '__main__':
tab_changed = put_events(calendar, after_tab, before_tab,
os.path.join(base_path, '../template/tabcalendar.json'))
- if changed or tab_changed:
+ if changed or tab_changed or regen:
subprocess.call(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'template.py'))