From 4f81d94120b5657a3e5534e62b7f6d64b0abb665 Mon Sep 17 00:00:00 2001 From: SAI UJJWAL U Date: Thu, 19 Aug 2021 11:36:58 +0530 Subject: [PATCH 1/6] Create AutoCalender --- AutoCalender | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 AutoCalender diff --git a/AutoCalender b/AutoCalender new file mode 100644 index 000000000..cab11ce11 --- /dev/null +++ b/AutoCalender @@ -0,0 +1,6 @@ + + +To start use the script you first need to go to https://developers.google.com/calendar/quickstart/python +Next press the "ENABLE THE GOOGLE CALENDAR API" to activate it. +Then you will get a popu with a file called "credentials.json" ,that you will need to download. +After that just add that file to the project root folder and then in the console or terminal you can run it by typing in: python calendarauto.py From 1b3f74dfd5945c971335a5128fec0769f3eb351b Mon Sep 17 00:00:00 2001 From: SAI UJJWAL U <19bcar1095@jainuniversity.ac.in> Date: Thu, 19 Aug 2021 14:15:10 +0530 Subject: [PATCH 2/6] new file: auto_calender/.gitignore new file: auto_calender/README.md new file: auto_calender/README.txt new file: auto_calender/calendarauto.py new file: auto_calender/requirements.txt new file: auto_calender/task.py --- auto_calender/.gitignore | 5 ++ auto_calender/README.md | 7 ++ auto_calender/README.txt | 7 ++ auto_calender/calendarauto.py | 135 +++++++++++++++++++++++++++++++++ auto_calender/requirements.txt | Bin 0 -> 708 bytes auto_calender/task.py | 29 +++++++ 6 files changed, 183 insertions(+) create mode 100644 auto_calender/.gitignore create mode 100644 auto_calender/README.md create mode 100644 auto_calender/README.txt create mode 100644 auto_calender/calendarauto.py create mode 100644 auto_calender/requirements.txt create mode 100644 auto_calender/task.py diff --git a/auto_calender/.gitignore b/auto_calender/.gitignore new file mode 100644 index 000000000..773aa6df6 --- /dev/null +++ b/auto_calender/.gitignore @@ -0,0 +1,5 @@ +__pycache__/ +credentials.json +token.pickle +*.pyc + diff --git a/auto_calender/README.md b/auto_calender/README.md new file mode 100644 index 000000000..b68d8edd5 --- /dev/null +++ b/auto_calender/README.md @@ -0,0 +1,7 @@ + + + + +To start use the script you first need to go to https://developers.google.com/calendar/quickstart/python adn press the "ENABLE THE GOOGLE CALENDAR API" to activate it. +Then you will get a popu with a file called credentials.json, that you will need to download. +After that just add that file to the project root folder and then in the console or terminal you can run it by typing in: python calendarauto.py \ No newline at end of file diff --git a/auto_calender/README.txt b/auto_calender/README.txt new file mode 100644 index 000000000..b68d8edd5 --- /dev/null +++ b/auto_calender/README.txt @@ -0,0 +1,7 @@ + + + + +To start use the script you first need to go to https://developers.google.com/calendar/quickstart/python adn press the "ENABLE THE GOOGLE CALENDAR API" to activate it. +Then you will get a popu with a file called credentials.json, that you will need to download. +After that just add that file to the project root folder and then in the console or terminal you can run it by typing in: python calendarauto.py \ No newline at end of file diff --git a/auto_calender/calendarauto.py b/auto_calender/calendarauto.py new file mode 100644 index 000000000..3191294cc --- /dev/null +++ b/auto_calender/calendarauto.py @@ -0,0 +1,135 @@ +from __future__ import print_function +import pickle +import os.path +from googleapiclient.discovery import build +from google_auth_oauthlib.flow import InstalledAppFlow +from google.auth.transport.requests import Request +from task import Task +from datetime import timedelta, datetime + + +categories = { + # Lavender + '#a4bdfc': '', + # Blueberry + '#5484ed': '', + # Peacock + '#46d6db': 'Exercise', + # Sage + '#7ae7bf': 'My Apps', + # Basil + '#51b749': 'App', + # Tangerine + '#ffb878': '', + # Banana + '#fbd75b': '', + # Flamingo + '#ff887c': '', + # Tomato + '#dc2127': 'YouTube', + # Mandarine + '#fa573c': '', + # Grape + '#dbadff': 'Work', + # Graphite + '#e1e1e1': 'School' +} + + +EVENTS_TO_LOOK_THROUGH = 60 +# If modifying these scopes, delete the file token.pickle. +SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] + + +def main(): + """Shows basic usage of the Google Calendar API. + Prints the start and name of the next 10 events on the user's calendar. + """ + creds = None + # The file token.pickle stores the user's access and refresh tokens, and is + # created automatically when the authorization flow completes for the first + # time. + if os.path.exists('token.pickle'): + with open('token.pickle', 'rb') as token: + creds = pickle.load(token) + # If there are no (valid) credentials available, let the user log in. + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + 'credentials.json', SCOPES) + creds = flow.run_local_server() + # Save the credentials for the next run + with open('token.pickle', 'wb') as token: + pickle.dump(creds, token) + + service = build('calendar', 'v3', credentials=creds) + + # Call the Calendar API + start_day = datetime.utcnow() + now = datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time + last_monday = (datetime.utcnow()- timedelta(start_day.weekday())).isoformat() + 'Z' # 'Z' indicates UTC time + week_end_time = str(datetime.utcnow() + timedelta(days=7)) + 'Z' + print('Getting the upcoming 10 events') + print('**************************************************************\n') + events_result = service.events().list(calendarId='primary', timeMin=last_monday, timeMax=now, + maxResults=EVENTS_TO_LOOK_THROUGH, singleEvents=True, + orderBy='startTime').execute() + colors = service.colors().get(fields='event').execute() + defaultColor = (service.calendarList().get(calendarId="primary").execute())['backgroundColor'] + events = events_result.get('items', []) + tasks = [] + + if not events: + print('No upcoming events found.') + for event in events: + start_string = event['start'].get('dateTime', event['start'].get('date')) + end_string = event['end'].get('dateTime', event['end'].get('date')) + + name = event['summary'] + try: + color = colors['event'][event['colorId']]['background'] + except KeyError: + color = defaultColor + task = Task(name, parse_time(start_string), parse_time(end_string), color) + task.date = parse_time(start_string) + task.get_time_of_task() + tasks.append(task) + + total_tasks = [] + for color, category in categories.items(): + if len(category) > 1: + total_time = timedelta(hours=0) + for task in tasks: + if task.color == color: + total_time += task.total_time + text = "For " + category + " you have planned to spend:" + number_of_spaces = 15 + number_of_spaces -= len(category) + string_length=len(text)+number_of_spaces # will be adding 10 extra spaces + string_revised=text.ljust(string_length) + print("\n-----------------------------------------------------------------") + print(string_revised + format_timedelta(total_time) + "hrs this week") + print("\n-----------------------------------------------------------------\n") + print("\n********************************************************\n") + + +def parse_time(timestamp): + # Takes a timestamp string and returns a datetime object + try: + time_object = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S%z") + except ValueError: + # If no start and end time is specified the format string must be different + time_object = datetime.strptime(timestamp, "%Y-%m-%d") + return time_object + + +def format_timedelta(timedelta): + # Takes a timedelta and returns a string + hours = timedelta.total_seconds() / 3600 + return("%.2f" % hours) + + +if __name__ == '__main__': + main() diff --git a/auto_calender/requirements.txt b/auto_calender/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..827d9319261ff2bb72ca5227c7585cb147ef6129 GIT binary patch literal 708 zcmaKq;cmh(421oA(jFyH((v73bY09xRZivcxAucRXE4U#Cw5PJm3YgjI2}4v7rY=sy^+ Date: Wed, 29 Sep 2021 12:11:13 +0530 Subject: [PATCH 3/6] Delete AutoCalender --- AutoCalender | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 AutoCalender diff --git a/AutoCalender b/AutoCalender deleted file mode 100644 index cab11ce11..000000000 --- a/AutoCalender +++ /dev/null @@ -1,6 +0,0 @@ - - -To start use the script you first need to go to https://developers.google.com/calendar/quickstart/python -Next press the "ENABLE THE GOOGLE CALENDAR API" to activate it. -Then you will get a popu with a file called "credentials.json" ,that you will need to download. -After that just add that file to the project root folder and then in the console or terminal you can run it by typing in: python calendarauto.py From 99700cc88d7dce48163024b58ecb1c18fd310117 Mon Sep 17 00:00:00 2001 From: Pawan Jain <42181691+pawangeek@users.noreply.github.com> Date: Wed, 29 Sep 2021 12:11:32 +0530 Subject: [PATCH 4/6] Delete .gitignore --- auto_calender/.gitignore | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 auto_calender/.gitignore diff --git a/auto_calender/.gitignore b/auto_calender/.gitignore deleted file mode 100644 index 773aa6df6..000000000 --- a/auto_calender/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -__pycache__/ -credentials.json -token.pickle -*.pyc - From f81c6b5d47ba414e70c716d3179aa611833358ae Mon Sep 17 00:00:00 2001 From: Pawan Jain <42181691+pawangeek@users.noreply.github.com> Date: Wed, 29 Sep 2021 12:11:44 +0530 Subject: [PATCH 5/6] Delete README.txt --- auto_calender/README.txt | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 auto_calender/README.txt diff --git a/auto_calender/README.txt b/auto_calender/README.txt deleted file mode 100644 index b68d8edd5..000000000 --- a/auto_calender/README.txt +++ /dev/null @@ -1,7 +0,0 @@ - - - - -To start use the script you first need to go to https://developers.google.com/calendar/quickstart/python adn press the "ENABLE THE GOOGLE CALENDAR API" to activate it. -Then you will get a popu with a file called credentials.json, that you will need to download. -After that just add that file to the project root folder and then in the console or terminal you can run it by typing in: python calendarauto.py \ No newline at end of file From 3f5960131895b8b1a306e6d6b80177bcb1773945 Mon Sep 17 00:00:00 2001 From: Pawan Jain <42181691+pawangeek@users.noreply.github.com> Date: Wed, 29 Sep 2021 12:12:44 +0530 Subject: [PATCH 6/6] Update README.md --- auto_calender/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/auto_calender/README.md b/auto_calender/README.md index b68d8edd5..29a6c0966 100644 --- a/auto_calender/README.md +++ b/auto_calender/README.md @@ -1,7 +1,7 @@ - - - +## Steps to perform To start use the script you first need to go to https://developers.google.com/calendar/quickstart/python adn press the "ENABLE THE GOOGLE CALENDAR API" to activate it. + Then you will get a popu with a file called credentials.json, that you will need to download. -After that just add that file to the project root folder and then in the console or terminal you can run it by typing in: python calendarauto.py \ No newline at end of file + +After that just add that file to the project root folder and then in the console or terminal you can run it by typing in: python calendarauto.py