diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml new file mode 100644 index 0000000..383e65c --- /dev/null +++ b/.github/workflows/pylint.yml @@ -0,0 +1,23 @@ +name: Pylint + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint + - name: Analysing the code with pylint + run: | + pylint $(git ls-files '*.py') diff --git a/app.py b/app.py index 6881234..e799d31 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,8 @@ +"""Module providing a function Blocking websites.""" import time from datetime import datetime as dt import os +import sys # Enter the site name which you want to block sites_to_block = [ @@ -13,38 +15,38 @@ ] # 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 -redirect = "127.0.0.1" +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 +REDIRECT = "127.0.0.1" if os.name == 'posix': - default_hoster = Linux_host + DEFAULT_HOSTER = LINUX_HOST elif os.name == 'nt': - default_hoster = Window_host + DEFAULT_HOSTER = WINDOW_HOST else: print("OS Unknown") - exit() - + sys.exit() def block_websites(start_hour, end_hour): + """Function that Block websites""" 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) + 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) ): print("Do the work ....") - with open(default_hoster, "r+") as hostfile: + with open(DEFAULT_HOSTER, "r+", encoding="utf-8") as hostfile: hosts = hostfile.read() for site in sites_to_block: if site not in hosts: - hostfile.write(redirect + " " + site + "\n") + hostfile.write(REDIRECT + " " + site + "\n") else: - with open(default_hoster, "r+") as hostfile: + with open(DEFAULT_HOSTER, "r+", encoding="utf-8") as hostfile: hosts = hostfile.readlines() hostfile.seek(0) for host in hosts: @@ -60,4 +62,4 @@ def block_websites(start_hour, end_hour): if __name__ == "__main__": - block_websites(9, 21) + block_websites(9, 21) \ No newline at end of file diff --git a/app v2.py b/app_v2.py similarity index 91% rename from app v2.py rename to app_v2.py index 7055aa3..2418c6f 100644 --- a/app v2.py +++ b/app_v2.py @@ -1,27 +1,28 @@ -import time -import ctypes - -# Add the websites that you want to block to this list -websites_to_block = ["www.facebook.com", "www.twitter.com", "www.instagram.com"] - -while True: - current_time = time.localtime() - # Set the time range in which the websites should be blocked - if current_time.tm_hour >= 9 and current_time.tm_hour < 18: - for website in websites_to_block: - # Use the Windows hosts file to block the website - # This will only work on Windows systems - ctypes.windll.wininet.InternetSetOptionW(0, 39, ctypes.c_void_p(0), 0) - with open(r"C:\Windows\System32\drivers\etc\hosts", "a") as file: - file.write(f"127.0.0.1 {website}\n") - else: - # Remove the website block if it's outside the set time range - with open(r"C:\Windows\System32\drivers\etc\hosts", "r+") as file: - lines = file.readlines() - file.seek(0) - for line in lines: - if not any(website in line for website in websites_to_block): - file.write(line) - file.truncate() - # Set the time interval at which to check the current time - time.sleep(60) +"""Module of website blocking version 2""" +import time +import ctypes + +# Add the websites that you want to block to this list +websites_to_block = ["www.facebook.com", "www.twitter.com", "www.instagram.com"] + +while True: + current_time = time.localtime() + # Set the time range in which the websites should be blocked + if current_time.tm_hour >= 9 and current_time.tm_hour < 18: + for website in websites_to_block: + # Use the Windows hosts file to block the website + # This will only work on Windows systems + ctypes.windll.wininet.InternetSetOptionW(0, 39, ctypes.c_void_p(0), 0) + with open(r"C:\Windows\System32\drivers\etc\hosts", "a", encoding="utf-8") as file: + file.write(f"127.0.0.1 {website}\n") + else: + # Remove the website block if it's outside the set time range + with open(r"C:\Windows\System32\drivers\etc\hosts", "r+", encoding="utf-8") as file: + lines = file.readlines() + file.seek(0) + for line in lines: + if not any(website in line for website in websites_to_block): + file.write(line) + file.truncate() + # Set the time interval at which to check the current time + time.sleep(60)