Skip to content

Commit

Permalink
Merge pull request #141 from PLanB2008/master
Browse files Browse the repository at this point in the history
Created simple script to generate holidays from ferien-api.de
  • Loading branch information
HappyBasher committed Jul 30, 2022
2 parents 64c9701 + 8c89351 commit 6490d87
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions sbin/linuxmuster-holiday-generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3

from datetime import datetime, date
import requests
from requests.structures import CaseInsensitiveDict
import argparse
import yaml
import sys

"""
Simple script to generate yaml file for holidays based on ferien-api.de
The user has to take care of the output, maybe pipe it into /etc/linuxmuster/sophomorix/SCHOOL/holidays.yml
Structure of the configuration file (YAML):
holiday_name1:
start: "dd.mm.yyyy"
end: "dd.mm.yyyy"
holiday_name2:
start: "dd.mm.yyyy"
end: "dd.mm.yyyy"
"""

def get_holidays(year,state) -> list:
url = "https://ferien-api.de/api/v1/holidays/" + state
headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/json"
resp = requests.get(url, headers=headers)
content = resp.json()
holidays = []

for entry in content:
if entry['year'] == int(year) or entry['year'] == int(year)+1:
start = datetime.strptime(entry['start'], '%Y-%m-%dT%H:%MZ')
end = datetime.strptime(entry['end'], '%Y-%m-%dT%H:%MZ')
holidays.append({entry['name']:{'start': start.strftime('%d.%m.%Y'), 'end': end.strftime('%d.%m.%Y')}})
return holidays

def print_holiday_yaml(holidays):
for holiday in holidays:
yaml.dump(holiday, sys.stdout, default_flow_style=False)


def main():
possible_states = ["BW", "BY", "BE", "BB", "HB", "HH", "HE", "MV", "NI", "NW", "RP", "SL", "SN", "ST", "SH", "TH"]
parser = argparse.ArgumentParser()
parser.add_argument("-y", "--year", required = False, help = "Define which year too look for")
parser.add_argument("-s", "--state", required = True, help = "Define state, possible values are: " + ','.join(possible_states))

args = parser.parse_args()

args.state = args.state.upper()
if args.state not in possible_states:
print ("Provided state is not supported.\nSupported states are: "+ ','.join(possible_states))
quit(1)

if not args.year:
args.year = date.today().year

holidays = get_holidays(args.year,args.state)
print_holiday_yaml(holidays)

if __name__ == "__main__":
main()

0 comments on commit 6490d87

Please sign in to comment.