How I Automate My TIME MANAGEMENT using PYTHON & Google Calendar

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to yet another python tutorial in this video we're going to be building ourselves a time management system using python and google calendar i've said in many videos that when you're learning to code it's really useful to have an hourly goal of how many hours of coding you want to hit each day because then you can keep yourself accountable and know exactly how many hours you're putting in but i realized that i never actually know how many hours of deep work i'm putting in every day so that is why i decided to build a python script to track my time and know exactly where my time is going every single day in this video you are going to learn how to do exactly all of these step by step so if you appreciate that hit the like button down below we recently hit 100 000 subscribers on this channel and i'm on a mission to make the best coding videos on youtube so that we can hit a million subscribers so if you enjoyed this video do join the movement with that let's get into the tutorial as always this specific program is gonna be for my specific needs but i highly recommend you watch through this video get inspired about what i'm doing because you are going to be learning some very useful skills like how to access the google calendar api and in general figuring out how to do more magical things in python which is always very exciting isn't it the first thing we need to do is connect to the google calendar api so let's see if i still remember how to do this so first you need to create a project in the google cloud console then you need to go to the left hand side here click apis and services enable apis and services then search for google calendar you're going to click on google calendar api and click enable the next thing you need to do is go to oauth content screen on the left the only option you'll have is external so you're going to click that and create then give your app a name select your email as a user support email then you're going to scroll all the way down here and again input your email click on add or remove scopes and necessarily just scroll down find all the ones related to google calendar i have no idea what all of these do but i'm just selecting all of them just to be safe then you're going to go down click save and continue then continue again you're going to go to credentials click create credentials and oauth client id as application type i'm just going to put desktop application then click on create and you're going to click on download json we need to do with this file is you need to move it to the same folder where you're going to be writing your code and rename it to credentials.json after that you can copy this quick start code from this link which i'm going to leave down below before we do this the only change we'll make is in the scopes remove the dot read only because what we want to do is give ourselves the ability to actually modify our calendar and not just read it you should ask for our permission to allow ourselves to access our own app and that was not supposed to happen okay i figured out what was wrong in order to fix this error we need to do is go down here back to our console and we're going to find add test users so we're going to add ourselves as a test user for this app to allow ourselves to use our own app and now when we do this we can just click on continue and then authorize ourselves to be a user of our app and voila we are now connected to the google cloud api if you have some items in your calendar it should retrieve 10 items for your calendar and show them to you as you can see the only events i've placed in my calendar is to meet my friend conrad apparently so now we can get into some python magic business starting from the main function what all of this does is simply connects you the api so we don't need to worry about that starting this line is line 35 we're actually going to wrap this into a function now this line is going to connect the api instead of getting the upcoming 10 events what we want to do is essentially save all of the past events from this day into a database to get the day we're going to change this a little bit we're gonna go get today's day using daytime.day.today and basically these lines just give you the time and date from the start of the day in the correct format till the end of the day in the correct format it's just the format that you need to use with google calendar api you don't need to worry about it and now for this one what you need to change is this calendar id what you need to do is go to your google calendar and when you go into the settings you're going to find this id for the current canada for example for me i have several calendars for example for my programming hours i have one calendar and i've set up another calendar to track my youtube hours for example and you're gonna copy it here this primary is essentially just gonna use your default one so if you use your default one you're gonna leave this as primary we're gonna copy this down there so this whole line is going to be getting events from your calendar you want to set up the start time and the end time for the window from which is going to be grabbing these events so that's why we've set up this time mean to be this time start variable which we defined before the time max will be this time n that we defined before as you just said single events true order by start time and time zone whatever time zone you have and then dot execute which is going to save these events it is evidence result variable then we're going to go events underscore results dot get there's nothing there i'm going to print no upcoming edits found now the first thing that we're going to be doing is adding up the duration of all the hours that we find based on our api call it's going to be a little bit complicated because when we get the time from the api it's going to be in this weird iso time format we're going to set it to date time dot time delta which creates the time delta class seconds minutes hours why it's like this it's going to become clear in a second print coding hours then for event in events basically now we first need to get the duration of an event we need to do a bit of trickery but it's all good we're going to do is use these lines to the start of the event is going to be event.start don't get and basically this kind of stuff essentially how i found this is on stack overflow so these two are gonna get the start and end time of your event and what we still need to do is reformat it into this specific format that we want and for this we also need to import from date util this course function actually better names for this would probably be something like start formatted and end formatted and the duration is simply going to be and formatted minus start formatted now we have the duration of the event and we're gonna add it to our sum this we can simply do by going to the duration plus equals duration and then we're gonna print on the command line summary of the event and the duration and at the end of all of this we're gonna print total coding time with the version and we're going to remove these default lines right here because we're not going to name them anymore and we've just got this error handler here automatically as well and now lastly of course in our main function up here we're going to just make sure we run our function commit hours with credits inside when we run this i've set up some test events on my own calendar so they should show up and as you can see it grabs these two events which i've got on my google calendar and shows the total time as the sum of all of this now next thing we're going to set up an sql database to save these hours into so that we can grab them later in later dates and compare where our time is going to over time but first i need to confess you something i didn't actually used to be very good at sql at all and that was because i could never really find the right resource that would teach me sql in a way that i couldn't understand it and also that would be engaging enough to make the learning process enjoyable because that's what it's really all about for me so when some time ago i got reached out to by learnsql.com i was like yeah why not i'll try your courses and i was actually very very impressed i ended up even doing these courses myself and that is why i'm very happy to say that i have partnered with learnsql.com for them to sponsor this video if you want to learn and practice sql learnsql.com have a set of over 50 hands-on sql courses their platform offers a 100 online experience where you have no need to install anything on your device and everything seamlessly happens through your favorite web browser all learn sql.com courses are interactive and based on real-world business scenarios meaning when you write sql queries you'll be seeing the results instantly i honestly think that learn sql has some of the best sql courses out there and i can personally vouch for them because they actually helped me and their reviews and reputation definitely agrees you'll find a link to learnsql.com in the first line of the description for you to check out their offer and choose one of the learning paths from either sql postgresql or tsql so go check them out below today and i want to thank learnsql.com for sponsoring this video and obviously at this point we're just doing this for my programming hours if you want to track your time watching anime i won't i won't judge you so first we're going to go and import sqlite and next it is time to learn how to use sql then i've actually defined this other file where we essentially create the table where these hours are going to be saved this one you just need to run once first we're going to create the actual file and connect to it using these lines up here then we're going to go con.execute and whenever you run sql commands using python you need to start it using three of these single quotation marks then create table and the name of the table and then all the columns of the table that you want so we want a date which is going to be of type date not null category which is text and hours which is an integer again if all of this is confusing to you go do some courses from learnersql.com basically you just run this once to initialize the table and then we can continue on with our main file we're again going to get today's day go by going daytime.a dot today we first need to format our time a little bit we're going to go format the total duration equals total underscore duration dot seconds which is going to get all the time in seconds and then divided by 60 divided by 60 which is going to get it in hours this is so that we can actually input it as an actual number into a database rather than this weird google calendar api time format calling underscore hours equals tuple of date the name of coding this is sort of in table i can separate what are coding hours and what are youtube hours and whatever else i want to add to in the future so that's a category and then formatted total duration as the time and execute this cur dot execute and then these lines insert into table name values and it's going to look a bit weird basically what this does is these question marks which is the values that are going to be inserted into the table are going to be replaced by this tuple that we created up above here after that all you have to do is talk on dark commit you're going to commit our changes into the table and after you run this if there's no errors you should find this house.db file on your left and if you're on vs code in order to actually view this you're going gonna have to install this sqlite viewer extension and with any other text editor you can probably google how to view these database files on your coding editors but as you can see in here i've already got some hours from my previous days and once you run this file what should happen is that my today's hours are added into the database so we're going to try that you can see codinghouse added the database successfully and when we refresh this you can actually see that today's hours which is the 9th of august are successfully in the database first step of our program done wasn't too bad was it give yourself a pattern in the back and let's keep going the next thing we want to do is be able to create an event just from our command line just from our python program rather than having to open up google calendar create a new event and do all of that which just takes a lot of time essentially we're gonna create another function called add event inside of there we're gonna have to pass a couple of things first the credits then a duration that we wanted to give to our event and then description to the event but now this is just going to be arguments with the function but at the end of the video i'm going to show you how to do this directly from the command line using shell scripting again we're going to do some stuff with the time essentially you just go daytime.daytime.utc now a couple of more lines but essentially what we're doing here is creating an event from the current time up until however many hours we define as the duration so the end time is going to be daytime.daytime start plus a daytime dot time delta which creates this delta so essentially a difference from the current time into the end time and the way we just do this is using this line right here and inside the hours we're going to define as a duration and then again we're going to need to do some formatting format is going to be start.iso format first is that letter which i don't know exactly how this works but essentially this is again just a format that good the google calendar api wants then we're going to need to define this event variable right here essentially this here is going to define what our event is going to be you know google calendar when you go to manually add an event using the gui is going to give you a lot of options to give you like notifications about it the description the time the name the category all of that and using code the way we define this is using this object right here and i've just got the bare bones of the things that i actually wanted to put inside the event if you want to put more information than this you can go do the google calendar api documentation down below and it's going to show you all the options that you can give your event versus the all i need is the summary which is the description variable which we've defined as an argument of the function start day which is going to be in this kind of weird format again where the date time is the start formatted which we just created time zone again my time zone which is in london in the uk and then end is going to be this end formatted variable which we just created then we're going to go with service equals very similar thing that we do in the past then to actually insert this event in the calendar we're going to go service dot events insert this is the key function right here and get the calendar id you're gonna select the same calendar id you wanna add it to and then a body equals this event variable that we defined and then simply dot execute which is actually going to place this event into your google calendar which is going to be pretty exciting then the engineers are going to print event created show yourself that it was created successfully now what you're going to define all these variables let's say a duration of two hours and a description of hello youtube and once we run this you'd actually arrive in my calendar and of course there's an error okay i figured it out what we need to do instead is in here as well just go daytime.daytime.utc now and then add this time delta u at the end now when we run this it should work and as you can see the hello youtube event is in our google calendar straight from our python code isn't that amazing if you find it that amazing leave a like down below now we're almost done we now just need to do is be able to do this from our command line first we need ways to run either this commit hours function or the add event function not both we first need to import from cis arg b if you're not familiar with arc b augbis essentially just allows you to access command line arguments that you pass your python function from the command line and here we're going to go if r b one so augmented zero is always just going to be your program name rgb1 equals let's say if we give it the argument of add and what we want to do is run this add event function whereas if the first argument is let's say commit then we want run to commit our function obviously for the ad we would need to be able to get the parameters of the function from the command line so let's say the first argument is going to be the duration the second is going to be the description we're going to set duration as rb 2 and the description as rb3 and then to the function all we're going to do is pass in the duration and the description that we've got through the commit we basically don't need to do any of that the idea how i'm gonna be using this is that whenever i start working i run this ad function to add an event for however many hours of deep work i want to do in that moment and then at the end of the day i just run this commit function to commit all the hours of the day into the database so i'm going to buy add let's say a three hour event and sort of work because for some reason it's sticking in as a string rather than integers which is going to go in back in the add event function and make this duration forced into an integer and now let's see if it works seems to have worked the last thing we're going to do necessarily going to be creating an alias to run this program from this directory which is one command open up your finder and click command shift period to show your hidden files exciting right if you're on mac you're gonna find this dot z profile file and open it and here you can create these aliases go alias and then let's say time manager equals then we're gonna grab present working directory we're gonna set it here like this and the name of the file essentially this is just an alias for this full part of the program okay so to make this work you have to restart your terminal window it looks like in order to make this work what i also have to do is copy the credential.json file into the root directory which opens up when you open a new terminal window with that as you do is it asks you to authorize again and once you do that you can create events in your canada and run this whole thing just from your terminal though without having to actually open up your python file now the last thing is function which i've done for myself which is this get hours function it allows you to as i said get all the hours and show them to you now this one i will actually leave to you as an exercise as i always say the best way to learn to code is to do stuff for yourself and i believe with this background knowledge that i've done in the past using it uh googling it using stack overflow calendar api documentation you should be able to figure out how to do this goal of our program is that you can run your program again just with some keyword then define how many days you want to get the hours from we're going to say 5 for example and it's going to show you all your hours from these days as well as calculate the total and calculate the average and print them all to you i am going to leave my code for this specific function down below in the description which you can look at as a solution or as inspiration if you do get stuck do definitely try to do this yourself first if this was helpful to you you're welcome to use exactly what i did but what i obviously recommend you do is figure out how to apply this for your specific needs as i always say most exciting thing about coding is building stuff that's useful for you allows you to do things automate things that you do anyway just optimize your life essentially for more python tutorials like this i made these two videos up here they've been extremely popular on my channel so i think you'll really enjoy them also if you enjoyed this video i highly recommend you subscribe to the channel i'm on a mission to make the best coding videos on the internet we're gonna hit a million subscribers go join the movement down below with that let's all keep coding and enjoy the journey along the way i will see you next time [Music]
Info
Channel: Internet Made Coder
Views: 53,767
Rating: undefined out of 5
Keywords: python automation projects, automate the boring stuff with python, learn python, python automation, automate your life, learn python fast, python fundamentals, bash scripting, learn to code, coding projects, coding project ideas, python tutorial, desktop automation, coding tutorial, python 101, how to automate tasks for beginners
Id: vUOtS6zU40A
Channel Id: undefined
Length: 18min 22sec (1102 seconds)
Published: Wed Aug 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.