Skip to content

Commit

Permalink
Merge pull request #3 from sheilbronn/master
Browse files Browse the repository at this point in the history
Add validFrom and validTo tags to generated output
  • Loading branch information
Rosi2143 authored Aug 26, 2024
2 parents a829792 + 8519992 commit 83dad84
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 22 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Pylint

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
pip install icalendar
- name: Analysing the code with pylint
run: |
pylint --disable=line-too-long $(git ls-files '*.py')
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# EphemerisCalendarImport
import the school holiday calendar of [schulferien.org](https://www.schulferien.org/deutschland/ferien/)
Make the the school holiday calendar of [schulferien.org](https://www.schulferien.org/deutschland/ferien/)
available as a [JollyDay](https://github.com/svendiedrichsen/jollyday) XML file, e.g. for use in [OpenHab Ephemeris](https://www.openhab.org/docs/configuration/actions.html#ephemeris).

The script imports the data from the school calendar files for your state into a xml-file readable by the Ephemeris action.
## get the calendars
Calendars for German school holidays are centrally provided by [schulferien.org](https://www.schulferien.org/deutschland/ferien/).

Just download the calendar for your state **[from here](https://www.schulferien.org/deutschland/ical/)** and store it. You can download as many calendars as you want.
Just download the calendar for your state **[from here](https://www.schulferien.org/deutschland/ical/)** and store it.
You can download as many calendars as you want.

## advantages of this fork
This fork has the following changes:
* ported to Python 3
* added the actual *year* to the generated calendar entry (this was missing, causing errors and overlapping entries when used with multiple years)

## Preconditions

### python 2.7.x
install [python 2.7](https://www.python.org/downloads/)
### python 3
install [python 3](https://www.python.org/downloads/)

### install icalendar
The script depends on the [icalendar package](https://pypi.org/project/icalendar/)
Expand Down
47 changes: 29 additions & 18 deletions ephemeris.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
# see https://pypi.org/project/icalendar/
# get calendars for Germany from https://www.schulferien.org/deutschland/ical/

from icalendar import Calendar, Event
from datetime import date, timedelta, datetime

import calendar
import datetime
# import datetime
import glob
import argparse
import os
from icalendar import Calendar, Event

OpenHabConf = "/etc/openhab/"
try:
Expand All @@ -19,11 +18,11 @@

count = 0

parser = argparse.ArgumentParser(description='convert school holidays ICS files from https://www.schulferien.org/deutschland/ical/')
parser = argparse.ArgumentParser(description='Convert school holidays ICS files, e.g. from https://www.schulferien.org/deutschland/ical/')

parser.add_argument('-v', '--verbose', action='store_const', const=1, default=0 , help='activate logging')
parser.add_argument('-i', '--inPath', default=os.path.join(OpenHabConf, "scripts/") , help='set path where the ics files are')
parser.add_argument('-o', '--outFile', default=os.path.join(OpenHabConf, "services/holidays.xml"), help='set the out file')
parser.add_argument('-v', '--verbose', default=0, action='store_const', const=1 , help='activate logging')
parser.add_argument('-i', '--inPath' , default=os.path.join(OpenHabConf,"scripts/") , help='set path where the ics files are')
parser.add_argument('-o', '--outFile', default=os.path.join(OpenHabConf,"services/holidays.xml"), help='set the out file')

args = parser.parse_args()

Expand All @@ -40,7 +39,7 @@
9 :'SEPTEMBER',
10:'OCTOBER' ,
11:'NOVEMBER' ,
12:'DECEMBER'};
12:'DECEMBER'}

xmlFileContent = """\
<?xml version="1.0" encoding="UTF-8"?>
Expand All @@ -51,18 +50,25 @@
"""

def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
"""
Generate a range of dates between the start_date and end_date. Args:
start_date (datetime.date): The start date of the range.
end_date (datetime.date): The end date of the range.
Yields:
datetime.date: The dates in the range.
"""
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)

for filename in glob.glob(os.path.join(args.inPath, FileNameFilter)):
if args.verbose > 0:
print ("parsing file {0}".format(filename))
print (f"parsing file {filename}")
file = open(filename, 'rb')
cal = Calendar.from_ical(file.read())

if args.verbose > 1:
print (cal)
xmlFileContent += "\t<!-- filename = {file} -->\n".format(file=filename)
xmlFileContent += f"\t<!-- filename = {filename} -->\n"

for component in cal.walk():
if component.name == "VEVENT":
Expand All @@ -76,30 +82,35 @@ def daterange(start_date, end_date):
reoccur = component.get('rrule').to_ical().decode('utf-8')
for item in parse_recurrences(reoccur, startdt, exdate):
if args.verbose > 0:
print("\t{0} {1}: {2} - {3}".format(item, summary, description, location))
print(f"\t{item} {summary}: {description} - {location}")
else:
if args.verbose > 0:
print("\t{0}-{1} {2}: {3} - {4}".format(startdt.strftime("%D %H:%M UTC"), enddt.strftime("%D %H:%M UTC"), summary, description, location))
xmlFileContent += "\t\t<!-- reason = {_summary} -->\n".format(_summary=summary)
print( f"\t{0}-{1} {2}: {3} - {4}".format(startdt.strftime("%D %H:%M UTC"), enddt.strftime("%D %H:%M UTC"), summary, description, location))
xmlFileContent += f"\t\t<!-- reason = {summary} -->\n"
for single_date in daterange(startdt, enddt):
if args.verbose > 0:
print (single_date.strftime("%Y-%m-%d"))
if args.verbose > 1:
print (int(single_date.strftime("%m")))
month = MONTHS[int(single_date.strftime("%m"))]
print (month)
addString = """\t\t<tns:Fixed month=\"{_month}\" """.format(_month=month)
addString = f"""\t\t<tns:Fixed month=\"{month}\" """
print (addString)
addString = """\t\t<tns:Fixed month=\"{_month}\" day=\"{_day}\" descriptionPropertiesKey=\"{_summary}\" />\n""".format(_month=MONTHS[int(single_date.strftime("%m"))], _day=single_date.strftime("%d"),_summary=summary)
addString = """\t\t<tns:Fixed month=\"{_month}\" day=\"{_day}\" descriptionPropertiesKey=\"{_summary}\" validFrom=\"{_from}\" validTo=\"{_to}\" />\n""".format(
_month=MONTHS[int(single_date.strftime("%m"))],
_day=single_date.strftime("%d"),
_summary=summary,
_from=single_date.strftime("%Y"),
_to=single_date.strftime("%Y"))
if args.verbose > 0:
print (addString)
xmlFileContent += addString
count = count + 1

xmlFileContent += "\t</tns:Holidays>"
xmlFileContent += "</tns:Configuration>";
xmlFileContent += "</tns:Configuration>"

filehandle = open(args.outFile, mode='w')
filehandle = open(args.outFile, mode='w', encoding='utf-8')
filehandle.write(xmlFileContent)
filehandle.close

Expand Down
19 changes: 19 additions & 0 deletions holiday_sample.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
PRODID:-//example.org//Test file//DE
BEGIN:VEVENT
DTSTAMP:20770313T032001Z
SUMMARY:Test Herbstferien 2088 Bayern
DTSTART;VALUE=DATE:20881119
DTEND;VALUE=DATE:20881120
TRANSP:TRANSPARENT
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20770313T032001Z
SUMMARY:Test Weihnachtsferien 2088 Bayern
DTSTART;VALUE=DATE:20881222
DTEND;VALUE=DATE:20890106
TRANSP:TRANSPARENT
END:VEVENT
END:VCALENDAR

0 comments on commit 83dad84

Please sign in to comment.