summaryrefslogtreecommitdiff
path: root/scripts/icstocal
blob: 94e870045e099f015000096c050bb318af75f50f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/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")
                year = ""
                month = ""
                day = ""
                hour = ""
                min = ""
                for i in range(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")
                hour = ""
                min = ""
                for i in range(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!")