Distractions, whose life is it anyway?
Tags: linux, productivity, python
Published: 2019-07-12 22:34:36

---
title: Distractions, whose life is it anyway?
published: true
description: productivity, linux 
tags: linux, productivity, python
cover_image: https://cdn.pixabay.com/photo/2016/09/01/13/52/steampunk-1636156_960_720.png
---

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.

![](https://thepracticaldev.s3.amazonaws.com/i/jakeaw826xanheb6l59h.jpeg)

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

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.

<img src="https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F55xNG9gNcRnd6%2Fgiphy.gif&f=1">


### 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)
        

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:
* <a href="https://humanetech.com/">Humanetech</a>
* <a href="https://techcrunch.com/2017/07/30/the-attention-economy-created-by-silicon-valley-is-bankrupting-us/">the attention economy is bankrupting us</a>
* <a href="https://pythonbasics.org/">Python tutorial</a>
* <a href="https://pythonbasics.org/time-and-date/">Time and date</a>