Thursday, September 16, 2010

how to update facebook status via command line

Recently, I found a text-based twitter client named "twidge".  It allows me to tweet right from command line.  For example, if you want to do a hello world tweet, you run this:

% twidge update "hello world"

I then think it would be a good idea if I can do the same thing with facebook i.e. to update facebook status right from command line.  However, when I did search on google, I found nothing.

Well, actually, there is an indirect way by:
  1. Adding facebook application "Selective Tweet".
  2. With the same twitter account you use to add "Selective Tweet", whenever you append "#fb" to your tweet, that tweet will also appear on facebook status.
But for many personal reasons, I just dislike this method as the same thing will be put onto both twitter and facebook which is not always what I want.

So what if I want to do it directly...

Recently, I had a chance to try out the Perl module "WWW::Mechanize" - Handy web browsing in a Perl object.  It is a subclass of the well-known LWP::UserAgent module which is a User Agent module for Perl which we use to write robot, spider and such.

Here is the Perl script I use to update facebook status:

#!/usr/bin/perl

use WWW::Mechanize;

$mech = WWW::Mechanize->new;
$mech->agent_alias('Windows Mozilla');

$base_url = 'http://m.facebook.com';

# {{{ login
$mech->get($base_url);
$response = $mech->submit_form(
  fields => {
    email => 'your_email_address',
    pass => 'facebook_password'
  }
);
die $response->status_line unless $response->status_line;
# }}}
 
while (1) {
  print "What's on your mind? : ";
  $status = <STDIN>;
  $response = $mech->submit_form(
    fields => {
      status => $status
    }
  );
  die $response->status_line unless $response->status_line;
}

Here is what you will see when you run it:

unsigned_nerd@linuxsucks ~ % fbset
What's on your mind? : it's easier to update facebook status from command line

By the way, isn't it better if I use some Facebook API instead...?  Umm... maybe later.

10 comments:

cloverink said...

good!

phu said...

OMG!

Anonymous said...

When I run this I get "No form defined at line 12" which is the login submit_form line.

unsigned_nerd said...

hmm thanks for your comment. i have heard that a week or so ago, the mobile version of facebook (m.facebook.com) has been updated. i will check it later and let you know.

Geoff Schultz said...

Thank you so much for taking a look at this. I have a sailing blog that I update via e-mail (via high frequency radio) while at sea and/or many of the places that I visit that don't have Internet.

The script that processes my e-mail is written in Perl and I wanted to add the capability of posting to FB that a new log was available. I'll starting heading down the Pacific coast of Mexico in about a week and won't have Internet for most of 4 months, but if this doesn't get done, it doesn't get done. There will always be next year...

Anonymous said...

I'll jump in and ask for an update to this. I've tried all kinds of scripts to do this, and this is the cleanest that I've seen. None of them seem to work anymore. If you could fix this, that would be very helpful!

unsigned_nerd said...

Hi...

I can make it work now without modifying codes. It didn't work at first because I tested it on MacOSX which didn't have a module: Crypt::SSLeay.

Can you post your full error message again plus the system you are running on?

THanks

Anonymous said...

I added "use Crypt::SSLeay;" and I still get "No form defined at fb_status_update_3.pl line 13"

unsigned_nerd said...

can you open this url: http://m.facebook.com from a web browser on the same machine where this fbset script runs on, view source and send me its content? r u using a proxy to browse website?

Anonymous said...

I'm trying to run this on a shared web hosting site (www.pair.com) and only have telnet access to it. I've used lynx and similar applications, but have no idea how to do a "view source". Any ideas?

Post a Comment