Skip to content

app; Improve Modularity #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
from datetime import datetime as dt
import os

def get_hosts_path():
if os.name == 'posix':
return "/etc/hosts"
elif os.name == 'nt':
return r"C:\Windows\System32\drivers\etc\hosts"
else:
raise OSError("Unsupported operating system.")


# Enter the site name which you want to block
sites_to_block = [
"www.facebook.com",
Expand All @@ -13,30 +22,24 @@
]

# different hosts for different os
Linux_host = "/etc/hosts"
Window_host = r"C:\Windows\System32\drivers\etc\hosts"
default_hoster = Linux_host # if you are on windows then change it to Window_host
default_hoster = get_hosts_path() #get the correct host path depending on if you're using linux or windows
redirect = "127.0.0.1"


if os.name == 'posix':
default_hoster = Linux_host

elif os.name == 'nt':
default_hoster = Window_host
else:
print("OS Unknown")
exit()
def is_work_time(start_hour, end_hour):
now = dt.now()
start = dt(now.year, now.month, now.day, start_hour)
end = dt(now.year, now.month, now.day, end_hour)

if start_hour < end_hour:
return start <= now < end
else:
return not (end <= now < start) # handles overnight shifts

def block_websites(start_hour, end_hour):
while True:
try:
if (
dt(dt.now().year, dt.now().month, dt.now().day, start_hour)
< dt.now()
< dt(dt.now().year, dt.now().month, dt.now().day, end_hour)
):
if is_work_time(start_hour, end_hour):
print("Do the work ....")
with open(default_hoster, "r+") as hostfile:
hosts = hostfile.read()
Expand All @@ -59,5 +62,6 @@ def block_websites(start_hour, end_hour):
break



if __name__ == "__main__":
block_websites(9, 21)