Commit 04be21ba9623880440785cb54c6a98c2e2f1d539
Merge branch 'davical'
Showing
3 changed files
with
53 additions
and
23 deletions
Show diff stats
... | ... | @@ -0,0 +1,25 @@ |
1 | +#/bin/env bash | |
2 | + | |
3 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
4 | + | |
5 | +source $DIR/config.sh | |
6 | + | |
7 | +COOKIES=$(mktemp) | |
8 | +FILE=$(mktemp) | |
9 | + | |
10 | +$DIR/parse.py $ANNEE $EDT -o $FILE | |
11 | + | |
12 | +curl --data "username=$USERNAME&password=$PASSWORD" \ | |
13 | + --cookie-jar $COOKIES \ | |
14 | + $BASELINK/ > /dev/null | |
15 | + | |
16 | +curl -v \ | |
17 | + -F "collection_id=$CID" \ | |
18 | + -F "ics_file=@$FILE;type=text/calendar" \ | |
19 | + -F "mode=off" \ | |
20 | + -F "_editor_action[editor_1]=update" \ | |
21 | + -F "submit=true" \ | |
22 | + --cookie $COOKIES \ | |
23 | + "$BASELINK/admin.php?action=edit&t=collection&id=$CID" > /dev/null | |
24 | + | |
25 | +rm $COOKIES $FILE | ... | ... |
parse.py
... | ... | @@ -5,6 +5,7 @@ import sys |
5 | 5 | import argparse |
6 | 6 | import datetime |
7 | 7 | import urllib.request |
8 | +import dateutil.parser | |
8 | 9 | from icalendar import Calendar |
9 | 10 | from html.parser import HTMLParser |
10 | 11 | from icalendar import vDatetime, Calendar, Event as CalEvent |
... | ... | @@ -37,7 +38,7 @@ elif args.annee == 4: |
37 | 38 | else: |
38 | 39 | raise ValueError('Année inconnue : ' + annee) |
39 | 40 | |
40 | -DAYS_PER_WEEK = 6 | |
41 | +DAYS_PER_WEEK = 5.5 | |
41 | 42 | |
42 | 43 | TABLE_1_DATE_X = 1 |
43 | 44 | TABLE_1_FIRST_SLOT_X = 2 |
... | ... | @@ -120,9 +121,6 @@ class TableHTMLParser(HTMLParser): |
120 | 121 | if self.iscell(): |
121 | 122 | self.cell += data |
122 | 123 | |
123 | -# TODO Use HTTP header date | |
124 | -UPDATE_TIME = datetime.datetime.now() | |
125 | - | |
126 | 124 | # TODO Do something that really is OOP or do not... |
127 | 125 | |
128 | 126 | class Event: |
... | ... | @@ -160,7 +158,7 @@ class Event: |
160 | 158 | self.shortName = self.shortText |
161 | 159 | self.longName = self.longText |
162 | 160 | |
163 | - if self.longName: | |
161 | + if self.shortName: | |
164 | 162 | self.active = True |
165 | 163 | |
166 | 164 | if self.date and isinstance(self.begSlot, int) and isinstance(self.endSlot, int): |
... | ... | @@ -198,11 +196,12 @@ class Event: |
198 | 196 | e.add('dtstart', self.startTime) |
199 | 197 | e.add('dtend', self.endTime) |
200 | 198 | e.add('location', self.location) |
201 | - e.add('last-modified', UPDATE_TIME) | |
202 | - e.add('dtstamp', UPDATE_TIME) | |
199 | + e.add('last-modified', updateTime) | |
200 | + e.add('dtstamp', updateTime) | |
203 | 201 | return e |
204 | 202 | |
205 | 203 | with urllib.request.urlopen(url) as handle: |
204 | + updateTime = dateutil.parser.parse(handle.headers['Last-Modified']) | |
206 | 205 | htmlStr = handle.read().decode('iso-8859-15') |
207 | 206 | |
208 | 207 | # Read HTML tables |
... | ... | @@ -251,25 +250,24 @@ days = dict() |
251 | 250 | # Parsing table 1 |
252 | 251 | for line in tables[0]: |
253 | 252 | try: |
254 | - day1date = datetime.datetime.strptime(line[TABLE_1_DATE_X], DATE_FORMAT) | |
253 | + date = datetime.datetime.strptime(line[TABLE_1_DATE_X], DATE_FORMAT) | |
255 | 254 | except (ValueError, TypeError): |
256 | 255 | # This is not a date, no data to grab here |
257 | 256 | continue |
258 | 257 | |
259 | - for day in range(DAYS_PER_WEEK): | |
260 | - date = day1date + datetime.timedelta(days=day) | |
258 | + for weekSlot in range(int(DAYS_PER_WEEK * len(SLOTS))): | |
259 | + daySlot = weekSlot % len(SLOTS) | |
260 | + | |
261 | + # New day | |
262 | + if daySlot == 0: | |
263 | + if weekSlot != 0: | |
264 | + date = date + datetime.timedelta(days=1) | |
261 | 265 | |
262 | - if date not in days: | |
263 | - days[date] = [Event() for s in range(len(SLOTS))] | |
266 | + if date not in days: | |
267 | + days[date] = [Event() for s in range(len(SLOTS))] | |
264 | 268 | |
265 | - for slot in range(len(SLOTS)): | |
266 | - try: | |
267 | - cell = line[day * len(SLOTS) + slot + TABLE_1_FIRST_SLOT_X] | |
268 | - except IndexError: | |
269 | - # Out of the table: saturday afternoon | |
270 | - break | |
271 | - days[date][slot].feedShortText(cell) | |
272 | - continue | |
269 | + cell = line[TABLE_1_FIRST_SLOT_X + weekSlot] | |
270 | + days[date][daySlot].feedShortText(cell) | |
273 | 271 | |
274 | 272 | # Parsing table 2 |
275 | 273 | for line in tables[1]: |
... | ... | @@ -291,13 +289,15 @@ for day in days: |
291 | 289 | prevEvent = False |
292 | 290 | for slot in range(len(SLOTS)): |
293 | 291 | event = days[day][slot] |
292 | + sameAsPrevious = False | |
294 | 293 | if prevEvent: |
295 | - if prevEvent.longText == event.longText: | |
294 | + sameAsPrevious = event.longText == prevEvent.longText if prevEvent.longText else event.shortText == prevEvent.shortText | |
295 | + if sameAsPrevious: | |
296 | 296 | prevEvent.feedEndSlot(slot) |
297 | 297 | else: |
298 | 298 | prevEvent.endFeed() |
299 | 299 | events.append(prevEvent) |
300 | - if not prevEvent or (prevEvent and prevEvent.longText != event.longText): | |
300 | + if not prevEvent or (prevEvent and not sameAsPrevious): | |
301 | 301 | event.feedDate(day) |
302 | 302 | event.feedBegSlot(slot) |
303 | 303 | event.feedEndSlot(slot) |
... | ... | @@ -320,7 +320,6 @@ if os.path.isfile('custom.py'): |
320 | 320 | |
321 | 321 | for event in events: |
322 | 322 | if event.active: |
323 | - print(event, file=sys.stderr) | |
324 | 323 | cal.add_component(event.getEvent()) |
325 | 324 | |
326 | 325 | # Writing calendar to file | ... | ... |