#!/usr/bin/env python3 # A really poorly written script to convert .ics file to calendar file for quand or when # doesn't work when an event take longer than a day from sys import argv from pathlib import Path if len(argv) < 3: print("To use this script you need to supply 2 arguments.") print("e.g.: icsparser calendar.ics $XDG_DATA_HOME/quand/calendar") exit() TIMEZONE = 2 # as UTC+2 IN="en" # 'en' means 'in' in french INPUT = argv[1] OUTPUT = argv[2] if not Path(INPUT).is_file(): print("{0} doesn't exist".format(INPUT)) exit() file = open(INPUT, "r") if file.readline() != "BEGIN:VCALENDAR\n": print("{0} is not valid".format(INPUT)) file.close() exit() line = file.readline() locations = [] summaries = [] starts = [] ends = [] while line != "END:VCALENDAR\n": if line == "BEGIN:VEVENT\n": while line != "END:VEVENT\n": if line.startswith("LOCATION"): locations.append(line[9:].strip()) elif line.startswith("SUMMARY"): summaries.append(line[8:].strip()) elif line.startswith("DTSTART"): start = line.lstrip("DTSTART") n = 0 while start[n] != ":": n+=1 year = "" month = "" day = "" hour = "" min = "" for i in range(n + 1, len(start)): if i >= 1 and i <= 4: year += start[i] elif i == 5 or i == 6: month += start[i] elif i == 7 or i == 8: day += start[i] elif i == 10: hour += start[i] elif i == 11: if int(start[i]) + TIMEZONE > 9: hour = str(int(hour[0]) + 1) + "0" # I cba do the case where H will become 25 else: hour += str(int(start[i]) + TIMEZONE) elif i == 12 or i == 13: min += start[i] starts.append([year, month, day, hour, min]) elif line.startswith("DTEND"): end = line.lstrip("DTEND") n = 0 while end[n] != ":": n+=1 hour = "" min = "" for i in range(n + 1, len(end)): if i == 10: hour += end[i] elif i == 11: if int(end[i]) + TIMEZONE > 9: hour = str(int(hour[0]) + 1) + "0" # I cba do the case where H will become 25 else: hour += str(int(end[i]) + TIMEZONE) elif i == 12 or i == 13: min += end[i] ends.append("{0}:{1}".format(hour, min)) line = file.readline() line = file.readline() file.close() file = open(OUTPUT, "w") for i in range(len(locations)): # yyyy mm dd, hh:hh -> hh:hh summary location file.write("{0} {1} {2}, {3}:{4} -> {5} {6} {7} {8}\n".format(starts[i][0], starts[i][1], starts[i][2], starts[i][3], starts[i][4], ends[i], summaries[i], IN, locations[i])) file.close() print("All done!")