-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from PLanB2008/master
Created simple script to generate holidays from ferien-api.de
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |