Created
January 23, 2012 19:37
-
-
Save ryane/1665132 to your computer and use it in GitHub Desktop.
(un)Block distracting sites in hosts file via quick and dirty script. Inspired by http://al3x.net/2009/09/14/my-get-back-to-work-hack.html
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 ruby | |
## you will probably need to run this via sudo | |
## sudo ruby focus.rb [on|off] | |
# inspired by http://al3x.net/2009/09/14/my-get-back-to-work-hack.html | |
require 'fileutils' | |
require 'tempfile' | |
class Focus | |
HOSTS_FILE = "/etc/hosts" | |
HOSTS_FILE_BACKUP = "/etc/hosts.bak" | |
DISTRACTIONS_START = "#begin_distractions" | |
DISTRACTIONS_END = "#end_distractions" | |
DISTRACTION_HOSTS = [ | |
"news.ycombinator.com", | |
"twitter.com www.twitter.com", | |
"techmeme.com www.techmeme.com", | |
"techcrunch.com www.techcrunch.com", | |
"reddit.com www.reddit.com", | |
"hacker-newspaper.gilesb.com", | |
"icombinator.net www.icombinator.net", | |
"readwriteweb.com www.readwriteweb.com", | |
"gizmodo.com www.gizmodo.com", | |
"engadget.com www.engadget.com", | |
"tuaw.com www.tuaw.com", | |
"macnn.com www.macnn.com", | |
"arstechnica.com www.arstechnica.com", | |
"digg.com www.digg.com", | |
"wired.com www.wired.com", | |
"cnet.com www.cnet.com", | |
"zdnet.com www.zdnet.com", | |
"mashable.com www.mashable.com", | |
"boingboing.net www.boingboing.net", | |
"lifehacker.com www.lifehacker.com", | |
"gawker.com www.gawker.com", | |
"nytimes.com www.nytimes.com", | |
"tweetmeme.com www.tweetmeme.com", | |
"venturebeat.com www.venturebeat.com", | |
"torrentfreak.com www.torrentfreak.com", | |
"kottke.org www.kottke.org", | |
"daringfireball.net www.daringfireball.net", | |
"metafilter.com www.metafilter.com", | |
"codinghorror.com www.codinghorror.com", | |
"zenhabits.net www.zenhabits.net", | |
"macrumors.com www.macrumors.com", | |
"radar.oreilly.com", | |
"engadgetmobile.com www.engadgetmobile.com", | |
"gigaom.com www.gigaom.com", | |
"crunchgear.com www.crunchgear.com", | |
"news.google.com" | |
] | |
def off | |
update_hosts | |
end | |
def on | |
update_hosts(DISTRACTION_HOSTS) | |
end | |
private | |
def update_hosts(distractions = []) | |
# backup hosts | |
FileUtils.cp HOSTS_FILE, HOSTS_FILE_BACKUP | |
in_distractions = false | |
Tempfile.open("hosts") do |temp| | |
File.foreach(HOSTS_FILE) do |line| | |
if line =~ /^#{DISTRACTIONS_START}/ | |
in_distractions = true | |
end | |
temp.puts line unless in_distractions | |
if line =~ /^#{DISTRACTIONS_END}/ | |
in_distractions = false | |
end | |
end | |
unless distractions.empty? | |
temp.puts DISTRACTIONS_START | |
distractions.each do |host| | |
temp.puts "127.0.0.1 #{host}" | |
end | |
temp.puts DISTRACTIONS_END | |
end | |
temp.close | |
FileUtils.cp temp.path, HOSTS_FILE | |
end | |
end | |
end | |
focus = Focus.new | |
case ARGV[0] | |
when /stop|off/ | |
focus.off | |
else | |
focus.on | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment