diff --git a/body_create.py b/body_create.py index 77cffc8..b198a7b 100644 --- a/body_create.py +++ b/body_create.py @@ -3,34 +3,35 @@ from gcalendar import dateTime import datetime import pickle import json +import re LOCATION = "5500 St Louis Ave, Chicago, IL 60625" -# event = { - # 'summary': 'Google I/O 2015', - # 'location': '800 Howard St., San Francisco, CA 94103', - # 'description': 'A chance to hear more about Google\'s developer products.', - # 'start': { - # 'dateTime': '2015-05-28T09:00:00-07:00', - # 'timeZone': 'America/Los_Angeles', - # }, - # 'end': { - # 'dateTime': '2015-05-28T17:00:00-07:00', - # 'timeZone': 'America/Los_Angeles', - # }, - # 'recurrence': [ - # 'RRULE:FREQ=DAILY;COUNT=2' - # ], - # 'attendees': [ - # {'email': 'lpage@example.com'}, - # {'email': 'sbrin@example.com'}, - # ], - # 'reminders': { - # 'useDefault': False, - # 'overrides': [ - # {'method': 'email', 'minutes': 24 * 60}, - # {'method': 'popup', 'minutes': 10}, - # ], - # }, -# } +event = { + 'summary': 'Google I/O 2015', + 'location': '800 Howard St., San Francisco, CA 94103', + 'description': 'A chance to hear more about Google\'s developer products.', + 'start': { + 'dateTime': '2015-05-28T09:00:00-07:00', + 'timeZone': 'America/Los_Angeles', + }, + 'end': { + 'dateTime': '2015-05-28T17:00:00-07:00', + 'timeZone': 'America/Los_Angeles', + }, + 'recurrence': [ + 'RRULE:FREQ=DAILY;COUNT=2' + ], + 'attendees': [ + {'email': 'lpage@example.com'}, + {'email': 'sbrin@example.com'}, + ], + 'reminders': { + 'useDefault': False, + 'overrides': [ + {'method': 'email', 'minutes': 24 * 60}, + {'method': 'popup', 'minutes': 10}, + ], + }, +} def rrule_former(class_obj): days = class_obj.days @@ -39,19 +40,25 @@ def rrule_former(class_obj): days = [ (day -1) % 7 for day in days] ret = rrule.rrule(freq=rrule.WEEKLY,dtstart=start,wkst=rrule.SU,until=end,byweekday=days) - return ret - + ret_str = str(ret).split('\n')[-1] + ret_str=re.sub(r'(UNTIL=[^;]+)',r'\1Z',ret_str) + return 'RRULE:{}'.format(ret_str) def create_body(_class): if _class.time_range: - body = {} - body['recurrence'] = [str(rrule_former(_class))] + body = { + # "kind": "calendar#event", + } + body['recurrence'] = [rrule_former(_class)] body['start'] = dateTime(datetime.datetime.combine(_class.date_range[0],_class.time_range[0])) - body['end'] = dateTime(datetime.datetime.combine(_class.date_range[1],_class.time_range[0])) + body['end'] = dateTime(datetime.datetime.combine(_class.date_range[0],_class.time_range[1])) body['summary'] = _class.title body['description'] = 'location: {}'.format(_class.location) body['location'] = LOCATION + body['reminders'] = {'useDefault':True} return body - +def json_dump(obj): + with open('classes.json','w') as file: + json.dump(obj,file) def test_rrule(): #test now = datetime.datetime.now() @@ -73,5 +80,8 @@ def test_rrule(): def test_class2body(): with open('classes.pkl','rb') as file: classes = pickle.load(file) - test_result = list(map(create_body,classes)) - return test_result \ No newline at end of file + test_result = list(filter(bool,map(create_body,classes))) + return test_result + +if __name__ == "__main__": + json_dump(test_class2body()) \ No newline at end of file diff --git a/gcalendar.py b/gcalendar.py index 94885ee..936cfd6 100644 --- a/gcalendar.py +++ b/gcalendar.py @@ -83,11 +83,20 @@ class api: def create_event(self, calendar_id, body): + try: + calendar_id = self.ids[calendar_id] + except KeyError: + pass service = self.service event = service.events().insert(calendarId=calendar_id, body=body).execute() return event['id'] def update_event(self,calendar_id, event_id, body): + try: + calendar_id = self.ids[calendar_id] + except KeyError: + pass + service = self.service try: event = service.events().get(calendarId=calendar_id, eventId=event_id).execute() @@ -118,7 +127,7 @@ class api: page_token = None ret = [] while True: - events = service.events().list(calendarId='primary', pageToken=page_token).execute() + events = service.events().list(calendarId=id, pageToken=page_token).execute() ret += events['items'] page_token = events.get('nextPageToken') if not page_token: diff --git a/main.py b/main.py new file mode 100644 index 0000000..68a5862 --- /dev/null +++ b/main.py @@ -0,0 +1,13 @@ +from gcalendar import api +import json +import pprint +api = api(r'api_info\client_secret.json','api_info') +cals = api.get_calendars() +cal = next(filter(lambda cal: cal['id'] == api.ids['school schedule'],cals)) +with open('classes.json') as file: + bodies = json.load(file) +for body in bodies: + # body['colorId'] = cal['colorId'] + # pprint.pprint(body) + # input() + api.create_event('school schedule',body) \ No newline at end of file