vatsched.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import imgkit, requests
  2. from dateutil.parser import parse
  3. from datetime import datetime, timedelta
  4. abbreviations = {}
  5. def EveryXDaysFromStartdate(startdate, x, numberofdates = 1):
  6. startdate = parse(startdate)
  7. days = []
  8. while (len(days) < numberofdates):
  9. startdate += timedelta(days=x)
  10. if(datetime.today() <= startdate):
  11. days.append(startdate)
  12. return days
  13. def EveryXWeeksFromStartdate(startdate, x, numberofdates = 1):
  14. return EveryXDaysFromStartdate(startdate, x * 7, numberofdates)
  15. def EveryWeekday(weekday, numberofdates=1):
  16. startdate = datetime.today() + timedelta((weekday - datetime.today().weekday()) % 7)
  17. days = []
  18. while(len(days) < numberofdates):
  19. days.append(startdate)
  20. startdate += timedelta(7)
  21. return days
  22. def WholeWeek(shift = 0):
  23. days=[]
  24. startdate = datetime.today() + timedelta(shift)
  25. for i in range(0,7):
  26. days.append(startdate + timedelta(i))
  27. return days
  28. def getDatePositionData(station, date, bookings):
  29. global abbreviations
  30. text = []
  31. for booking in bookings[station]:
  32. if date.date() == booking['starts_at'].date():
  33. abbr = booking['firstname'][:2] + booking['lastname'].split(" ")[-1][:2]
  34. abbreviations[abbr] = booking['firstname'] + " " + booking['lastname']
  35. text.append(abbr + " " + booking['starts_at'].strftime("%H") + "-" + booking['ends_at'].strftime("%H"))
  36. if len(text) < 1:
  37. text.append("--")
  38. return "<br>".join(text)
  39. def maketable(stations, displaydates, bookings):
  40. global abbreviations
  41. text = "<table><tr><th>Stationen</th>"
  42. for date in displaydates:
  43. text += "<th class="">%s</th>" % (date.strftime("%a %d.%m."))
  44. text += "</tr>"
  45. text += "<tr><td colspan='%d'></td></tr>" % (len(displaydates) + 1)
  46. for station in stations:
  47. if station == "--":
  48. text += "<tr><td colspan='%d' style='border-bottom: 0'></td></tr>" % (len(displaydates) + 1)
  49. text += "<tr><td colspan='%d' style='border-top: 0'></td></tr>" % (len(displaydates) + 1)
  50. continue
  51. datestext = ""
  52. for date in displaydates:
  53. datestext += "<td>%s</td>" % (getDatePositionData(station, date, bookings))
  54. text += "<tr><td style='text-align: left'>%s</td>%s</tr>" % (station, datestext)
  55. text += "</table> <div class='abbreviations'>"
  56. i = 0
  57. for key in sorted(abbreviations.keys()):
  58. text += "<div class='abbrv'><span>%s:</span> %s</div>" % (key, abbreviations[key])
  59. i += 1
  60. if i > 4:
  61. text += "</div><div class='abbreviations'>"
  62. i = 0
  63. return text + "</div>"
  64. def createImage(displaydates, stations, filename):
  65. rawdata = fetchData(displaydates[0], displaydates[-1])
  66. relbookings = {}
  67. for station in stations:
  68. relbookings[station] = []
  69. for datum in rawdata:
  70. if datum['station']['ident'] in stations:
  71. 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']})
  72. text = maketable(stations, displaydates, relbookings)
  73. css = 'layout.css'
  74. options = {'width': (100 + 75 * len(displaydates)), 'disable-smart-width': ''}
  75. # config = imgkit.config(wkhtmltoimage='C:/Program Files/wkhtmltopdf/bin/wkhtmltoimage.exe')
  76. config = imgkit.config(wkhtmltoimage='/usr/bin/wkhtmltoimage')
  77. imgkit.from_string(text, filename, css=css, options=options, config=config)
  78. return
  79. def fetchData(startdate = datetime.today(), enddate = (datetime.today() + timedelta(1))):
  80. url = 'https://vatsim-germany.org/api/booking/atc/daterange/%s/%s' % (startdate.strftime("%d.%m.%Y"), enddate.strftime("%d.%m.%Y"))
  81. r = requests.get(url=url,headers={'X-Requested-With': 'XMLHttpRequest'})
  82. return r.json()