Commit 82e73c44ff15bc7e45995c31c877e45b8c3af704

Authored by Geoffrey PREUD'HOMME
1 parent 99b1d135

Interface en ligne de commande

Showing 1 changed file with 18 additions and 10 deletions   Show diff stats
parse.py
1 1 #!/usr/bin/env python3
2 2  
  3 +import sys
  4 +import argparse
3 5 import datetime
  6 +import urllib.request
4 7 from icalendar import Calendar
5 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 19 # Read HTML tables
11 20 parser = TableHTMLParser()
... ... @@ -45,10 +54,6 @@ for parserTable in parser.tables:
45 54 x = 0
46 55 y += 1
47 56  
48   - #for line in table:
49   - # print(' | '.join([str(cell) for cell in line]))
50   - #print('-'*20)
51   -
52 57 tables.append(table)
53 58  
54 59 # Creating events
... ... @@ -103,11 +108,14 @@ cal.add('version', '2.0')
103 108  
104 109 for event in events:
105 110 if event.active:
106   - print(event)
  111 + print(event, file=sys.stderr)
107 112 cal.add_component(event.getEvent())
108 113  
109 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  
... ...