Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The code that powered the Twerrible Towel (http://twerribletowel.com). It processed 44,610 relevant tweets over the course of 5 days.
#!/usr/bin/env python
import sys
import random
from ctypes import *
from Phidgets.Devices.InterfaceKit import InterfaceKit
from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException
try:
kit = InterfaceKit()
kit.openPhidget()
except RuntimeError as e:
print("Error %i: %s" % (e.code, e.details))
exit(1)
flip = (len(sys.argv) > 1 && sys.argv[0] == "on")
try:
kit.waitForAttach(10000)
kit.setOutputState(0, flip)
except PhidgetException as e:
print("Error %i: %s" % (e.code, e.details))
kit.closePhidget()
exit(1)
kit.closePhidget()
exit(0)
##
# Twerrible Towel
#
# It makes a towel spin.
#
# It complains about its problems via Prowl.
#
# Keep it simple, handsome.
##
require 'rubygems'
require 'twitter/json_stream'
require 'prowl'
require 'json'
PROWL = {
:keys => [] # => ["8b6954da6ff41c...", "8b6954da6ff41c..."]
}
TWITTER = {
:username => "",
:password => "",
:search_terms => [], # => ["steelersnation", "steelernation"]
:count_file => "#{Dir.pwd}/tweets.count"
}
class Integer
def ordinalize
return "#{self}th" if [11, 12, 13].include?(self % 100)
case self % 10
when 1
"#{self}st"
when 2
"#{self}nd"
when 3
"#{self}rd"
else
"#{self}th"
end
end
end
def notify(event, description)
puts "Twitter => #{event}: #{description}"
PROWL[:keys].each do |key|
Prowl.add(
:apikey => key,
:application => "Twerrible Towel",
:event => event,
:description => description
)
end
end
count = File.read(TWITTER[:count_file]).to_i
EventMachine::run do
stream = Twitter::JSONStream.connect(
:path => '/1/statuses/filter.json',
:auth => "#{TWITTER[:username]}:#{TWITTER[:password]}",
:method => 'POST',
:content => "track=#{TWITTER[:search_terms].join(',')}"
)
stream.each_item do |item|
tweet = JSON.parse item
screen_name = tweet['user']['screen_name']
puts "#{count} => #{screen_name}"
# Ensign, engage!
`python switch.py on`
`open "x-launchbar:large-type?string=@#{screen_name}\\#{count.ordinalize} twirl"`
sleep 4
`python switch.py`
count += 1
file = File.open(TWITTER[:count_file], 'w')
file.puts count
file.close
end
stream.on_error do |message|
notify "Error", message
end
stream.on_reconnect do |timeout, retries|
notify "Reconnect", "Reconnecting in #{timeout} seconds"
end
stream.on_max_reconnects do |timeout, retries|
notify "Max Reconnects", "Failed after #{retries} failed reconnects"
end
trap('INT') do
stream.stop
EventMachine.stop if EventMachine.reactor_running?
end
end
puts "Stopped."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.