modify datetime
This commit is contained in:
parent
eb6b8db0ea
commit
1b225d08fd
|
@ -3,10 +3,11 @@ import toml, argparse, os, sys, hashlib, json, pytz, glob, os, time
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
from dateutil.rrule import rrulestr
|
from dateutil.rrule import rrulestr
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
from datetime import time, timedelta, datetime
|
import time
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from watchdog.events import FileSystemEventHandler
|
from watchdog.events import FileSystemEventHandler
|
||||||
import xmpp_alert, email_alert
|
import email_alert
|
||||||
|
#import xmpp_alert
|
||||||
|
|
||||||
# Parse args
|
# Parse args
|
||||||
parser = argparse.ArgumentParser(description="A simple calendar alerting daemon written in Python")
|
parser = argparse.ArgumentParser(description="A simple calendar alerting daemon written in Python")
|
||||||
|
@ -38,7 +39,7 @@ files = glob.glob(os.path.join(cal_dir, '*.ics'))
|
||||||
|
|
||||||
class DateTimeEncoder(json.JSONEncoder):
|
class DateTimeEncoder(json.JSONEncoder):
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
if isinstance(o, (datetime, timedelta)):
|
if isinstance(o, (dt.datetime, dt.timedelta)):
|
||||||
return str(o) # Convert the datetime or timedelta object to a string
|
return str(o) # Convert the datetime or timedelta object to a string
|
||||||
return super().default(o)
|
return super().default(o)
|
||||||
|
|
||||||
|
@ -46,7 +47,7 @@ class FileChangeHandler(FileSystemEventHandler):
|
||||||
def on_modified(self, event):
|
def on_modified(self, event):
|
||||||
print(f"File modified: {event.src_path}")
|
print(f"File modified: {event.src_path}")
|
||||||
if not event.is_directory: # If it's a file and not a directory
|
if not event.is_directory: # If it's a file and not a directory
|
||||||
print(str(datetime.now()), "Sync detected, updating events")
|
print(str(dt.datetime.now()), "Sync detected, updating events")
|
||||||
with open(event.src_path, 'r') as f:
|
with open(event.src_path, 'r') as f:
|
||||||
cal_str = f.read()
|
cal_str = f.read()
|
||||||
|
|
||||||
|
@ -132,7 +133,7 @@ def calculate_recur_dates(dtstart, vrecur):
|
||||||
delta = relativedelta(years=interval)
|
delta = relativedelta(years=interval)
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
current_date = datetime.now().replace(tzinfo=pytz.UTC)
|
current_date = dt.datetime.now().replace(tzinfo=pytz.UTC)
|
||||||
origin_date = start_date
|
origin_date = start_date
|
||||||
while origin_date < current_date:
|
while origin_date < current_date:
|
||||||
count += interval
|
count += interval
|
||||||
|
@ -154,7 +155,7 @@ def calendar_parser(cal_str):
|
||||||
if component.name == "VEVENT": # If it's a VEVENT, create a new event dictionary
|
if component.name == "VEVENT": # If it's a VEVENT, create a new event dictionary
|
||||||
uid = component.get("UID")
|
uid = component.get("UID")
|
||||||
dtstart = component.get("DTSTART").dt
|
dtstart = component.get("DTSTART").dt
|
||||||
dtstart = dtstart if isinstance(dtstart, dt.datetime) else datetime.combine(dtstart, dt.time.min) # Ensure dates are always as datetime
|
dtstart = dtstart if isinstance(dtstart, dt.datetime) else dt.datetime.combine(dtstart, dt.time.min) # Ensure dates are always as datetime
|
||||||
dtstart = dtstart.replace(tzinfo=pytz.UTC)
|
dtstart = dtstart.replace(tzinfo=pytz.UTC)
|
||||||
summary = component.get("SUMMARY")
|
summary = component.get("SUMMARY")
|
||||||
vrecur = component.get("RRULE")
|
vrecur = component.get("RRULE")
|
||||||
|
@ -192,7 +193,7 @@ def get_next_alert(event, current_time):
|
||||||
next_alert = next_event
|
next_alert = next_event
|
||||||
else:
|
else:
|
||||||
next_alert = min(next_alert_list)
|
next_alert = min(next_alert_list)
|
||||||
return next_alert - timedelta(seconds=5), next_event
|
return next_alert - dt.timedelta(seconds=5), next_event
|
||||||
|
|
||||||
# Create initial event_list using calendar_parser
|
# Create initial event_list using calendar_parser
|
||||||
event_list = [] # List to hold dictionaries for each event
|
event_list = [] # List to hold dictionaries for each event
|
||||||
|
@ -221,7 +222,7 @@ try:
|
||||||
# Write the output to the file
|
# Write the output to the file
|
||||||
f.write(monitor_status)
|
f.write(monitor_status)
|
||||||
f.write("\n")
|
f.write("\n")
|
||||||
if current_time >= next_alert and next_alert < next_alert + timedelta(seconds=15):
|
if current_time >= next_alert and next_alert < next_alert + dt.timedelta(seconds=15):
|
||||||
if len(event["alert_history"]) == 0:
|
if len(event["alert_history"]) == 0:
|
||||||
print("First alert for '{}' detected".format(event["summary"]))
|
print("First alert for '{}' detected".format(event["summary"]))
|
||||||
event["alert_history"] = [{"timestamp_alert_triggered": current_time, "alert_defintition_time": next_alert}]
|
event["alert_history"] = [{"timestamp_alert_triggered": current_time, "alert_defintition_time": next_alert}]
|
||||||
|
@ -230,7 +231,7 @@ try:
|
||||||
else:
|
else:
|
||||||
print("Posting alert for {}!".format(event["summary"]))
|
print("Posting alert for {}!".format(event["summary"]))
|
||||||
event["alert_history"].append({"timestamp_alert_triggered": current_time, "alert_defintition_time": next_alert})
|
event["alert_history"].append({"timestamp_alert_triggered": current_time, "alert_defintition_time": next_alert})
|
||||||
xmpp_alert.send_xmpp(event["summary"], next_alert, next_event, args.config)
|
#xmpp_alert.send_xmpp(event["summary"], next_alert, next_event, args.config)
|
||||||
email_alert.send_email(event["summary"], next_alert, next_event, args.config)
|
email_alert.send_email(event["summary"], next_alert, next_event, args.config)
|
||||||
with open("alert_history", 'a') as f:
|
with open("alert_history", 'a') as f:
|
||||||
f.write(event)
|
f.write(event)
|
||||||
|
|
Loading…
Reference in New Issue