Tuesday, January 28, 2020

Project #3 - Website Blocker

For my third project I built a program that blocks selective websites during certain times of day. This is accomplished by having the program write to the hosts file. In my example below the website Facebook.com will not open during working hours, and will open on non-working hours.

import time
from datetime import datetime as dt

hosts_path = r"/filepath/hosts"redirect = "127.0.0.1"website_lists = ["www.Facebook.com", "Facebook.com"]

while True:
    if dt(dt.now().year, dt.now().month, dt.now().day, 8) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, 16):
        print("Can't access during working hours...")
        with open(hosts_path, 'r+') as file:
            content = file.read()
            for website in website_lists:
                if website in content:
                    pass                else:
                    file.write(redirect + " " + website + "\n")
    else:
        with open(hosts_path, 'r+') as file:
            content = file.readlines()
            file.seek(0)
            for line in content:
                if not any(website in line for website in website_lists):
                    file.write(line)
            file.truncate()
        print("Non-working hours, have fun...")
    time.sleep(5)

No comments:

Post a Comment

Project #8 - Stock Tracking Graph

For my 8th project, I made a program that pulls stock data from Yahoo Finance and displays it in a graph. For this example, I chose GOOG whi...