DEV Community

Cover image for Distractions, whose life is it anyway?
petercour
petercour

Posted on

Distractions, whose life is it anyway?

Some websites these days are designed to waste your time. You click and before you know it, you wasted an hour or more of your life.

You probably have better things to do.

So how do you get your life back?

Browser blocking

The most simple way is browser blocking. But that doesn't help, because as an experienced developer you have so many ways to quickly browse the web : chrome,firefox,falkon,safari,links,lynx.

Chances are you are using multiple computers at once. So that approach isn't effective.

Lets be honest: Browser block isn't effective for hardcore developer.

But you can try with leechblock, stayfocusd or tools like that

System blocking

One way is blocking those sites in /etc/hosts
So you can add a line like this

127.0.0.1  twitter.com
127.0.0.1  facebook.com
127.0.0.1  news.ycombinator.com
127.0.0.1  digg.com
127.0.0.1  stumbleupon.com
Enter fullscreen mode Exit fullscreen mode

That would block the site. This works fine in an office computer, but what if you're working from home?

Firefox DNS caching

Now if you use Firefox, it has it's own DNS cache expiriation. Open about:config, search for network.dnsCacheExpiration and set it to 0. Then restart Firefox.

Python to the rescue

Can write a Python script that automatically blocks at certain hours.
Something like this (I haven't extensively tested this yet):

#!/usr/bin/python3
import time
from datetime import datetime as dt
from datetime import datetime

#Path to the host file, redirect to local host, list of websits to block
host_path = "/etc/hosts"
redirect = "127.0.0.1"
website_list = ["www.netflix.com","www.facebook.com", "reddit.com","old.reddit.com","twitter.com","news.ycombinator.com","nu.nl"]


def is_between(time, time_range):
    if time_range[1] < time_range[0]:
        return time >= time_range[0] or time <= time_range[1]
    return time_range[0] <= time <= time_range[1]

while True:
    #Check for the current time

    what_time_is_it = datetime.now().strftime('%H:%M')
    print(what_time_is_it)
    if not is_between(what_time_is_it, ("09:00", "17:00")):
        print("Allowed")
        file = open(host_path,'w+')
        file.seek(0)
        file.truncate()        
    else:
        print("Work time now")
        file = open(host_path,'w+')
        file.seek(0)
        file.truncate()
        for website in website_list:
            print('block ' + website)
            line = redirect + "  " + website + "\n"
            file.write(line)

    time.sleep(5)
Enter fullscreen mode Exit fullscreen mode

Now this could run in the background. As a tech geek, you can just use pkill to close Python. So this also wouldn't be perfect.

Related links:

Top comments (0)