vatsched.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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() <= startdate):
  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> <div class='abbreviations'>"
  67. i = 0
  68. for key in sorted(abbreviations.keys()):
  69. text += "<div class='abbrv'><span>%s:</span> %s</div>" % (key, abbreviations[key])
  70. i += 1
  71. if i > 4:
  72. text += "</div><div class='abbreviations'>"
  73. i = 0
  74. text += "</div>"
  75. text += "<div style='clear:both'>"
  76. if(training == 1):
  77. text += "<span class='training'>Training </span>"
  78. if(event == 1):
  79. text += "<span class='event'>Event</span>"
  80. text += "</div>"
  81. return text
  82. def createImage(displaydates, stations, filename):
  83. global abbreviations, training, event
  84. training = event = 0
  85. abbreviations = {}
  86. rawdata = fetchData(displaydates[0], displaydates[-1])
  87. relbookings = {}
  88. for station in stations:
  89. relbookings[station] = []
  90. for datum in rawdata:
  91. if datum['station']['ident'] in stations:
  92. relbookings[datum['station']['ident']].append({
  93. 'starts_at': parse(datum['starts_at']),
  94. 'ends_at': parse(datum['ends_at']),
  95. 'lastname': datum['controller']['lastname'],
  96. 'firstname': datum['controller']['firstname'],
  97. 'training': datum['training'],
  98. 'event': datum['event']
  99. })
  100. text = maketable(stations, displaydates, relbookings)
  101. directory, dontcare = os.path.split(__file__)
  102. css = directory + os.sep + 'layout.css'
  103. options = {'width': (100 + 75 * len(displaydates)), 'disable-smart-width': ''}
  104. # config = imgkit.config(wkhtmltoimage='C:/Program Files/wkhtmltopdf/bin/wkhtmltoimage.exe')
  105. config = imgkit.config(wkhtmltoimage='/usr/bin/wkhtmltoimage')
  106. imgkit.from_string(text, filename, css=css, options=options, config=config)
  107. return
  108. def fetchData(startdate = datetime.today(), enddate = (datetime.today() + timedelta(1))):
  109. url = 'https://vatsim-germany.org/api/booking/atc/daterange/%s/%s' % (startdate.strftime("%d.%m.%Y"), enddate.strftime("%d.%m.%Y"))
  110. r = requests.get(url=url,headers={'X-Requested-With': 'XMLHttpRequest'})
  111. return r.json()