1
0
personal-data/personal_data_calendar/__main__.py
2024-08-25 20:38:16 +02:00

66 lines
1.8 KiB
Python

import argparse
import logging
import icalendar
import urllib.parse
import datetime
import csv
from personal_data.util import load_csv_file
NOW = datetime.datetime.now(tz = datetime.UTC)
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('data_folder')
parser.add_argument('output_file')
return parser.parse_args()
def generate_calendar(rows: list[dict]) -> icalendar.Calendar:
cal = icalendar.Calendar()
cal.add('prodid', '-//personal_data_calendar//example.org//')
cal.add('version', '2.0')
for event_data in rows:
print(event_data)
# Select data
possible_time_keys = [k for k,v in event_data.items() if isinstance(v, datetime.date)]
possible_name_keys = [k for k,v in event_data.items() if isinstance(v, str)]
possible_image_keys = [k for k,v in event_data.items() if isinstance(v, urllib.parse.ParseResult)]
date = event_data[possible_time_keys[0]] if possible_time_keys else None
title = event_data[possible_name_keys[0]]
image = event_data[possible_image_keys[0]] if possible_image_keys else None
if date is None:
continue
# Create event
event = icalendar.Event()
event.add('summary', title)
event.add('dtstart', date)
event.add('dtend', date)
event.add('created', NOW)
event.add('dtstamp', NOW)
if image:
event.add('image', image.geturl())
cal.add_component(event)
del event
return cal
def main():
args = parse_arguments()
dicts = load_csv_file(args.data_folder + '/games_played_playstation.csv')
print(dicts)
calendar = generate_calendar(dicts)
with open(args.output_file, 'wb') as f:
f.write(calendar.to_ical())
if __name__ == '__main__':
main()