Commit 82e73c44ff15bc7e45995c31c877e45b8c3af704
1 parent
99b1d135
Interface en ligne de commande
Showing
1 changed file
with
18 additions
and
10 deletions
Show diff stats
parse.py
1 | #!/usr/bin/env python3 | 1 | #!/usr/bin/env python3 |
2 | 2 | ||
3 | +import sys | ||
4 | +import argparse | ||
3 | import datetime | 5 | import datetime |
6 | +import urllib.request | ||
4 | from icalendar import Calendar | 7 | from icalendar import Calendar |
5 | from Edt import * | 8 | from Edt import * |
6 | 9 | ||
7 | -htmlHandle = open('groupe12.html', 'rb') | ||
8 | -htmlStr = htmlHandle.read().decode('iso-8859-15') | 10 | +# Parse command line arguments |
11 | +parser = argparse.ArgumentParser(description='Convertit l\'emploi du temps IMA3 en ICS') | ||
12 | +parser.add_argument('edt', metavar='EDT', type=str, help='la page pointant vers l\'emploi du temps concerné') | ||
13 | +parser.add_argument('-o', '--output', dest='file', type=str, default='-', help='fichier de sortie, - pour stdout') | ||
14 | +args = parser.parse_args() | ||
15 | + | ||
16 | +with urllib.request.urlopen('http://dptima3.polytech-lille.net/' + args.edt + '.html') as handle: | ||
17 | + htmlStr = handle.read().decode('iso-8859-15') | ||
9 | 18 | ||
10 | # Read HTML tables | 19 | # Read HTML tables |
11 | parser = TableHTMLParser() | 20 | parser = TableHTMLParser() |
@@ -45,10 +54,6 @@ for parserTable in parser.tables: | @@ -45,10 +54,6 @@ for parserTable in parser.tables: | ||
45 | x = 0 | 54 | x = 0 |
46 | y += 1 | 55 | y += 1 |
47 | 56 | ||
48 | - #for line in table: | ||
49 | - # print(' | '.join([str(cell) for cell in line])) | ||
50 | - #print('-'*20) | ||
51 | - | ||
52 | tables.append(table) | 57 | tables.append(table) |
53 | 58 | ||
54 | # Creating events | 59 | # Creating events |
@@ -103,11 +108,14 @@ cal.add('version', '2.0') | @@ -103,11 +108,14 @@ cal.add('version', '2.0') | ||
103 | 108 | ||
104 | for event in events: | 109 | for event in events: |
105 | if event.active: | 110 | if event.active: |
106 | - print(event) | 111 | + print(event, file=sys.stderr) |
107 | cal.add_component(event.getEvent()) | 112 | cal.add_component(event.getEvent()) |
108 | 113 | ||
109 | # Writing calendar to file | 114 | # Writing calendar to file |
110 | -f = open('groupe12.ics', 'wb') | ||
111 | -f.write(cal.to_ical()) | ||
112 | -f.close() | 115 | +data = cal.to_ical() |
116 | +if args.file == '-': | ||
117 | + sys.stdout.write(data.decode('utf-8')) | ||
118 | +else: | ||
119 | + with open(args.file, 'wb') as f: | ||
120 | + f.write(data) | ||
113 | 121 |