-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathupdate.py
More file actions
107 lines (82 loc) · 2.45 KB
/
update.py
File metadata and controls
107 lines (82 loc) · 2.45 KB
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
100
101
102
103
104
105
106
107
# /usr/bin/env python3
# encoding: utf-8
import sys
import os
import re
import datetime
import csv
import urllib.request
LISTFILE = "list.txt"
ICSDIR = "ics"
CSVDIR = "csv"
def icspath(id_):
return os.path.join(ICSDIR, id_)
def csvpath(id_):
return os.path.join(CSVDIR, id_)
# [date, summary]
def readics(id_):
rows = []
date = None
summary = None
for line in open(icspath(id_)):
line = line.rstrip("\r\n")
if line == "BEGIN:VEVENT":
pass
elif line.startswith("DTSTART;"):
m = re.match("^DTSTART;VALUE=DATE:(\d{4}\d{2}\d{2})$", line)
date = datetime.datetime.strptime(m.group(1), "%Y%m%d").date()
elif line.startswith("SUMMARY:"):
m = re.match("^SUMMARY:(.*)$", line)
summary = m.group(1)
elif line == "END:VEVENT":
rows.append((date, summary))
return rows
# [date, summary]
def readcsv(id_):
rows = []
for datestr, summary in csv.reader(open(csvpath(id_), newline="")):
date = datetime.datetime.strptime(datestr, "%Y-%m-%d").date()
rows.append((date, summary))
return rows
def writecsv(id_, rows):
o = open(csvpath(id_), "w")
writer = csv.writer(o)
for date, summary in rows:
writer.writerow([date.strftime("%Y-%m-%d"), summary])
o.close()
# [id, url, country, title]
def readlist():
return csv.reader(open(LISTFILE, newline=""))
def mergeicscsv(id_):
if not os.path.exists(csvpath(id_)):
rows = readics(id_)
else:
year = datetime.datetime.now().year
rows = []
for date, summary in readcsv(id_):
if year < date.year:
rows.append((date, summary))
for date, summary in readics(id_):
if year >= date.year:
rows.append((date, summary))
return sorted(rows, key=lambda x: x[0])
def download():
rows = list(readlist())
for i, [id_, url, country, title] in enumerate(rows, start=1):
print("({}/{}): {}".format(i, len(rows), url))
f = urllib.request.urlopen(url)
ics = f.read()
f.close()
if not ics.startswith(b"BEGIN:VCALENDAR"):
raise Exception("not ics")
o = open(icspath(id_), "wb")
o.write(ics)
o.close()
def convert():
for id_, url, country, title in readlist():
writecsv(id_, mergeicscsv(id_))
def main():
download()
convert()
if __name__ == "__main__":
sys.exit(main())