Ads Blocker with Python


Aren’t we all tired of random pop-ups during site surfing? So, we can create website blockers for restraining pushy ads by creating this Python project.

Remember, when you code this, you can add the sites you need to block by editing sites_to_block, change the host, or edit the time when you need to block the site 

Source Code:

import time
from datetime import datetime as dt

sites_to_block = [
    "www.linkdin.com",
    "facebook.com",
    "www.youtube.com",
    "youtube.com",
    "www.google.com",
    "google.com",
]

Linux_host = "/etc/hosts"
Window_host = r"C:\Windows\System32\drivers\etc\hosts"
default_hoster = Linux_host
redirect = "127.0.0.1"
print("CodeWithAditya")

def block_websites(start_hour, end_hour):
    while True:
        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)
        ):
            print("Do the work ....")
            with open(default_hoster, "r+") as hostfile:
                hosts = hostfile.read()
                for site in sites_to_block:
                    if site not in hosts:
                        hostfile.write(redirect + " " + site + "\n")
        else:
            with open(default_hoster, "r+") as hostfile:
                hosts = hostfile.readlines()
                hostfile.seek(0)
                for host in hosts:
                    if not any(site in host for site in sites_to_block):
                        hostfile.write(host)
                hostfile.truncate()
            print("Good Time")
        time.sleep(3)


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

Comments

Popular posts from this blog

Python Tic Tac Toe

Just A Really Very Intelligent System with Python