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.

86 lines
2.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  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 'RRULE:{}'.format(ret_str)
  44. def create_body(_class):
  45. if _class.time_range:
  46. body = {
  47. # "kind": "calendar#event",
  48. }
  49. body['recurrence'] = [rrule_former(_class)]
  50. body['start'] = dateTime(datetime.datetime.combine(_class.date_range[0],_class.time_range[0]))
  51. body['end'] = dateTime(datetime.datetime.combine(_class.date_range[0],_class.time_range[1]))
  52. body['summary'] = _class.title
  53. body['description'] = 'location: {}'.format(_class.location)
  54. body['location'] = LOCATION
  55. body['reminders'] = {'useDefault':True}
  56. return body
  57. def json_dump(obj):
  58. with open('classes.json','w') as file:
  59. json.dump(obj,file)
  60. def test_rrule():
  61. #test
  62. now = datetime.datetime.now()
  63. from munch import Munch
  64. test_obj = Munch(
  65. days=[1,3,5],
  66. time_range=[
  67. now.time(),
  68. (now+datetime.timedelta(seconds=50*60)).time()
  69. ],
  70. date_range=[
  71. now.date(),
  72. (now+datetime.timedelta(days=20)).date()
  73. ],
  74. )
  75. test_result = rrule_former(test_obj)
  76. return locals()
  77. def test_class2body():
  78. with open('classes.pkl','rb') as file:
  79. classes = pickle.load(file)
  80. test_result = list(filter(bool,map(create_body,classes)))
  81. return test_result
  82. if __name__ == "__main__":
  83. json_dump(test_class2body())