You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.7 KiB

  1. from dateutil import rrule
  2. from gcalendar import dateTime
  3. import datetime
  4. import pickle
  5. import json
  6. import re
  7. LOCATION = "5500 St Louis Ave, Chicago, IL 60625"
  8. event = {
  9. 'summary': 'Google I/O 2015',
  10. 'location': '800 Howard St., San Francisco, CA 94103',
  11. 'description': 'A chance to hear more about Google\'s developer products.',
  12. 'start': {
  13. 'dateTime': '2015-05-28T09:00:00-07:00',
  14. 'timeZone': 'America/Los_Angeles',
  15. },
  16. 'end': {
  17. 'dateTime': '2015-05-28T17:00:00-07:00',
  18. 'timeZone': 'America/Los_Angeles',
  19. },
  20. 'recurrence': [
  21. 'RRULE:FREQ=DAILY;COUNT=2'
  22. ],
  23. 'attendees': [
  24. {'email': 'lpage@example.com'},
  25. {'email': 'sbrin@example.com'},
  26. ],
  27. 'reminders': {
  28. 'useDefault': False,
  29. 'overrides': [
  30. {'method': 'email', 'minutes': 24 * 60},
  31. {'method': 'popup', 'minutes': 10},
  32. ],
  33. },
  34. }
  35. def rrule_former(class_obj):
  36. days = class_obj.days
  37. start =datetime.datetime.combine(class_obj.date_range[0],class_obj.time_range[0]).astimezone()
  38. end =datetime.datetime.combine(class_obj.date_range[1],class_obj.time_range[1]).astimezone()
  39. days = [ (day -1) % 7 for day in days]
  40. ret = rrule.rrule(freq=rrule.WEEKLY,dtstart=start,wkst=rrule.SU,until=end,byweekday=days)
  41. ret_str = str(ret).split('\n')[-1]
  42. ret_str=re.sub(r'(UNTIL=[^;]+)',r'\1Z',ret_str)
  43. return '{}'.format(ret_str)
  44. def create_body(class_obj,is_lab = False):
  45. if not is_lab:
  46. lab_body = None
  47. if class_obj.lab:
  48. lab_body = create_body(class_obj.lab,True)
  49. if class_obj.time_range:
  50. body = {
  51. # "kind": "calendar#event",
  52. }
  53. body['recurrence'] = [rrule_former(class_obj)]
  54. body['start'] = dateTime(datetime.datetime.combine(class_obj.date_range[0],class_obj.time_range[0]))
  55. body['end'] = dateTime(datetime.datetime.combine(class_obj.date_range[0],class_obj.time_range[1]))
  56. body['summary'] = class_obj.title
  57. body['description'] = 'location: {}'.format(class_obj.location)
  58. body['location'] = LOCATION
  59. body['reminders'] = {'useDefault':True}
  60. if is_lab:
  61. return body
  62. else:
  63. return body,lab_body
  64. def json_dump(obj):
  65. with open('classes.json','w') as file:
  66. json.dump(obj,file)
  67. def create_event(class_list):
  68. for class_obj in class_list:
  69. out = create_body(class_obj)
  70. if out:
  71. body,lab_body = out
  72. yield body
  73. if lab_body:
  74. yield lab_body
  75. if __name__ == "__main__":
  76. from scraper import get_classes
  77. with open('schedule.html') as file:
  78. classes = get_classes(file.read())
  79. l = list(create_event(classes))
  80. json_dump(l)