Created
September 26, 2018 16:33
-
-
Save josifoski/3d1406bba37694e19fce9d1e032bcb99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Script for planing /etc/hosts to disable time wasting sites (like lichess.org or youtube for me) during some time period in day | |
# Created: 2018 September 26 | |
# Author: Aleksandar Josifoski https://about.me/josifsk | |
# how to start python script with sudo without asking password for specific script | |
# https://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password | |
# my crontab settings (set to check script on every 30min) | |
# 0,30 * * * * sudo /data/bash_scripts/hosts_time.py | |
# program assumes that | |
#0.0.0.1 lichess.org | |
#0.0.0.1 www.lichess.org | |
#0.0.0.1 youtube.com | |
#0.0.0.1 www.youtube.com | |
# are in 4.th - 7.th line in /etc/hosts file where first line count is 1 | |
import datetime | |
import sys | |
import fileinput | |
import codecs | |
# following dictionary will enable lichess.org in time intervals 5:30-59, 14:00-29, 20:00-29 | |
dlichess_time_enabled = {5: range(30, 60), 14: range(30), 20: range(30)} | |
dyoutube_time_enabled = {20: range(60)} | |
ldicts = [dlichess_time_enabled, dyoutube_time_enabled] | |
observed_lines = [4, 5, 6, 7] | |
def get_state(linenums): | |
lret = [] | |
with codecs.open('/etc/hosts', 'r', 'utf8') as fs: | |
lhostslines = fs.readlines() | |
for linenum in linenums: | |
if lhostslines[linenum - 1].strip()[0] == '#': | |
lret.append(True) | |
else: | |
lret.append(False) | |
return lret | |
def disable_site(linenum): | |
for i, line in enumerate(fileinput.input('/etc/hosts', inplace=1), start = 0): | |
if i == linenum - 1: | |
sys.stdout.write(line.lstrip('#')) | |
else: | |
sys.stdout.write(line) | |
def enable_site(linenum): | |
for i, line in enumerate(fileinput.input('/etc/hosts', inplace=1), start = 0): | |
if i == linenum - 1: | |
towrite = '#' + line.lstrip('#') | |
sys.stdout.write(towrite) | |
else: | |
sys.stdout.write(line) | |
prevstates = get_state(observed_lines) | |
#print(prevstates) | |
now = str(datetime.datetime.now()) | |
#print(now) | |
h = now.split()[1].split(':')[0] | |
m = now.split()[1].split(':')[1] | |
if h == '00': # this is for using bellow lstrip('0'). Hmm, that should be time for sleeping. | |
h = '1' | |
if m == '00': | |
m = '1' | |
#print(h, m) | |
linesstate = [False] * len(observed_lines) | |
j = -2 | |
for d in ldicts: | |
j += 2 | |
for key in d.keys(): | |
if int(h.lstrip('0')) == key: | |
if int(m.lstrip('0')) in d[key]: | |
linesstate[j] = True | |
linesstate[j+1] = True | |
break | |
for j in range(len(observed_lines)): | |
if prevstates[j] != linesstate[j]: | |
if linesstate[j]: | |
enable_site(observed_lines[j]) | |
else: | |
disable_site(observed_lines[j]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems that for youtube is bit more complex
https://gist.github.com/marshymarsh/5c34b5a6530d46fa0f3076bed52d293a