Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created August 23, 2011 08:07
Show Gist options
  • Save timruffles/1164608 to your computer and use it in GitHub Desktop.
Save timruffles/1164608 to your computer and use it in GitHub Desktop.
Multi-threaded Touchpad watcher - taking it too seriously?
require "net/http"
require "csv"
require "addressable/uri"
require "pp"
unstable_urls = ARGV[0]
stable_urls = ARGV[1]
urls = {}
[[unstable_urls,:grep],[stable_urls,:change]].each do |urls_file,technique|
CSV.read(urls_file).map { |url|
url = Addressable::URI.parse url.first
setup = urls[url] = {}
setup[:technique] = technique
}
end
statuses = {}
threads = []
urls.each do |url,setup|
threads << Thread.new do
status = statuses[url] ||= {}
status[:last] = false
loop do
if status[:found] == true
puts "Found in #{url} - time to check it!"
`say "Found one"`
else
begin
# puts "Starting #{url} using '#{setup[:technique]}' technique"
page = Net::HTTP.get url
# puts "Got #{url}"
rescue Timeout::Error => e
puts "Timeout #{url}"
next
end
if setup[:technique] == :change
unless status[:last] == false
status[:found] = page != status[:last]
end
status[:last] = page
if status[:found] == true
puts "\n\nCHANGED\n#{url}"
end
else
if match = page.scan(/(?:£|&pound;)(?:89)/)
if status[:initial_count].nil?
status[:initial_count] = match.length
else
if match.length != status[:initial_count]
puts "\n\nMATCHED\n#{url}\n#{match.join(",")}"
status[:found] = true
end
end
end
end
end
sleep 10
end
end
end
threads.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment