vatsched.py 3.6 KB

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