How To Update Facebook, Twitter, and Kopete's Status All At Once

I have whipped up a short script to update Facebook’s status, Twitter, and Kopete’s status all at once. The Facebook portion of the code comes directly from Kerry Buckley’s script

To make use of my script, you need:

To use this code, edit the constants on top to set your login details, and simply execute status "my status message". Kopete and Twitter will be updated as you expect, but Facebook will appear as <yourname> is saying "my status message". Everything before the ‘saying’ is beyond my control, so I have to reformat the message so that it makes grammatical sense. It’d be nice if Facebook didn’t have this constraint.

To clear your messages, type status reset. This sends an empty message.

Note: this code is still experimental - use at your own risk! The code:

  1. #!/usr/bin/env ruby
  2.  
  3. FACEBOOK<em>EMAIL = ‘name@example.com’
  4. FACEBOOK</em>PASSWORD = ‘mypassword’
  5.  
  6. TWITTER<em>EMAIL = ‘name@example.com’
  7. TWITTER</em>PASSWORD =‘mypassword’
  8.  
  9. USERAGENT = ‘Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/XX (KHTML, like Gecko) Safari/YY
  10.  
  11. class StatusUpdater
  12.     require ‘net/http’
  13.    require ‘net/https’
  14.   require ‘rexml/document’
  15.  require ‘twitter’
  16.     include REXML
  17.  
  18.  def initialize(options = {})
  19.        @options = {
  20.            :useragent => USERAGENT,
  21.             :fb<em>email => FACEBOOK</em>EMAIL,
  22.             :fb<em>pass => FACEBOOK</em>PASSWORD,
  23.           :tw<em>email => TWITTER</em>EMAIL,
  24.          :tw<em>pass => TWITTER</em>PASSWORD
  25.             }.merge(options)
  26.    end
  27.  
  28.    def update<em>twitter(status)
  29.      twitter = Twitter::Base.new(TWITTER</em>EMAIL, TWITTER<em>PASSWORD)
  30.        twitter.update(status)
  31.  end
  32.  
  33.    def update</em>kopete(status)
  34.       status = status.gsub(\n, ’ ‘)
  35.         system("dcop kopete KopeteIface setAway \"#{status}\" false")
  36.   end
  37.  
  38.    def update<em>facebook(status)
  39.         status = "saying \"#{status.gsub(‘\n’, ’ ‘)}\"" unless status ==
  40.  
  41.      # Log in to FaceBook
  42.        req = Net::HTTP::Post.new ‘/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php’
  43.      req.set</em>form<em>data({‘email’ => @options[:fb</em>email], ‘pass’ => @options[:fb<em>pass]})
  44.         http = Net::HTTP.new(‘login.facebook.com’, 443)
  45.       http.use</em>ssl = true
  46.         http.start do |session|
  47.             resp = http.request req
  48.             @cookies = resp[‘Set-Cookie’].split(’, ‘).select { |line| line =~ /^\S*=/}.collect { |cookie| cookie.split(/; ?/)[0] }.join ’; ‘
  49.      end
  50.  
  51.        http = Net::HTTP.new ‘m.facebook.com’
  52.         http.start do |session|
  53.             # Retrieve the [mobile] home page, to get the form ID
  54.           resp = session.get ‘/home.php’, {‘User-Agent’ => USERAGENT, ‘Cookie’ => @cookies}
  55.           doc = Document.new(resp.body)
  56.           post<em>form</em>id = (XPath.first doc, "//input[@name=’post<em>form</em>id’]/@value").to<em>s
  57.  
  58.          # Post the status
  59.           resp = session.post ‘/home.php’,"post</em>form<em>id=#{URI.encode post</em>form<em>id}&status=#{URI.encode status}&update=Update",{‘User-Agent’ => USERAGENT, ‘Content-Type’ => ‘application/x-www-form-urlencoded’,‘Referer’ => ‘http://m.facebook.com/home.php’,‘Cookie’ => @cookies}
  60.       end
  61.     end
  62.  
  63.    def update(status)
  64.      status = status.gsub("\n", \n)
  65.  
  66.       update</em>methods = self.methods.select {|method<em>name| method</em>name =~ /^update<em>[a-z]{1,}$/}
  67.  
  68.       update</em>methods.each do |update<em>method|
  69.          self.send(update</em>method, status)
  70.        end
  71.     end
  72. end
  73.  
  74. status = ARGV.first
  75.  
  76. status = "" if status == ‘reset’
  77.  
  78. updater = StatusUpdater.new
  79. updater.update(status)

bk