1
00:00:00,060 --> 00:00:01,170
In previous lessons,

2
00:00:01,200 --> 00:00:05,700
we completed our automated birthday wisher Python project

3
00:00:06,000 --> 00:00:10,110
and also the motivational quotes on Monday's Python project.

4
00:00:10,620 --> 00:00:11,700
But at the moment,

5
00:00:11,700 --> 00:00:16,700
you'll notice that this code is only going to work to continuously check to see

6
00:00:17,250 --> 00:00:20,970
which day it is currently, compare it against the birthdays,

7
00:00:21,300 --> 00:00:22,710
and then run the code

8
00:00:23,070 --> 00:00:26,610
if this code is actually running constantly in the background.

9
00:00:27,210 --> 00:00:30,990
Now you could of course, open up your project in PyCharm,

10
00:00:31,050 --> 00:00:35,730
hit run every single day and make sure that it works,

11
00:00:36,000 --> 00:00:39,840
but that's actually more effort than wishing somebody happy birthday manually.

12
00:00:40,380 --> 00:00:42,330
So what do we do instead? Well,

13
00:00:42,330 --> 00:00:47,330
we want some sort of way where we can host our code in the cloud and run our code

14
00:00:49,290 --> 00:00:50,670
every single day.

15
00:00:51,390 --> 00:00:55,380
So one way of doing that is using a service called Python anywhere,

16
00:00:55,830 --> 00:01:00,270
and they have a free tier where we can do exactly that without having to pay

17
00:01:00,270 --> 00:01:03,030
anything. Head over to python

18
00:01:03,030 --> 00:01:07,020
anywhere.com and go ahead and sign up for a new account.

19
00:01:07,830 --> 00:01:09,960
Now, once you've signed up and signed in,

20
00:01:10,230 --> 00:01:13,710
then you want to go ahead and start adding some files.

21
00:01:14,250 --> 00:01:16,320
Head over to the files tab right here

22
00:01:16,740 --> 00:01:21,740
and we're going to upload the files that's currently inside our birthday wisher

23
00:01:22,080 --> 00:01:26,310
project. So that's our main.y, our birthdays.csv,

24
00:01:27,480 --> 00:01:29,640
and also our letter templates.

25
00:01:30,150 --> 00:01:34,530
Now we want our letter templates to be inside a folder, so we're going to

26
00:01:34,530 --> 00:01:38,340
create a new directory called letter_templates.

27
00:01:38,760 --> 00:01:40,470
Make sure there's no typos there.

28
00:01:41,130 --> 00:01:45,360
And then inside this directory is where we're going to upload these three files.

29
00:01:45,840 --> 00:01:47,850
So letter 1, 2 and 3.

30
00:01:48,870 --> 00:01:53,700
So now we've got our letters inside hom/AppBrewery/letter_

31
00:01:53,700 --> 00:01:55,980
templates, and inside AppBrewery

32
00:01:55,980 --> 00:01:58,740
we've got our birthdays.csv and our main.py.

33
00:01:59,400 --> 00:02:04,400
So now we're going to go into our console and we're going to create a new bash

34
00:02:05,430 --> 00:02:08,280
console. So we're going to click on this link right here,

35
00:02:09,570 --> 00:02:13,470
and then once all of that's loaded and you see this dollar sign right here,

36
00:02:13,740 --> 00:02:17,550
we're going to type python3 main.py.

37
00:02:18,960 --> 00:02:23,820
Now you might see this error right here, the SMTP authentication error.

38
00:02:24,360 --> 00:02:29,360
And this is something that happens because our Python program is trying to log

39
00:02:29,370 --> 00:02:33,510
into our email, and our email provider in this case,

40
00:02:33,540 --> 00:02:38,540
it's Google, is actually blocking it. So we can copy this URL and you can see it

41
00:02:39,510 --> 00:02:40,590
ends right here

42
00:02:40,590 --> 00:02:45,590
cause there's a space there, and we can paste it into the address bar and it

43
00:02:45,930 --> 00:02:47,220
takes us to this problem.

44
00:02:47,400 --> 00:02:52,200
I can't sign into my email client, and this is essentially what our Python code

45
00:02:52,200 --> 00:02:53,033
is trying to do.

46
00:02:53,400 --> 00:02:58,400
So what we need to do is click on this link to display the unlock capture, we

47
00:02:58,830 --> 00:03:03,670
click continue, and it's now enabled our code to sign in again.

48
00:03:04,120 --> 00:03:08,830
So we have to go back and type in exactly the same as we did before, python3

49
00:03:09,190 --> 00:03:10,120
main.py.

50
00:03:11,050 --> 00:03:14,980
But this time it should complete without giving us any errors

51
00:03:15,280 --> 00:03:19,000
and it does that by showing us the dollar sign again and our prompt.

52
00:03:19,570 --> 00:03:22,600
And now if I head back into my email,

53
00:03:22,810 --> 00:03:27,810
you can see that this email was actually sent because the code was initiated

54
00:03:29,290 --> 00:03:33,340
inside Pythonanywhere. So going back to our Python

55
00:03:33,340 --> 00:03:34,570
anywhere dashboard,

56
00:03:35,080 --> 00:03:39,040
we can go to the task section to actually schedule

57
00:03:39,280 --> 00:03:43,210
a task to run. Now to set up a scheduled task,

58
00:03:43,270 --> 00:03:46,930
we're going to type the same command that we did in the console

59
00:03:47,170 --> 00:03:49,870
which is python3 main.py.

60
00:03:50,560 --> 00:03:53,590
And then we're going to schedule this at a particular time.

61
00:03:54,010 --> 00:03:56,650
And the server time is based on UTC.

62
00:03:57,160 --> 00:04:01,180
So you can take a look at the current time in UTC

63
00:04:01,210 --> 00:04:03,790
by typing in UTC now into Google

64
00:04:04,120 --> 00:04:06,310
and you can see right now it's 12:09.

65
00:04:06,910 --> 00:04:10,900
So if I go ahead and schedule this for 12:10

66
00:04:11,440 --> 00:04:13,030
and I hit create,

67
00:04:13,540 --> 00:04:17,200
then at the time when this particular time hits,

68
00:04:17,470 --> 00:04:21,970
when it becomes 12:10, then our command should be run

69
00:04:22,300 --> 00:04:26,560
and we should see a happy birthday email show up in our testing Gmail account.

70
00:04:27,460 --> 00:04:30,970
So right now UTC has just gone to 12:10.

71
00:04:31,720 --> 00:04:34,810
And so if we wait for this schedule task,

72
00:04:35,080 --> 00:04:40,080
we can click on this button to view the task log and you do have to be quite

73
00:04:41,530 --> 00:04:46,530
patient because sometimes it might take a while before that task actually gets

74
00:04:46,810 --> 00:04:51,640
executed. So just keep checking the task log until you see this happen.

75
00:04:51,940 --> 00:04:56,320
Completed task at 12:10:56, o at the end of that minute.

76
00:04:56,710 --> 00:05:01,630
Took 16.92 seconds return code was zero, which means it was successful.

77
00:05:02,110 --> 00:05:07,110
And now we can see that email show up in our Gmail account and this was

78
00:05:07,480 --> 00:05:11,770
completely automated and it was done by Python anywhere.

79
00:05:12,160 --> 00:05:16,210
So now if we have that code running in the background every day,

80
00:05:16,240 --> 00:05:21,010
you can get it to run that code at this particular time or another time of your

81
00:05:21,010 --> 00:05:23,440
choosing in fact. If you're not testing it,

82
00:05:23,530 --> 00:05:28,030
you can actually get it to run much earlier. Let's say maybe seven o'clock UTC.

83
00:05:28,930 --> 00:05:31,180
Then it'll send the email out in the morning.

84
00:05:31,780 --> 00:05:36,760
And every day it's going to check whose birthday it is in your birthday CSV

85
00:05:36,850 --> 00:05:40,210
and then send an email to them and do all of that fancy stuff

86
00:05:40,330 --> 00:05:45,330
using what we did with Python. Python anywhere has really good forums for help

87
00:05:46,210 --> 00:05:48,910
and advice. And on the free tier,

88
00:05:48,940 --> 00:05:53,830
you can only schedule one task to run, so one particular code base.

89
00:05:54,190 --> 00:05:57,220
Now I don't work for them and I have no relations to them,

90
00:05:57,610 --> 00:06:02,050
but I find it really helpful to have a really simple way of getting my Python

91
00:06:02,050 --> 00:06:05,260
scripts to run everyday at a particular time.

92
00:06:05,500 --> 00:06:09,430
So have a look at that and you can use it if you find it helpful as well.

