From 22111e3608a5e13277a1cde1c2e3f4d6b2f1cf86 Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 19 Jun 2020 01:32:13 +0200 Subject: [PATCH] [cron] ICS-calendars to Mattermost channel(s). --- ics_to_channel/.gitignore | 1 + ics_to_channel/config.py.example | 16 ++++++++ ics_to_channel/main.py | 64 ++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 ics_to_channel/.gitignore create mode 100644 ics_to_channel/config.py.example create mode 100755 ics_to_channel/main.py diff --git a/ics_to_channel/.gitignore b/ics_to_channel/.gitignore new file mode 100644 index 0000000..4acd06b --- /dev/null +++ b/ics_to_channel/.gitignore @@ -0,0 +1 @@ +config.py diff --git a/ics_to_channel/config.py.example b/ics_to_channel/config.py.example new file mode 100644 index 0000000..04fd0f7 --- /dev/null +++ b/ics_to_channel/config.py.example @@ -0,0 +1,16 @@ +# +# Someone's Mattermost maintenance scripts. +# Copyright (c) 2016-2020 by Someone (aka. Jan Vales ) +# published under MIT-License +# +# Config file. +# mv to config.py.examle config.py + edit. +# + +mm_api_url = "..." +mm_user = "..." +mm_user_pw = "..." + +ics_config = { + "alias": ["ics-url",["channel1-id", "channel2-id", ]], + } diff --git a/ics_to_channel/main.py b/ics_to_channel/main.py new file mode 100755 index 0000000..a93a01c --- /dev/null +++ b/ics_to_channel/main.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# Someone's Mattermost scripts. +# Copyright (c) 2016-2020 by Someone (aka. Jan Vales ) +# published under MIT-License +# +# Send event-reminders to configured channels. +# + +import datetime +import pytz +import pprint +import time +import traceback + +import icalendar +import requests +import mattermost + +import config + +def process_ics(mm, now, alias, ics_config): + c = icalendar.Calendar.from_ical(requests.get(ics_config[0]).text) + for ev in c.subcomponents: + if isinstance(ev,icalendar.cal.Event): + spam_mm = False + for remind in ev.subcomponents: + if isinstance(ev.get("DTSTART").dt,datetime.datetime): + event_datetime = ev.get("DTSTART").dt + alarm_datetime = ev.get("DTSTART").dt+remind.get("TRIGGER").dt + + # convert to now's tz + event_datetime = (event_datetime-datetime.datetime.utcoffset(event_datetime)+datetime.datetime.utcoffset(now)).replace(second=0, tzinfo=now.tzinfo) + alarm_datetime = (alarm_datetime-datetime.datetime.utcoffset(alarm_datetime)+datetime.datetime.utcoffset(now)).replace(second=0, tzinfo=now.tzinfo) + + else: + # why the diffrent handling of "full-day" events?! + event_datetime = datetime.datetime.combine(ev.get("DTSTART").dt, datetime.datetime.min.time()).replace(second=0, tzinfo=now.tzinfo) + alarm_datetime = (datetime.datetime.combine(ev.get("DTSTART").dt, datetime.datetime.min.time())+remind.get("TRIGGER").dt).replace(second=0, tzinfo=now.tzinfo) + + seconds_to_reminder_time = (now-alarm_datetime).total_seconds() + + if seconds_to_reminder_time >= 0 and seconds_to_reminder_time < 300: + spam_mm = True + + if spam_mm: + for channel_id in ics_config[1]: + pprint.pprint(mm.create_post(channel_id, "``BOT-AUTODELETE-FAST`` #termin_reminder Datasource: "+alias+"\nUpcoming event at: **``"+str(event_datetime)+"``**\n### ``"+str(ev.get("SUMMARY"))+"``\n"+str(ev.get("DESCRIPTION")))) + + + +mm = mattermost.MMApi(config.mm_api_url) +mm.login(config.mm_user, config.mm_user_pw) + +TS_START = time.time() +now = datetime.datetime.now(tz=pytz.timezone("Europe/Vienna")).replace(second=0, microsecond=0) + +for alias, ics_config in config.ics_config.items(): + try: + process_ics(mm, now, alias, ics_config) + except: + traceback.print_exc() + print("* ["+("%07.6g"%round(time.time() - TS_START, 5))+"] COMPLETED: "+alias) + +mm.logout() -- 2.43.0