WEBVTT

0
00:00.060 --> 00:01.170
In previous lessons,

1
00:01.200 --> 00:05.700
we completed our automated birthday wisher Python project

2
00:06.000 --> 00:10.110
and also the motivational quotes on Monday's Python project.

3
00:10.620 --> 00:11.700
But at the moment,

4
00:11.700 --> 00:16.700
you'll notice that this code is only going to work to continuously check to see

5
00:17.250 --> 00:20.970
which day it is currently, compare it against the birthdays,

6
00:21.300 --> 00:22.710
and then run the code

7
00:23.070 --> 00:26.610
if this code is actually running constantly in the background.

8
00:27.210 --> 00:30.990
Now you could of course, open up your project in PyCharm,

9
00:31.050 --> 00:35.730
hit run every single day and make sure that it works,

10
00:36.000 --> 00:39.840
but that's actually more effort than wishing somebody happy birthday manually.

11
00:40.380 --> 00:42.330
So what do we do instead? Well,

12
00:42.330 --> 00:47.330
we want some sort of way where we can host our code in the cloud and run our code

13
00:49.290 --> 00:50.670
every single day.

14
00:51.390 --> 00:55.380
So one way of doing that is using a service called Python anywhere,

15
00:55.830 --> 01:00.270
and they have a free tier where we can do exactly that without having to pay

16
01:00.270 --> 01:03.030
anything. Head over to python

17
01:03.030 --> 01:07.020
anywhere.com and go ahead and sign up for a new account.

18
01:07.830 --> 01:09.960
Now, once you've signed up and signed in,

19
01:10.230 --> 01:13.710
then you want to go ahead and start adding some files.

20
01:14.250 --> 01:16.320
Head over to the files tab right here

21
01:16.740 --> 01:21.740
and we're going to upload the files that's currently inside our birthday wisher

22
01:22.080 --> 01:26.310
project. So that's our main.y, our birthdays.csv,

23
01:27.480 --> 01:29.640
and also our letter templates.

24
01:30.150 --> 01:34.530
Now we want our letter templates to be inside a folder, so we're going to

25
01:34.530 --> 01:38.340
create a new directory called letter_templates.

26
01:38.760 --> 01:40.470
Make sure there's no typos there.

27
01:41.130 --> 01:45.360
And then inside this directory is where we're going to upload these three files.

28
01:45.840 --> 01:47.850
So letter 1, 2 and 3.

29
01:48.870 --> 01:53.700
So now we've got our letters inside hom/AppBrewery/letter_

30
01:53.700 --> 01:55.980
templates, and inside AppBrewery

31
01:55.980 --> 01:58.740
we've got our birthdays.csv and our main.py.

32
01:59.400 --> 02:04.400
So now we're going to go into our console and we're going to create a new bash

33
02:05.430 --> 02:08.280
console. So we're going to click on this link right here,

34
02:09.570 --> 02:13.470
and then once all of that's loaded and you see this dollar sign right here,

35
02:13.740 --> 02:17.550
we're going to type python3 main.py.

36
02:18.960 --> 02:23.820
Now you might see this error right here, the SMTP authentication error.

37
02:24.360 --> 02:29.360
And this is something that happens because our Python program is trying to log

38
02:29.370 --> 02:33.510
into our email, and our email provider in this case,

39
02:33.540 --> 02:38.540
it's Google, is actually blocking it. So we can copy this URL and you can see it

40
02:39.510 --> 02:40.590
ends right here

41
02:40.590 --> 02:45.590
cause there's a space there, and we can paste it into the address bar and it

42
02:45.930 --> 02:47.220
takes us to this problem.

43
02:47.400 --> 02:52.200
I can't sign into my email client, and this is essentially what our Python code

44
02:52.200 --> 02:53.033
is trying to do.

45
02:53.400 --> 02:58.400
So what we need to do is click on this link to display the unlock capture, we

46
02:58.830 --> 03:03.670
click continue, and it's now enabled our code to sign in again.

47
03:04.120 --> 03:08.830
So we have to go back and type in exactly the same as we did before, python3

48
03:09.190 --> 03:10.120
main.py.

49
03:11.050 --> 03:14.980
But this time it should complete without giving us any errors

50
03:15.280 --> 03:19.000
and it does that by showing us the dollar sign again and our prompt.

51
03:19.570 --> 03:22.600
And now if I head back into my email,

52
03:22.810 --> 03:27.810
you can see that this email was actually sent because the code was initiated

53
03:29.290 --> 03:33.340
inside Pythonanywhere. So going back to our Python

54
03:33.340 --> 03:34.570
anywhere dashboard,

55
03:35.080 --> 03:39.040
we can go to the task section to actually schedule

56
03:39.280 --> 03:43.210
a task to run. Now to set up a scheduled task,

57
03:43.270 --> 03:46.930
we're going to type the same command that we did in the console

58
03:47.170 --> 03:49.870
which is python3 main.py.

59
03:50.560 --> 03:53.590
And then we're going to schedule this at a particular time.

60
03:54.010 --> 03:56.650
And the server time is based on UTC.

61
03:57.160 --> 04:01.180
So you can take a look at the current time in UTC

62
04:01.210 --> 04:03.790
by typing in UTC now into Google

63
04:04.120 --> 04:06.310
and you can see right now it's 12:09.

64
04:06.910 --> 04:10.900
So if I go ahead and schedule this for 12:10

65
04:11.440 --> 04:13.030
and I hit create,

66
04:13.540 --> 04:17.200
then at the time when this particular time hits,

67
04:17.470 --> 04:21.970
when it becomes 12:10, then our command should be run

68
04:22.300 --> 04:26.560
and we should see a happy birthday email show up in our testing Gmail account.

69
04:27.460 --> 04:30.970
So right now UTC has just gone to 12:10.

70
04:31.720 --> 04:34.810
And so if we wait for this schedule task,

71
04:35.080 --> 04:40.080
we can click on this button to view the task log and you do have to be quite

72
04:41.530 --> 04:46.530
patient because sometimes it might take a while before that task actually gets

73
04:46.810 --> 04:51.640
executed. So just keep checking the task log until you see this happen.

74
04:51.940 --> 04:56.320
Completed task at 12:10:56, o at the end of that minute.

75
04:56.710 --> 05:01.630
Took 16.92 seconds return code was zero, which means it was successful.

76
05:02.110 --> 05:07.110
And now we can see that email show up in our Gmail account and this was

77
05:07.480 --> 05:11.770
completely automated and it was done by Python anywhere.

78
05:12.160 --> 05:16.210
So now if we have that code running in the background every day,

79
05:16.240 --> 05:21.010
you can get it to run that code at this particular time or another time of your

80
05:21.010 --> 05:23.440
choosing in fact. If you're not testing it,

81
05:23.530 --> 05:28.030
you can actually get it to run much earlier. Let's say maybe seven o'clock UTC.

82
05:28.930 --> 05:31.180
Then it'll send the email out in the morning.

83
05:31.780 --> 05:36.760
And every day it's going to check whose birthday it is in your birthday CSV

84
05:36.850 --> 05:40.210
and then send an email to them and do all of that fancy stuff

85
05:40.330 --> 05:45.330
using what we did with Python. Python anywhere has really good forums for help

86
05:46.210 --> 05:48.910
and advice. And on the free tier,

87
05:48.940 --> 05:53.830
you can only schedule one task to run, so one particular code base.

88
05:54.190 --> 05:57.220
Now I don't work for them and I have no relations to them,

89
05:57.610 --> 06:02.050
but I find it really helpful to have a really simple way of getting my Python

90
06:02.050 --> 06:05.260
scripts to run everyday at a particular time.

91
06:05.500 --> 06:09.430
So have a look at that and you can use it if you find it helpful as well.