snippets

More or less useful code snippets
Log | Files | Refs

commit 4c17bbbf643dbd6f9c70e795cc5964a5ff5c63f9
parent c56a3eb78fbbd1eabb2777e85ca3d3378eff54ca
Author: Vetle Haflan <vetle@haflan.dev>
Date:   Sun, 23 Aug 2020 17:41:33 +0200

Add date-events.py

Diffstat:
Adate-events.py | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 59 insertions(+), 0 deletions(-)

diff --git a/date-events.py b/date-events.py @@ -0,0 +1,59 @@ +#!/usr/bin/python3 + +import requests +import re +from datetime import datetime +from os.path import expanduser + +HOME=expanduser('~') + +# Quickfix to get date from midnight +date_today = datetime.now().replace(hour=0,minute=0,second=0,microsecond=0) + +def get_events(): + date_re = re.compile('\d{2}-\d{2}') + # TODO: Get string from qump? + event_strs = open(HOME + '/scripts/date-events.txt').read().splitlines() + event_dates = [] + for e in event_strs[1:]: + if not e: + continue + match = re.match(date_re, e) + if match: + dt = datetime.strptime(match.group(0), '%m-%d').replace(year = date_today.year) + text = e.split(' ', 1)[1] + event_dates.append({ 'date': dt, 'text': text }) + else: + print('Malformed event line: ' + e) + continue + return event_dates + +# Report with Gotify - this can of course be changed to whatever reporting service +def report(message): + gotify_token = open(HOME + '/.gotify-token').read().splitlines()[0] + headers = { 'X-Gotify-Key': gotify_token } + data = { 'title': 'Events', 'message': message } + res = requests.post('https://push.haflan.dev/message', headers=headers, data=data) + +events = get_events() +in_week = [] +today = [] +for e in events: + days_to = (e['date'] - date_today).days + if days_to == 0: + today.append(e) + if 0 < days_to < 8: + in_week.append(e) + +message = '' +if today: + message = 'TODAY:\n' +for e in today: + message += '- ' + e['text'] + '\n' + +if in_week: + message += 'WEEK:\n' +for e in in_week: + message += '- ' + str(e['date'].strftime('%A')) + ': ' + e['text'] + '\n' + +report(message)