123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import imgkit, requests, os
- from dateutil.parser import parse
- from datetime import datetime, timedelta
- abbreviations = {}
- training = 0
- event = 0
- def EveryXDaysFromStartdate(startdate, x, numberofdates = 1):
- startdate = parse(startdate)
- days = []
- while (len(days) < numberofdates):
- startdate += timedelta(days=x)
- if(datetime.today().date() <= startdate.date()):
- days.append(startdate)
- return days
- def EveryXWeeksFromStartdate(startdate, x, numberofdates = 1):
- return EveryXDaysFromStartdate(startdate, x * 7, numberofdates)
- def EveryWeekday(weekday, numberofdates=1):
- startdate = datetime.today() + timedelta((weekday - datetime.today().weekday()) % 7)
- days = []
- while(len(days) < numberofdates):
- days.append(startdate)
- startdate += timedelta(7)
- return days
- def WholeWeek(shift = 0):
- days=[]
- startdate = datetime.today() + timedelta(shift)
- for i in range(0,7):
- days.append(startdate + timedelta(i))
- return days
- def getDatePositionData(station, date, bookings):
- global abbreviations, training, event
- text = []
- for booking in bookings[station]:
- color1 = color2 = ""
- if date.date() == booking['starts_at'].date():
- abbr = booking['firstname'][:2] + booking['lastname'].split(" ")[-1][:2]
- abbreviations[abbr] = booking['firstname'] + " " + booking['lastname']
- if(booking['event'] == 1):
- color1 = "<span class='event'>"
- color2 = "</span>"
- event = 1
- if(booking['training'] == 1):
- color1 = "<span class='training'>"
- color2 = "</span>"
- training = 1
- text.append(color1 + abbr + " " + booking['starts_at'].strftime("%H") + "-" + booking['ends_at'].strftime("%H") + color2)
- if len(text) < 1:
- text.append("--")
- return "<br>".join(text)
- def maketable(stations, displaydates, bookings):
- global abbreviations, training, event
- text = "<table><tr><th>Stationen</th>"
- for date in displaydates:
- text += "<th class="">%s</th>" % (date.strftime("%a %d.%m."))
- text += "</tr>"
- text += "<tr><td colspan='%d'></td></tr>" % (len(displaydates) + 1)
- for station in stations:
- if station == "--":
- text += "<tr><td colspan='%d' style='border-bottom: 0'></td></tr>" % (len(displaydates) + 1)
- text += "<tr><td colspan='%d' style='border-top: 0'></td></tr>" % (len(displaydates) + 1)
- continue
- datestext = ""
- for date in displaydates:
- datestext += "<td>%s</td>" % (getDatePositionData(station, date, bookings))
- text += "<tr><td style='text-align: left'>%s</td>%s</tr>" % (station, datestext)
- text += "</table>"
-
- text += "<div>"
- if(training == 1):
- text += "<span class='training'>Training </span>"
- if(event == 1):
- text += "<span class='event'>Event</span>"
- text += "</div>"
-
- text += "<div class='abbreviations'>"
- i = 0
- for key in sorted(abbreviations.keys()):
- text += "<div class='abbrv'><span>%s:</span> %s</div>" % (key, abbreviations[key])
- i += 1
- if i > 4:
- text += "</div><div class='abbreviations'>"
- i = 0
- text += "</div>"
- return text
- def createImage(displaydates, stations, filename):
- global abbreviations, training, event
- training = event = 0
- abbreviations = {}
- rawdata = fetchData(displaydates[0] - timedelta(1), displaydates[-1] + timedelta(1))
- relbookings = {}
- for station in stations:
- relbookings[station] = []
- for datum in rawdata:
- if datum['station']['ident'] in stations:
- relbookings[datum['station']['ident']].append({
- 'starts_at': parse(datum['starts_at']),
- 'ends_at': parse(datum['ends_at']),
- 'lastname': datum['controller']['lastname'],
- 'firstname': datum['controller']['firstname'],
- 'training': datum['training'],
- 'event': datum['event']
- })
- print(relbookings)
- text = maketable(stations, displaydates, relbookings)
- directory, dontcare = os.path.split(__file__)
- css = directory + os.sep + 'layout.css'
- options = {'width': (100 + 75 * len(displaydates)), 'disable-smart-width': ''}
- # config = imgkit.config(wkhtmltoimage='C:/Program Files/wkhtmltopdf/bin/wkhtmltoimage.exe')
- config = imgkit.config(wkhtmltoimage='/usr/bin/wkhtmltoimage')
- imgkit.from_string(text, filename, css=css, options=options, config=config)
- return
- def fetchData(startdate = datetime.today(), enddate = (datetime.today() + timedelta(1))):
- url = 'https://vatsim-germany.org/api/booking/atc/daterange/%s/%s' % (startdate.strftime("%d.%m.%Y"), enddate.strftime("%d.%m.%Y"))
- print(url)
- r = requests.get(url=url,headers={'X-Requested-With': 'XMLHttpRequest'})
- return r.json()
|