1
00:00:01,440 --> 00:00:08,950
Great what we have so far is a script that checks if the current time is between these two times and then it

2
00:00:08,950 --> 00:00:13,170
appends these four lines in the hosts file.

3
00:00:13,860 --> 00:00:20,820
So we're talking about a appending because these R plus mode there means that you can read an append

4
00:00:20,910 --> 00:00:22,020
text in your file.

5
00:00:22,080 --> 00:00:28,440
And you know that through writing defers from a appending in that when you write a text in a file you are writing

6
00:00:28,440 --> 00:00:31,760
the text in that empty version of the file.

7
00:00:31,800 --> 00:00:39,420
So with write you'd have only these four lines in your hosts file and not the other ones. Good, that

8
00:00:39,420 --> 00:00:40,280
makes sense.

9
00:00:40,440 --> 00:00:44,740
And that's the first part of our conditional.

10
00:00:44,940 --> 00:00:50,280
And if that happens to be false then we want to execute something here.

11
00:00:50,400 --> 00:00:55,070
That would be to delete these four lines.

12
00:00:55,950 --> 00:01:02,880
Now as you know from all the previous lectures of file handling in this course there's no direct method

13
00:01:02,970 --> 00:01:12,300
to just pass some lines as arguments or as input and then have a delete function that removes those

14
00:01:12,300 --> 00:01:16,950
lines from the file and that makes the situation a bit tricky.

15
00:01:17,040 --> 00:01:27,110
So our input here is this list with these names with these items which means what we can do is well

16
00:01:27,120 --> 00:01:34,240
first logically you want to read this file and check if these names are in here again.

17
00:01:34,530 --> 00:01:37,370
Actually as we did in here more or less.

18
00:01:37,630 --> 00:01:41,060
So let me start with that.

19
00:01:41,940 --> 00:01:44,990
Open hosts.

20
00:01:45,160 --> 00:01:47,150
Temp. That's a file path.

21
00:01:47,370 --> 00:01:52,170
And again here we are reading and appending at the same time.

22
00:01:52,230 --> 00:01:57,240
So we want to use our R plus there. As file.

23
00:01:58,470 --> 00:02:02,300
Let's store this text in

24
00:02:02,310 --> 00:02:06,510
a variable. Good.

25
00:02:06,660 --> 00:02:13,490
And yeah, this will load the text in our content variable as a single string so.

26
00:02:13,810 --> 00:02:16,530
That's probably not the best solution.

27
00:02:16,890 --> 00:02:23,610
So the best solution would be to use the read lies instead which will produce a list of all

28
00:02:23,610 --> 00:02:25,510
the lines in our hosts file.

29
00:02:25,650 --> 00:02:33,420
So for instance this has twenty five lines and will have a list with 25 items where each item represents

30
00:02:33,830 --> 00:02:37,650
a string with each of these lines.

31
00:02:37,650 --> 00:02:44,090
So for instance this would be a first string of the list stored in the content of the variable.

32
00:02:44,160 --> 00:02:51,150
So content is now a list. Now that we have a list, we can start checking these items against

33
00:02:51,150 --> 00:02:55,330
the list that we have grabbed from hosts file.

34
00:02:55,470 --> 00:03:03,730
So if these items are there we want to delete them but as I said there is not a direct way to delete these items.

35
00:03:03,810 --> 00:03:08,480
We have methods to write content.

36
00:03:08,510 --> 00:03:14,340
So we have methods to append content in that file but not to delete.

37
00:03:14,610 --> 00:03:18,670
However we can make use of the append method.

38
00:03:19,230 --> 00:03:28,680
We could start iterating through these items in the content list and check if Facebook is in that

39
00:03:28,680 --> 00:03:29,780
particular item.

40
00:03:29,790 --> 00:03:35,590
So the items string if Facebook is there then we ignore that line.

41
00:03:35,910 --> 00:03:40,910
And if Facebook is not there we write that line the hosts file.

42
00:03:41,190 --> 00:03:42,930
So something like this.

43
00:03:42,990 --> 00:03:47,200
Let's say we iterate through this and say Facebook is not there.

44
00:03:47,220 --> 00:03:53,970
So create a hosts file like a new hosts file with this line there and then go to the next line.

45
00:03:53,970 --> 00:04:00,900
If Facebook is not there then add, append this line to that hosts file so we will have two lines and then three lines

46
00:04:01,660 --> 00:04:05,160
and then we will remove the lines which contain Facebook.

47
00:04:05,160 --> 00:04:11,650
I know this sounds very confusing at the moment but once I write the script and I explain line

48
00:04:11,660 --> 00:04:12,310
by line,

49
00:04:12,480 --> 00:04:15,660
I'm sure you'll have that moment when you say Aha.

50
00:04:16,050 --> 00:04:17,490
So you'll get that

51
00:04:17,690 --> 00:04:19,690
Once I write the lines here.

52
00:04:20,190 --> 00:04:27,210
So we're here, we read the lines but as you know when you apply a read method like read

53
00:04:27,210 --> 00:04:32,990
lines or read the pointer will be placed at the end of the last line.

54
00:04:33,000 --> 00:04:38,420
So here. So we read the lines there, we have a list.

55
00:04:38,520 --> 00:04:40,170
Now we can say for

56
00:04:42,820 --> 00:04:53,310
line in content. So we start looping through the content list and here I'd use use any method.

57
00:04:53,520 --> 00:05:02,480
So we want to check these items against the lines of content that would be something like if not any

58
00:05:04,880 --> 00:05:11,720
website in line for website in website list

59
00:05:16,100 --> 00:05:22,160
file.write so append the line.

60
00:05:22,160 --> 00:05:27,580
So what I did here is let me simplify that by talking about the first iteration only.

61
00:05:27,830 --> 00:05:32,450
So we have this line here the first line copyright etcetera.

62
00:05:32,930 --> 00:05:44,100
And we are saying that if not any websites so any item which is still a variable in that line.

63
00:05:44,120 --> 00:05:52,400
So in this line and so what is this a website variable that well where the website is the first item of the

64
00:05:52,400 --> 00:06:02,660
website list which should be based on here but this will create a loop so it will check the first item

65
00:06:02,660 --> 00:06:05,990
there and it will compare it.

66
00:06:05,990 --> 00:06:09,030
So against the first line.

67
00:06:09,410 --> 00:06:17,920
So we are in the first loop and we say if not www.Facebook.com

68
00:06:17,930 --> 00:06:19,380
in that first line there.

69
00:06:19,650 --> 00:06:22,900
Then write that line in the file.

70
00:06:22,940 --> 00:06:25,290
Otherwise we wouldn't do anything.

71
00:06:25,430 --> 00:06:26,250
So that would

72
00:06:26,270 --> 00:06:33,770
check the first item of the list and then go to the second item and check it against the first item

73
00:06:33,830 --> 00:06:35,930
of the hosts file.

74
00:06:36,160 --> 00:06:43,370
The first line of hosts file and then it would go to the third line and fourth line until this list is consumed.

75
00:06:43,390 --> 00:06:43,670

76
00:06:44,000 --> 00:06:51,830
So that's  for the first line of the hosts file. Than it goes to the second line and performs exactly

77
00:06:51,830 --> 00:06:53,770
the same thing.

78
00:06:53,960 --> 00:06:58,300
So it checks the four items of the website list again.

79
00:06:58,490 --> 00:07:04,770
Against the second line of the hosts file. I hope that makes sense.

80
00:07:04,870 --> 00:07:07,410
And I'll check what this does.

81
00:07:07,760 --> 00:07:12,150
But now it's 20 past four.

82
00:07:12,260 --> 00:07:14,340
So we are in the working hours.

83
00:07:14,500 --> 00:07:22,900
And let me modify this. Let's say 16 to 17 are working hours.

84
00:07:23,090 --> 00:07:28,990
So outside of this we want to move these four lines which will be the case.

85
00:07:29,080 --> 00:07:32,240
Even when I write the script now.

86
00:07:32,400 --> 00:07:35,610
Python website blocker. Run fun hours

87
00:07:35,630 --> 00:07:40,770
is executed and two times.

88
00:07:40,990 --> 00:07:44,780
I'll interrupt this and check what we got.

89
00:07:44,780 --> 00:07:57,790
So we have this block here. We have this block here and then the block is repeated.

90
00:07:58,220 --> 00:08:02,690
So it's not exactly what we want.

91
00:08:02,810 --> 00:08:03,700
And then this ends.
 
00:08:03,740 --> 00:08:14,510
And then it repeats again and it ends in here then it repeats another time so the loop, the while loop run

92
00:08:14,510 --> 00:08:18,720
three times. And the first block,

93
00:08:18,830 --> 00:08:26,870
so this one here was the block that we had already in the hosts file and then what happened is

94
00:08:26,870 --> 00:08:33,860
that we opened this hosts file in read and append mode and then iterated through the lines of the hosts

95
00:08:33,860 --> 00:08:42,740
file and then we said append only those lines where there's no Facebook or this mail.live

96
00:08:42,780 --> 00:08:50,420
in those lines. And Python did just that so it appended those lines under the existing

97
00:08:50,420 --> 00:08:53,580
content and it did it two times.

98
00:08:53,900 --> 00:08:54,330
Yes.

99
00:08:54,500 --> 00:09:02,360
Sorry, I set the loop around three times earlier actually the loop the loop ran two times but we had one

100
00:09:02,360 --> 00:09:03,690
existing block here.

101
00:09:03,980 --> 00:09:07,510
So the other two times Python appended

102
00:09:08,220 --> 00:09:16,560
this block again. Great. Even though we are not there yet, I'm satisfied with the progress because I think you

103
00:09:16,610 --> 00:09:19,840
are now able to understand how things are working.

104
00:09:20,240 --> 00:09:27,560
And now we need to put our logic into a very active state in order to make this work.

105
00:09:27,590 --> 00:09:30,820
So there are no new things that you need to apply here.

106
00:09:30,890 --> 00:09:32,700
It's just tricks.

107
00:09:32,870 --> 00:09:41,600
So you know about this truncate method which is what it does is it truncates it deletes the content

108
00:09:41,600 --> 00:09:47,010
of the file from the current point and downwards.

109
00:09:47,030 --> 00:09:50,950
So for instance if we apply that in here

110
00:09:53,910 --> 00:09:56,760
what we would get is�

111
00:09:57,160 --> 00:10:07,130
So think about the first iteration. So the first iteration will add the first block after the existing

112
00:10:07,130 --> 00:10:10,490
block which is this one here.

113
00:10:11,740 --> 00:10:18,300
So this is the first block with all of the Facebook things. The truncate method what it would do,

114
00:10:18,330 --> 00:10:26,310
it will delete everything that is under this, which is not exactly what we want

115
00:10:26,320 --> 00:10:31,760
because we would still have these things above.

116
00:10:31,780 --> 00:10:42,330
So we need to figure out a way to put this block of code above this, above the existing one and to solve

117
00:10:42,340 --> 00:10:47,360
that is by using file.seek.

118
00:10:47,830 --> 00:10:53,680
So you know that when you read it when you apply or methods to your file for instance here you apply

119
00:10:53,680 --> 00:10:59,220
the first read method and the pointer will go here.

120
00:10:59,250 --> 00:11:03,320
So just after the last character of the file content.

121
00:11:03,340 --> 00:11:10,630
So when you apply a write method and you append lines, the lines will append after the pointer.

122
00:11:10,670 --> 00:11:11,860

123
00:11:11,870 --> 00:11:18,460
Now if you want to append these lines as I said if you want to append them before this existing block

124
00:11:18,940 --> 00:11:24,650
you'd want to apply the seek method so that you replace the pointer.

125
00:11:24,670 --> 00:11:30,480
You replace the pointer just before the first character of the file content.

126
00:11:30,880 --> 00:11:37,480
What that will do is it will get all these lines.

127
00:11:37,600 --> 00:11:46,750
So cut it from there just to illustrate and it will replace them in here and there what the file truncate method

128
00:11:46,750 --> 00:11:52,170
will do is it will remove everything that is after that.

129
00:11:52,960 --> 00:11:54,070
And this as well.

130
00:11:54,150 --> 00:12:00,110
So we will end up with this text which is what we want.

131
00:12:00,460 --> 00:12:06,210
Good. I hope I explained that well and let me try that now.

132
00:12:07,210 --> 00:12:13,250
So it will delete these and these.

133
00:12:14,320 --> 00:12:17,600
This is the initial state of the hosts file.

134
00:12:17,680 --> 00:12:21,710
So we have these Facebook and Hotmail domains there.

135
00:12:21,970 --> 00:12:23,340
So I'll save this.

136
00:12:23,800 --> 00:12:34,180
And the script as well and I'll go ahead and execute.

137
00:12:34,600 --> 00:12:37,250
So it seems to be working well.

138
00:12:38,240 --> 00:12:39,860
Yes.

139
00:12:41,490 --> 00:12:44,980
And if I change the hour so let me�

140
00:12:48,190 --> 00:12:58,610
Alright, I'll do a soft ware there. Ok. So that you see both the hosts file and the script.

141
00:12:58,690 --> 00:13:01,160
So now it's three to four.

142
00:13:01,150 --> 00:13:05,120
And let me put this to 15.

143
00:13:05,230 --> 00:13:16,920
So we are in the working hours. Execute this. And the lines are there. Interrupt that. Put this 16. Try again.

144
00:13:17,080 --> 00:13:17,510

145
00:13:17,620 --> 00:13:20,530
And the lines are gone.

146
00:13:20,620 --> 00:13:27,330
So this is working. Again just to wrap up what we did here is

147
00:13:27,340 --> 00:13:34,660
so if we're talking about the else section and we opened the hosts file in read and append method.

148
00:13:34,670 --> 00:13:35,070

149
00:13:35,620 --> 00:13:43,720
And then we stored the lines in content list and that puts a pointer in here which means that everything

150
00:13:43,720 --> 00:13:47,140
you do after that will be applied under these lines.

151
00:13:47,530 --> 00:13:55,550
But by applying the seek method there we tell Python to put the pointer in here just here.

152
00:13:55,630 --> 00:14:04,950
And so then we iterate through this content list in this line and check that if any of these website

153
00:14:04,990 --> 00:14:09,840
list contents is in the current line of the content list�

154
00:14:09,900 --> 00:14:17,680
Actually if it is not in the current line of content list, then we write down that line in the hosts file.

155
00:14:17,690 --> 00:14:26,170
And we truncate everything that goes under that line which means we wrote all these twenty one

156
00:14:26,470 --> 00:14:33,840
lines and everything that we had down here, so which was th previous existing hosts text that content 

157
00:14:34,060 --> 00:14:35,400
was truncated.

158
00:14:35,400 --> 00:14:37,820
So it was deleted from the file.

159
00:14:38,020 --> 00:14:41,080
And yes we're basically done.

160
00:14:41,390 --> 00:14:47,710
I mean we still have some things to make this work to be able to run this script as a Python

161
00:14:47,710 --> 00:14:48,240
process

162
00:14:48,250 --> 00:14:55,970
on the background because at the moment the way we run this is by opening a command line like calling

163
00:14:55,980 --> 00:15:00,040
Python and then pointing to website blocker.py.

164
00:15:01,030 --> 00:15:03,780
And also we need to implement one thing.

165
00:15:03,790 --> 00:15:12,090
So at the moment we are accessing this fake hosts file but what we want to do instead is access the

166
00:15:12,100 --> 00:15:16,570
actual hosts file which is here.

167
00:15:16,640 --> 00:15:25,030
So hosts and to do that you'd first want to change host temp with hosts path.

168
00:15:25,330 --> 00:15:27,740
So you hosts temp with hosts path.

169
00:15:27,850 --> 00:15:32,530
So where do we have hosts temp. Hosts.

170
00:15:32,710 --> 00:15:34,560
Yeah it's here.

171
00:15:35,860 --> 00:15:40,790
Hosts path there and here as well.

172
00:15:43,170 --> 00:15:44,350
Now that's about it.

173
00:15:44,380 --> 00:15:47,200
But if you try this

174
00:15:50,830 --> 00:15:55,660
you get a permission error because you're runing that as a normal user.

175
00:15:55,720 --> 00:16:03,260
What you want to do instead is run that as an administrator or on Linux or Mac that is straightforward.

176
00:16:03,310 --> 00:16:03,370

177
00:16:03,370 --> 00:16:05,430
So you'd want to say sudo.

178
00:16:05,500 --> 00:16:08,670
And then Python and then point to website blocker.py.

179
00:16:08,910 --> 00:16:12,400
However sudo doesn't work with Windows.

180
00:16:12,520 --> 00:16:20,650
So the work around here is to open the command line from the terminal and right click and go run as

181
00:16:20,640 --> 00:16:21,910
administrator.

182
00:16:22,380 --> 00:16:22,990
Yes.

183
00:16:23,020 --> 00:16:27,610
And then you'd have to cd for change directory and then

184
00:16:30,580 --> 00:16:35,010
Slash d  which means I'm going to the D drive.

185
00:16:35,020 --> 00:16:39,600
So when you switch drives from C to D you want to use this backslash D.

186
00:16:40,290 --> 00:16:42,290
Dropbox.

187
00:16:48,650 --> 00:16:49,550
Block websites.

188
00:16:49,610 --> 00:16:50,300
And demo.

189
00:16:54,520 --> 00:16:55,840
And if I run the script now.

190
00:16:55,840 --> 00:17:01,950
Python and website blocker, that runs normaly.

191
00:17:02,860 --> 00:17:04,610
So we are during the fun hours.

192
00:17:04,620 --> 00:17:09,390
It's 15 16 past 6.

193
00:17:09,390 --> 00:17:12,570
So we are in the fun hours.

194
00:17:15,260 --> 00:17:22,330
and if you check the script, the hosts file you'll see that you don't have any web sites

195
00:17:22,340 --> 00:17:25,370
there being blocked.

196
00:17:25,520 --> 00:17:27,870
So let me try this.

197
00:17:27,880 --> 00:17:34,840
Let me change this to 19 so that will fall inside the working hours and run rather script again so we

198
00:17:34,840 --> 00:17:36,350
get the working hours there.

199
00:17:36,590 --> 00:17:43,450
And if I go and check the Notepad now it is saying this file has been modified by another program

200
00:17:44,200 --> 00:17:45,300
which is Python.

201
00:17:45,340 --> 00:17:46,610
And do you want to reload it.

202
00:17:46,680 --> 00:17:49,230
Yes.

203
00:17:49,300 --> 00:17:51,680
We have these four lines there.

204
00:17:52,790 --> 00:17:58,270
If I try Facebook now I won't be able to access it.

205
00:17:59,130 --> 00:18:06,220
But be aware though that sometimes if you have your browser open and you will you just block Facebook

206
00:18:06,220 --> 00:18:10,770
or another website in your hosts file, the browser may not reflect it immediately.

207
00:18:10,920 --> 00:18:16,890
So it may take some time ntil the browser can get the information from the hosts file.

208
00:18:17,380 --> 00:18:22,920
If you want to speed up the process you may want to restart your browser so close it and open up again

209
00:18:23,390 --> 00:18:26,260
and then check Facebook.

210
00:18:26,520 --> 00:18:30,370
And yet it should be the same for Hotmail as well.

211
00:18:30,370 --> 00:18:38,170
So it passes Hotmail but then Hotmail layer servers direct you to this Web site which I'm blocking and

212
00:18:38,160 --> 00:18:39,840
you cannot access that either.

213
00:18:40,200 --> 00:18:42,930
So this is working well we're doing great.

214
00:18:43,060 --> 00:18:48,300
And yeah, what's left now is to implement this program to run it in the background.

215
00:18:48,490 --> 00:18:52,910
So that's just a few minutes of video left for this section

216
00:18:53,030 --> 00:18:55,190
And we're done, so talk to you in the next lecture.

