Skip to content

Instantly share code, notes, and snippets.

@jxmorris12
Created August 4, 2019 21:50
Show Gist options
  • Save jxmorris12/324ce33c26c0a32906c9336069f8e8bd to your computer and use it in GitHub Desktop.
Save jxmorris12/324ce33c26c0a32906c9336069f8e8bd to your computer and use it in GitHub Desktop.
a script to force productivity during deep work time
#!/usr/local/bin/python3
#
# written 8/2/19 by jxm
#
# For some reason Chrome has its own DNS cache that makes websites
# still accessible sometimes after DNS is enabled. I considered adding
# code in this script to force-restart Chrome, but I'm going to forgo
# that for now until I find a better way to edit Chrome's settings
# to disable DNS caching. (Or just use Firefox...)
#
# I saved this as /usr/local/bin/deepwork on my machine.
#
import sys
NOT_ALLOWED_SITES = [
'facebook.com',
'twitter.com',
'reddit.com',
'17.249.124.38' # I think this is the IP for Mac Desktop iMessage
]
HOST_FILE = '/etc/hosts'
FILE_START_LINE = '###### START DEEPWORK ######'
FILE_STOP_LINE = '###### STOP DEEPWORK ######'
def _print_status(message):
print('STATUS: ' + message)
def _print_error(message):
print('ERROR: ' + message)
def _print_success(message):
print('SUCCESS: ' + message)
def _keyword_lines_found_in_host_file():
lines = open(HOST_FILE).readlines()
start_found = 0
stop_found = 0
for line in lines:
if FILE_START_LINE in line: start_found += 1
if FILE_STOP_LINE in line: stop_found += 1
return start_found, stop_found
def status():
start_found, stop_found = _keyword_lines_found_in_host_file()
if start_found == 0 and stop_found == 0:
_print_status('deepwork not running.')
elif start_found == 1 and stop_found == 1:
_print_status('deepwork running.') # @TODO print blocked hosts
else:
_print_status('deepwork corrupted file ' + HOST_FILE)
def start():
if not _keyword_lines_found_in_host_file() == (0,0):
_print_error('Cannot start; already running')
exit()
hosts_file = open(HOST_FILE, 'a+')
# Write header
hosts_file.write(FILE_START_LINE + '\n')
# Write blocked hosts
for site in NOT_ALLOWED_SITES:
hosts_file.write('127.0.0.1 {}\n'.format(site))
hosts_file.write('127.0.0.1 www.{}\n'.format(site))
# Write footer
hosts_file.write(FILE_STOP_LINE + '\n')
_print_success('begin deep work! [restart browser if DNS resolution still succeeds]')
# @TODO: Track deep work time
def stop():
if not _keyword_lines_found_in_host_file() == (1,1):
_print_error('Cannot stop; not running')
exit()
hosts_file = open(HOST_FILE, 'r')
keep_lines = True
lines = []
for line in hosts_file.readlines():
if FILE_START_LINE in line:
keep_lines = False
elif FILE_STOP_LINE in line:
keep_lines = True
elif keep_lines:
lines.append(line)
hosts_file = open(HOST_FILE, 'w')
for line in lines:
hosts_file.write(line)
if line[-1] != '\n':
hosts_file.write('\n')
hosts_file.close()
_print_success('deep work ended')
def main():
if not len(sys.argv) == 2:
print("ERROR: Call script like `deepwork <command>`")
exit()
command = sys.argv[1]
if command == 'status':
status()
elif command == 'start':
start()
elif command == 'stop':
stop()
else:
print("ERROR: Valid commands are 'help', 'stop', or 'status'")
exit()
if __name__ == '__main__': main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment