Skip to content

Code Quality Upgrade #15

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 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -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')
32 changes: 17 additions & 15 deletions app.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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:
Expand All @@ -60,4 +62,4 @@ def block_websites(start_hour, end_hour):


if __name__ == "__main__":
block_websites(9, 21)
block_websites(9, 21)
55 changes: 28 additions & 27 deletions app v2.py → app_v2.py
Original file line number Diff line number Diff line change
@@ -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)