forked from guiguiabloc/api-domogeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassSchoolCalendar.py
136 lines (130 loc) · 4.31 KB
/
ClassSchoolCalendar.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Gruik coded by GuiguiAbloc
# http://blog.guiguiabloc.fr
# http://api.domogeek.fr
#
# NEED : iCalendar https://pypi.python.org/pypi/icalendar
#
from __future__ import unicode_literals
from urllib import urlopen
from icalendar import Calendar, Event
from datetime import datetime
from datetime import date
from icalendar import LocalTimezone
import sys, time
class schoolcalendar:
def getschoolcalendar(self, zone):
try:
zoneok = str(zone.upper())
except:
return "Wrong Zone (must be A, B or C)"
if len(zoneok) > 1:
return "Wrong Zone (must be A, B or C)"
if zoneok not in ["A","B","C"]:
return "Wrong Zone (must be A, B or C)"
else :
URL = "http://media.education.gouv.fr/ics/Calendrier_Scolaire_Zone_"+zoneok+".ics"
try:
ics = urlopen(URL)
cal = Calendar.from_ical(ics.read())
except:
return "Data offline"
datenow = datetime.now()
year = datenow.year
month = datenow.month
day = datenow.day
today = date(year,month,day)
listvalue = []
for event in cal.walk('vevent'):
start = event.get('dtstart')
convertstart = start.to_ical()
stringconvertstart= str(convertstart)
startdecode = time.strptime(str(convertstart), '%Y%m%d')
end = event.get('dtend')
summary = event.get('summary')
yearstart = startdecode[0]
monthstart = startdecode[1]
daystart = startdecode[2]
d2 = date(yearstart, monthstart, daystart)
description = summary.to_ical().decode('utf-8')
if end:
convertend = end.to_ical()
enddecode = time.strptime(str(convertend), '%Y%m%d')
stringconvertend= str(convertend)
yearend = enddecode[0]
monthend = enddecode[1]
dayend = enddecode[2]
listvalue.append([stringconvertstart, description,stringconvertend])
else:
listvalue.append([stringconvertstart, description])
return str(listvalue)
def isschoolcalendar(self,zone,day,month,year):
try:
zoneok = str(zone.upper())
except:
return "Wrong Zone (must be A, B or C)"
if len(zoneok) > 1:
return "Wrong Zone (must be A, B or C)"
if zoneok not in ["A","B","C"]:
return "Wrong Zone (must be A, B or C)"
else :
URL = "http://media.education.gouv.fr/ics/Calendrier_Scolaire_Zone_"+zoneok+".ics"
try:
ics = urlopen(URL)
cal = Calendar.from_ical(ics.read())
except:
return "Data offline"
#datenow = datetime.now()
#year = datenow.year
#month = datenow.month
#month = 9
#day = datenow.day
#day = 26
today = date(year,month,day)
startspring = u"Vacances d'\xe9t\xe9"
endspring = u"Rentr\xe9e scolaire des \xe9l\xe8ves"
springd2 = None
springd3 = None
for event in cal.walk('vevent'):
start = event.get('dtstart')
convertstart = start.to_ical()
stringconvertstart= str(convertstart)
startdecode = time.strptime(str(convertstart), '%Y%m%d')
end = event.get('dtend')
summary = event.get('summary')
yearstart = startdecode[0]
monthstart = startdecode[1]
daystart = startdecode[2]
d2 = date(yearstart, monthstart, daystart)
d3 = None
description = summary.to_ical().decode('utf-8')
if str(year) in convertstart:
if startspring in summary :
if d2 is not None :
springd2 = d2
if endspring in summary:
if d2 is not None:
springd3 = d2
comparestart = ""
compareend = ""
if springd2:
comparestart = springd2 < today
if springd3:
compareend = today < springd3
if comparestart and compareend:
description = startspring.encode('utf-8')
return str(description)
if end:
convertend = end.to_ical()
enddecode = time.strptime(str(convertend), '%Y%m%d')
stringconvertend= str(convertend)
yearend = enddecode[0]
monthend = enddecode[1]
dayend = enddecode[2]
d3 = date(yearend, monthend, dayend)
if d2 < today < d3:
return description
else:
pass