Great what we have so far is a script that checks if the current time is between these two times and then it
appends these four lines in the hosts file.
So we're talking about a appending because these R plus mode there means that you can read an append
text in your file.
And you know that through writing defers from a appending in that when you write a text in a file you are writing
the text in that empty version of the file.
So with write you'd have only these four lines in your hosts file and not the other ones. Good, that
makes sense.
And that's the first part of our conditional.
And if that happens to be false then we want to execute something here.
That would be to delete these four lines.
Now as you know from all the previous lectures of file handling in this course there's no direct method
to just pass some lines as arguments or as input and then have a delete function that removes those
lines from the file and that makes the situation a bit tricky.
So our input here is this list with these names with these items which means what we can do is well
first logically you want to read this file and check if these names are in here again.
Actually as we did in here more or less.
So let me start with that.
Open hosts.
Temp. That's a file path.
And again here we are reading and appending at the same time.
So we want to use our R plus there. As file.
Let's store this text in
a variable. Good.
And yeah, this will load the text in our content variable as a single string so.
That's probably not the best solution.
So the best solution would be to use the read lies instead which will produce a list of all
the lines in our hosts file.
So for instance this has twenty five lines and will have a list with 25 items where each item represents
a string with each of these lines.
So for instance this would be a first string of the list stored in the content of the variable.
So content is now a list. Now that we have a list, we can start checking these items against
the list that we have grabbed from hosts file.
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.
We have methods to write content.
So we have methods to append content in that file but not to delete.
However we can make use of the append method.
We could start iterating through these items in the content list and check if Facebook is in that
particular item.
So the items string if Facebook is there then we ignore that line.
And if Facebook is not there we write that line the hosts file.
So something like this.
Let's say we iterate through this and say Facebook is not there.
So create a hosts file like a new hosts file with this line there and then go to the next line.
If Facebook is not there then add, append this line to that hosts file so we will have two lines and then three lines
and then we will remove the lines which contain Facebook.
I know this sounds very confusing at the moment but once I write the script and I explain line
by line,
I'm sure you'll have that moment when you say Aha.
So you'll get that
Once I write the lines here.
So we're here, we read the lines but as you know when you apply a read method like read
lines or read the pointer will be placed at the end of the last line.
So here. So we read the lines there, we have a list.
Now we can say for
line in content. So we start looping through the content list and here I'd use use any method.
So we want to check these items against the lines of content that would be something like if not any
website in line for website in website list
file.write so append the line.
So what I did here is let me simplify that by talking about the first iteration only.
So we have this line here the first line copyright etcetera.
And we are saying that if not any websites so any item which is still a variable in that line.
So in this line and so what is this a website variable that well where the website is the first item of the
website list which should be based on here but this will create a loop so it will check the first item
there and it will compare it.
So against the first line.
So we are in the first loop and we say if not www.Facebook.com
in that first line there.
Then write that line in the file.
Otherwise we wouldn't do anything.
So that would
check the first item of the list and then go to the second item and check it against the first item
of the hosts file.
The first line of hosts file and then it would go to the third line and fourth line until this list is consumed.
So that's for the first line of the hosts file. Than it goes to the second line and performs exactly
the same thing.
So it checks the four items of the website list again.
Against the second line of the hosts file. I hope that makes sense.
And I'll check what this does.
But now it's 20 past four.
So we are in the working hours.
And let me modify this. Let's say 16 to 17 are working hours.
So outside of this we want to move these four lines which will be the case.
Even when I write the script now.
Python website blocker. Run fun hours
is executed and two times.
I'll interrupt this and check what we got.
So we have this block here. We have this block here and then the block is repeated.
So it's not exactly what we want.
And then this ends.
And then it repeats again and it ends in here then it repeats another time so the loop, the while loop run
three times. And the first block,
so this one here was the block that we had already in the hosts file and then what happened is
that we opened this hosts file in read and append mode and then iterated through the lines of the hosts
file and then we said append only those lines where there's no Facebook or this mail.live
in those lines. And Python did just that so it appended those lines under the existing
content and it did it two times.
Yes.
Sorry, I set the loop around three times earlier actually the loop the loop ran two times but we had one
existing block here.
So the other two times Python appended
this block again. Great. Even though we are not there yet, I'm satisfied with the progress because I think you
are now able to understand how things are working.
And now we need to put our logic into a very active state in order to make this work.
So there are no new things that you need to apply here.
It's just tricks.
So you know about this truncate method which is what it does is it truncates it deletes the content
of the file from the current point and downwards.
So for instance if we apply that in here
what we would get is
So think about the first iteration. So the first iteration will add the first block after the existing
block which is this one here.
So this is the first block with all of the Facebook things. The truncate method what it would do,
it will delete everything that is under this, which is not exactly what we want
because we would still have these things above.
So we need to figure out a way to put this block of code above this, above the existing one and to solve
that is by using file.seek.
So you know that when you read it when you apply or methods to your file for instance here you apply
the first read method and the pointer will go here.
So just after the last character of the file content.
So when you apply a write method and you append lines, the lines will append after the pointer.
Now if you want to append these lines as I said if you want to append them before this existing block
you'd want to apply the seek method so that you replace the pointer.
You replace the pointer just before the first character of the file content.
What that will do is it will get all these lines.
So cut it from there just to illustrate and it will replace them in here and there what the file truncate method
will do is it will remove everything that is after that.
And this as well.
So we will end up with this text which is what we want.
Good. I hope I explained that well and let me try that now.
So it will delete these and these.
This is the initial state of the hosts file.
So we have these Facebook and Hotmail domains there.
So I'll save this.
And the script as well and I'll go ahead and execute.
So it seems to be working well.
Yes.
And if I change the hour so let me
Alright, I'll do a soft ware there. Ok. So that you see both the hosts file and the script.
So now it's three to four.
And let me put this to 15.
So we are in the working hours. Execute this. And the lines are there. Interrupt that. Put this 16. Try again.
And the lines are gone.
So this is working. Again just to wrap up what we did here is
so if we're talking about the else section and we opened the hosts file in read and append method.
And then we stored the lines in content list and that puts a pointer in here which means that everything
you do after that will be applied under these lines.
But by applying the seek method there we tell Python to put the pointer in here just here.
And so then we iterate through this content list in this line and check that if any of these website
list contents is in the current line of the content list
Actually if it is not in the current line of content list, then we write down that line in the hosts file.
And we truncate everything that goes under that line which means we wrote all these twenty one
lines and everything that we had down here, so which was th previous existing hosts text that content
was truncated.
So it was deleted from the file.
And yes we're basically done.
I mean we still have some things to make this work to be able to run this script as a Python
process
on the background because at the moment the way we run this is by opening a command line like calling
Python and then pointing to website blocker.py.
And also we need to implement one thing.
So at the moment we are accessing this fake hosts file but what we want to do instead is access the
actual hosts file which is here.
So hosts and to do that you'd first want to change host temp with hosts path.
So you hosts temp with hosts path.
So where do we have hosts temp. Hosts.
Yeah it's here.
Hosts path there and here as well.
Now that's about it.
But if you try this
you get a permission error because you're runing that as a normal user.
What you want to do instead is run that as an administrator or on Linux or Mac that is straightforward.
So you'd want to say sudo.
And then Python and then point to website blocker.py.
However sudo doesn't work with Windows.
So the work around here is to open the command line from the terminal and right click and go run as
administrator.
Yes.
And then you'd have to cd for change directory and then
Slash d which means I'm going to the D drive.
So when you switch drives from C to D you want to use this backslash D.
Dropbox.
Block websites.
And demo.
And if I run the script now.
Python and website blocker, that runs normaly.
So we are during the fun hours.
It's 15 16 past 6.
So we are in the fun hours.
and if you check the script, the hosts file you'll see that you don't have any web sites
there being blocked.
So let me try this.
Let me change this to 19 so that will fall inside the working hours and run rather script again so we
get the working hours there.
And if I go and check the Notepad now it is saying this file has been modified by another program
which is Python.
And do you want to reload it.
Yes.
We have these four lines there.
If I try Facebook now I won't be able to access it.
But be aware though that sometimes if you have your browser open and you will you just block Facebook
or another website in your hosts file, the browser may not reflect it immediately.
So it may take some time ntil the browser can get the information from the hosts file.
If you want to speed up the process you may want to restart your browser so close it and open up again
and then check Facebook.
And yet it should be the same for Hotmail as well.
So it passes Hotmail but then Hotmail layer servers direct you to this Web site which I'm blocking and
you cannot access that either.
So this is working well we're doing great.
And yeah, what's left now is to implement this program to run it in the background.
So that's just a few minutes of video left for this section
And we're done, so talk to you in the next lecture.