From 4754ad83ee95dcae6b34b9ea20b6308fadaa9aa0 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 15 Mar 2022 16:17:34 +0100 Subject: working with description and deadline now --- filters.py | 21 +++++++++++++++++++++ main.py | 26 +++++++++++++++++--------- taggen.py | 15 --------------- 3 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 filters.py delete mode 100644 taggen.py diff --git a/filters.py b/filters.py new file mode 100644 index 0000000..bad50e9 --- /dev/null +++ b/filters.py @@ -0,0 +1,21 @@ +import re + +def tag_gen(input_str): + input_str = input_str.lower() + input_str = re.sub(r'aii', '', input_str) + input_str = re.sub(r'20\d{2}-\d{2}', '', input_str) + input_str = re.sub(r'[pb]\d+', '', input_str) + input_str = re.sub(r'\d', '', input_str) + input_str = re.sub(r'blok', '', input_str) + input_str = re.sub(r'programmeren c', 'programmeren', input_str) + input_str = re.sub(r'stylofoon', '', input_str) + input_str = re.sub(r'[-]', '', input_str) + input_str = re.sub(r'\s+', ' ', input_str) + return input_str.strip() + +def title_filter(input_str): + input_str = input_str.lower() + input_str = re.sub(r'- due', '', input_str) + input_str = re.sub(r'- available', '', input_str) + input_str = re.sub(r'- availability ends', '', input_str) + return input_str.strip() diff --git a/main.py b/main.py index 5b0a68b..cd4daea 100755 --- a/main.py +++ b/main.py @@ -2,10 +2,11 @@ from libdecsync import Decsync from icalendar import Calendar, Todo -from taggen import tag_gen from datetime import datetime from uuid import uuid4 +import filters import time +import re import json import os import requests @@ -23,6 +24,10 @@ def get_brightspace_events(config): cal = Calendar.from_ical(request.text) return list(cal.walk('vevent')) +def description_parser(desc): + return (re.sub(r'\n\nView event - (.+)', r'[view event](\1)', desc), + re.sub(r'View event - (.+)', r'\1', desc)) + def create_task_ical(event): cal = Calendar() todo = Todo() @@ -32,17 +37,18 @@ def create_task_ical(event): todo.add("priority", 9) todo.add("status", "needs-action") - tag = tag_gen(event.get('location')) + tag = filters.tag_gen(event.get('location')) if tag: todo.add("categories", [tag]) todo.add("created", datetime.now()) + todo.add("due", event.decoded('dtstart')) + todo.add("summary", filters.title_filter(str(event.decoded('summary'), 'utf-8'))) - todo.add("dtstamp", event.decoded('dtstart')) - todo.add("uid", uuid4()) - - todo.add("summary", event.decoded('summary')) - description = event.get('description') - if description: todo.add("description", description) + desctext = event.get('description') + if not desctext: return None + description, uid = description_parser(desctext) + todo.add("description", description) + todo.add("uid", description) cal.add_component(todo) return cal.to_ical() @@ -50,7 +56,9 @@ def create_task_ical(event): def task_handler(config, ds, event): epoch = time.time() timestamp = f"{int(epoch)}{int((epoch % 1) * 10 ** 9)}" - ds.set_entry(["resources", timestamp], None, str(create_task_ical(event), 'utf-8')) + ical = create_task_ical(event) + if not ical: return + ds.set_entry(["resources", timestamp], str(event.decoded('uid'), 'utf-8'), str(ical, 'utf-8')) def main(): config = load_config() diff --git a/taggen.py b/taggen.py deleted file mode 100644 index 3d36e87..0000000 --- a/taggen.py +++ /dev/null @@ -1,15 +0,0 @@ -import re - -def tag_gen(input_str): - input_str = input_str.lower() - input_str = re.sub(r'aii', '', input_str) - input_str = re.sub(r'20\d{2}-\d{2}', '', input_str) - input_str = re.sub(r'[pb]\d+', '', input_str) - input_str = re.sub(r'\d', '', input_str) - input_str = re.sub(r'blok', '', input_str) - input_str = re.sub(r'programmeren c', 'programmeren', input_str) - input_str = re.sub(r'stylofoon', '', input_str) - input_str = re.sub(r'[-]', '', input_str) - input_str = re.sub(r'\s+', ' ', input_str) - return input_str.strip() - -- cgit v1.2.3