vatsched.py 4.9 KB

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