Wednesday, October 24, 2007

twitter2hp.rb - post twitter items to your printer

This simple ruby script reads the specified twitter RSS feed and posts the latest item to your HP printer. Use it to post local weather, pithy quotes, or my favorite, the lolprinters twitter feed :-)

Stick it in cron to run every hour or so to keep those messages fresh!


#!/usr/local/bin/ruby

# twitter2hp.rb - loads a twitter RSS feed
# and posts the latest entry
# to the specified hp printer

#change to your printer's IP
printer_ip = '127.0.0.1'
#RSS feed to grab
rss_url = "http://twitter.com/statuses/user_timeline/9547912.rss"

# www.lolprinters.com

# simplified version of the code from
# http://snippets.dzone.com/posts/show/3714
# (removed sqlite data store)

require 'rubygems'
require 'simple-rss'
require 'open-uri'
require 'socket'

#rss feed to post
#http://twitter.com/lolprinters
rss_user_agent = "twitter2hp.rb"
rss = SimpleRSS.parse open(rss_url ,"User-Agent" => rss_user_agent)

# you can use the gsub to strip out the username
# that gets added by twitter
puts "#{rss.items.first.title.gsub(/lolprinters: /, '')}"

hpcommand = <<EOF
\e%-12345X\@PJL JOB"
\@PJL RDYMSG DISPLAY="#{rss.items.first.title.gsub(/lolprinters: /, '')}"
\@PJL EOJ
\e%-12345X
EOF

include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 9100, printer_ip )
socket.connect( sockaddr )
socket.write( hpcommand )
socket.close


This script uses some code from RSS Twitter Bot. Thanks! I originally spotted the HP ready message hack on this BoingBoing post

0 comments: