In previous lessons,
we completed our automated birthday wisher Python project
and also the motivational quotes on Monday's Python project.
But at the moment,
you'll notice that this code is only going to work to continuously check to see
which day it is currently, compare it against the birthdays,
and then run the code
if this code is actually running constantly in the background.
Now you could of course, open up your project in PyCharm,
hit run every single day and make sure that it works,
but that's actually more effort than wishing somebody happy birthday manually.
So what do we do instead? Well,
we want some sort of way where we can host our code in the cloud and run our code
every single day.
So one way of doing that is using a service called Python anywhere,
and they have a free tier where we can do exactly that without having to pay
anything. Head over to python
anywhere.com and go ahead and sign up for a new account.
Now, once you've signed up and signed in,
then you want to go ahead and start adding some files.
Head over to the files tab right here
and we're going to upload the files that's currently inside our birthday wisher
project. So that's our main.y, our birthdays.csv,
and also our letter templates.
Now we want our letter templates to be inside a folder, so we're going to
create a new directory called letter_templates.
Make sure there's no typos there.
And then inside this directory is where we're going to upload these three files.
So letter 1, 2 and 3.
So now we've got our letters inside hom/AppBrewery/letter_
templates, and inside AppBrewery
we've got our birthdays.csv and our main.py.
So now we're going to go into our console and we're going to create a new bash
console. So we're going to click on this link right here,
and then once all of that's loaded and you see this dollar sign right here,
we're going to type python3 main.py.
Now you might see this error right here, the SMTP authentication error.
And this is something that happens because our Python program is trying to log
into our email, and our email provider in this case,
it's Google, is actually blocking it. So we can copy this URL and you can see it
ends right here
cause there's a space there, and we can paste it into the address bar and it
takes us to this problem.
I can't sign into my email client, and this is essentially what our Python code
is trying to do.
So what we need to do is click on this link to display the unlock capture, we
click continue, and it's now enabled our code to sign in again.
So we have to go back and type in exactly the same as we did before, python3
main.py.
But this time it should complete without giving us any errors
and it does that by showing us the dollar sign again and our prompt.
And now if I head back into my email,
you can see that this email was actually sent because the code was initiated
inside Pythonanywhere. So going back to our Python
anywhere dashboard,
we can go to the task section to actually schedule
a task to run. Now to set up a scheduled task,
we're going to type the same command that we did in the console
which is python3 main.py.
And then we're going to schedule this at a particular time.
And the server time is based on UTC.
So you can take a look at the current time in UTC
by typing in UTC now into Google
and you can see right now it's 12:09.
So if I go ahead and schedule this for 12:10
and I hit create,
then at the time when this particular time hits,
when it becomes 12:10, then our command should be run
and we should see a happy birthday email show up in our testing Gmail account.
So right now UTC has just gone to 12:10.
And so if we wait for this schedule task,
we can click on this button to view the task log and you do have to be quite
patient because sometimes it might take a while before that task actually gets
executed. So just keep checking the task log until you see this happen.
Completed task at 12:10:56, o at the end of that minute.
Took 16.92 seconds return code was zero, which means it was successful.
And now we can see that email show up in our Gmail account and this was
completely automated and it was done by Python anywhere.
So now if we have that code running in the background every day,
you can get it to run that code at this particular time or another time of your
choosing in fact. If you're not testing it,
you can actually get it to run much earlier. Let's say maybe seven o'clock UTC.
Then it'll send the email out in the morning.
And every day it's going to check whose birthday it is in your birthday CSV
and then send an email to them and do all of that fancy stuff
using what we did with Python. Python anywhere has really good forums for help
and advice. And on the free tier,
you can only schedule one task to run, so one particular code base.
Now I don't work for them and I have no relations to them,
but I find it really helpful to have a really simple way of getting my Python
scripts to run everyday at a particular time.
So have a look at that and you can use it if you find it helpful as well.