1
00:00:01,760 --> 00:00:10,710
OK I think it's an odd time to code and I'll be putting everything in one script.

2
00:00:10,710 --> 00:00:15,230
So let's call this website blocker.py.

3
00:00:16,090 --> 00:00:16,440
And yeah again

4
00:00:16,500 --> 00:00:22,560
The idea is that I'll be accessing this host file which is this directory.

5
00:00:22,590 --> 00:00:30,300
If you are on Windows you'll get it here. If you are on a Mac or Linux that would be directly at this

6
00:00:30,300 --> 00:00:33,250
folder and then you have the host file.

7
00:00:33,400 --> 00:00:39,510
So you get the idea and so let's keep that path in a variable.

8
00:00:40,210 --> 00:00:47,970
Let's say hosts path and that would be equal to� I had copied this earlier.

9
00:00:47,970 --> 00:00:51,750
So I'm just pasting it here. So on Windows

10
00:00:51,750 --> 00:01:00,680
these are backslashes on the Mac or Linux you'd have forward slash and then etc and then you have of course the  hosts

11
00:01:01,840 --> 00:01:03,540
name so the name of the file.

12
00:01:03,770 --> 00:01:08,310
If you are on Windows you should be careful about something. These back slashes

13
00:01:08,310 --> 00:01:15,870
sometimes you might have something like let's say N. A folder that starts with the N character.

14
00:01:16,140 --> 00:01:20,460
What that will do is Python will read this as a special character.

15
00:01:20,640 --> 00:01:24,600
And what this special character means is� It means a break line.

16
00:01:24,600 --> 00:01:32,070
So when you pass backslash and Python thinks you're passing a Python keyword not actually a string.

17
00:01:32,070 --> 00:01:32,600

18

00:01:32,670 --> 00:01:38,940
In that case what you could do is you could pass R prefix there which raw.

19
00:01:39,240 --> 00:01:42,310
That means� Remove this.

20
00:01:42,590 --> 00:01:51,360
With this you'd tell Python that you're actually passing a raw string in there so you're not planning to pass any

21
00:01:51,440 --> 00:01:54,000
brake lines in there like a backslash.

22
00:01:54,170 --> 00:01:56,720
And I hope that is clear.

23
00:01:56,760 --> 00:02:04,680
Or alternatively you can remove this and you can add two backslashes in each of these directories separators.

24
00:02:04,700 --> 00:02:05,010
.

25
00:02:05,340 --> 00:02:07,070
So that would be an alternative solution.

26
00:02:07,080 --> 00:02:10,800
But let's keep it like this.

27
00:02:11,120 --> 00:02:18,510
Good. And maybe two other variables let's say redirect equals to this string.

28
00:02:19,890 --> 00:02:26,310
So the I.P where your browse visit will be redirected when you browse Facebook or other things that

29
00:02:26,310 --> 00:02:28,280
you find distracting.

30
00:02:28,760 --> 00:02:35,220
And then let's say website so you need to pass the websites that you want to block.

31
00:02:36,000 --> 00:02:38,430
That could be a list.

32
00:02:38,490 --> 00:02:40,540
So I'm calling this website list here.

33
00:02:40,560 --> 00:02:47,510
Here I'll pass the www.Facebook.com.

34
00:02:47,820 --> 00:02:55,610
And also you want to pass Facebook.com as well because sometimes these websites use 3 w there

35
00:02:55,920 --> 00:02:58,560
for their domain but sometimes they don't.

36
00:02:58,680 --> 00:03:07,240
So you want to pass both of these domains in the host file and then maybe my hotmail account.

37
00:03:09,240 --> 00:03:11,570
So how do I know that this is the domain?

38
00:03:11,580 --> 00:03:19,760
Well if I typed Hotmail.com and I can see that Hotmail.com is sending me to this domain in

39
00:03:19,760 --> 00:03:23,310
the browser so I can see this domain in the browser.

40
00:03:23,580 --> 00:03:27,400
This opens up my email displays.

41
00:03:27,420 --> 00:03:29,320
So this is what you want to pass.

42
00:03:29,340 --> 00:03:37,230
You want to see the final landing domain and maybe the other version.

43
00:03:37,810 --> 00:03:40,940
And yeah, we should be good to go. We have a list with four elements.

44
00:03:41,010 --> 00:03:45,720
That means the host file we will have four lines.

45
00:03:45,810 --> 00:03:52,570
I currently have this Facebook's  here. So I am gonna save the host again with all those.

46
00:03:53,510 --> 00:04:00,980
And I'll make sure I don't have any Python scripts running here in the processes.

47
00:04:01,190 --> 00:04:03,440
I don't have any.

48
00:04:03,790 --> 00:04:08,210
And I was afraid that I had a running script, a Python script there.

49
00:04:08,460 --> 00:04:11,710
So that's why I checked in the processes.

50
00:04:11,850 --> 00:04:13,930
So how are we going to do this?

51
00:04:14,370 --> 00:04:17,670
Let me put this here and this here.

52
00:04:17,910 --> 00:04:25,980
Well our goal is to have this this file modified at let's say 8 a.m..

53
00:04:26,190 --> 00:04:33,490
So at that time I want these four Web sites these four lines to be added in here together with this IP.

54
00:04:33,510 --> 00:04:34,050

55

00:04:34,480 --> 00:04:42,870
And then I want these four lines to be removed at let's say 4:00 p.m. and there are maybe two ways to do

56
00:04:42,870 --> 00:04:43,340
this.

57
00:04:43,350 --> 00:04:49,920
One way would be to write a simple Python script that all it does is it opens this file.

58
00:04:50,130 --> 00:04:58,720
Let's say with open host path as file and then write these elements of the list in that file in that host file.

59
00:04:58,800 --> 00:04:59,530

60

00:05:00,140 --> 00:05:01,730
And that's all.

61
00:05:01,900 --> 00:05:09,930
And then what you do is you may use this task scheduler on Windows or Cron on Linux or other

62
00:05:09,930 --> 00:05:16,290
packages that do schedules tasks and you may set this Python scripts to run in certain times.

63
00:05:16,290 --> 00:05:16,560

64

00:05:16,560 --> 00:05:18,140
That is one solution.

65
00:05:18,160 --> 00:05:23,100
But why would I use third party tools when I know Python.

66
00:05:23,190 --> 00:05:29,670
I mean I know Python better than these tools and I'm not 100 percent sure that with these tools I'll

67
00:05:29,670 --> 00:05:32,440
do exactly what I'm after.

68
00:05:32,670 --> 00:05:37,830
But with Python that is a programming language if you set to doing something if you think about something

69
00:05:38,370 --> 00:05:43,500
that's almost a hundred percent sure that you will do that because you have a lot of flexibility

70
00:05:43,500 --> 00:05:49,380
with a programming language so it's good that if you know programming language then you should seek

71
00:05:49,380 --> 00:05:51,040
for a native solution.

72
00:05:51,050 --> 00:05:57,270
And yeah, that's what I'll do in this case. I will not just write a simple script that adds to these four

73
00:05:57,270 --> 00:05:59,240
lines of code in this host file.

74
00:05:59,430 --> 00:06:07,170
Instead I'll have this script running all the time and to do that I'll use a while loop.

75
00:06:09,990 --> 00:06:16,500
While true and then everything goes inside this while loop. What while loop does

76
00:06:16,530 --> 00:06:18,630
is it execute an action

77
00:06:18,720 --> 00:06:23,190
very very fast so it depends on your processor speed.

78
00:06:23,190 --> 00:06:29,290
But in my case for instance what this will do it will print out one every�

79
00:06:29,370 --> 00:06:30,910
Let me guess, one millisecond.

80
00:06:31,050 --> 00:06:35,360
So it depends on your processor speed but maybe faster then one millisecond.

81
00:06:35,700 --> 00:06:39,740
And let me open a command line and execute this script.

82
00:06:39,820 --> 00:06:41,540
Shift right click here.

83
00:06:41,970 --> 00:06:48,280
Or command window here. This terminal plus package of atom is having a problem of the moment.

84
00:06:48,540 --> 00:06:53,980
It has a bug so I'll be using a simple command line.

85
00:06:53,980 --> 00:06:59,410
Terminal plus is probably more convenient but this shouldn't be a problem. So Python 

86
00:07:00,120 --> 00:07:09,540
and the script name is website blocker. And yeah, one is being printed out as you see. Control C to interrupt the

87
00:07:09,570 --> 00:07:10,710
process.

88
00:07:11,130 --> 00:07:18,820
What I'm thinking about is to have some code here that checks the time every millisecond.

89
00:07:19,090 --> 00:07:24,200
So that would be something like East time between 8 a.m. and 4 p.m..

90
00:07:24,320 --> 00:07:31,590
If it, then keep these lines in this host file. If it's not then remove these lines from the host file.

91
00:07:31,830 --> 00:07:35,880
And that action would be performed I am saying every millisecond.

92
00:07:36,450 --> 00:07:39,580
But that's not a very smart thing to do.

93
00:07:39,600 --> 00:07:42,490
Why would you want your program to run every millisecond.

94
00:07:42,540 --> 00:07:45,480
That doesn't sound very memory efficient.

95
00:07:45,490 --> 00:07:49,320
So what we could do here is a simple trick.

96
00:07:49,350 --> 00:07:56,710
We import time and then here we say time sleep and let's say 5.

97
00:08:00,840 --> 00:08:07,990
This time we execute this and one we will be printed every 5 seconds.

98
00:08:08,140 --> 00:08:10,980
So that's it.

99
00:08:10,980 --> 00:08:20,070
Which means if we write now the code that accesses these lines here in this host file, that code will run every five

100
00:08:20,070 --> 00:08:22,280
seconds and not every millisecond.

101
00:08:22,500 --> 00:08:25,410
So that's enough accuracy for us.

102
00:08:25,410 --> 00:08:34,080
That means that if time is 4 pm and then it goes four and one second we will still have this line.

103
00:08:34,330 --> 00:08:34,520

104

00:08:34,530 --> 00:08:42,770
We will have this website blocked yet 4 pm plus one second so there will be a delay of 5 seconds which

105
00:08:42,770 --> 00:08:43,770
is not a problem.

106
00:08:43,970 --> 00:08:51,550
And you can also put this higher number so you can make it 5 minutes which should be 300 300 seconds

107
00:08:52,400 --> 00:08:54,890
but let me keep it 5. 5 is good.

108
00:08:55,170 --> 00:08:56,520
5 seconds.

109
00:08:56,870 --> 00:08:59,080
I think this is starting to make more sense for you.

110
00:08:59,080 --> 00:09:02,100
I would like to stop here. In the next lecture

111
00:09:02,100 --> 00:09:07,800
we will go ahead and write the code that writes these lines in this host file.

