FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

Automating your Forum Signature with Perl




A few days ago, I thought it would be nice to put the three most recent headlines from Slashdot in my forum signature (Don't ask me why). This can be done using a cronjob which runs every hour and executes a little program which gets the 3 newest headlines from slashdot and changes my signature. The program is short and simple. I wrote it in Perl, using Perl's WWW::Mechanize library, which allows you to automate all kinds of web related tasks: following links, filling in forms, downloading and uploading, etc.

First the script has to fetch the latest headlines. Although you can take them from the frontpage at www.slashdot.org, this file contains all kinds of HTML and other stuff which is of no interest in this case. Of course slashdot also offers serveral RSS-feeds, and the feed located at http://rss.slashdot.org/Slashdot/slashdot/to is exactly what I want: it contains both the titles and links of the articles, and it is short and easy to parse.

First I use get($url) to get the source of the page. Get() is provided by the module LWP::Simple and simply returns the HTML source of the URL as a string. Next, a regexp is applied to the string to get the titles and links of the first 3 items, and they are used to create the new sig:

Code:

sub headlines() {
    my $source = get("http://rss.slashdot.org/Slashdot/slashdot/to");
    $source =~ /<item>.*?<title>(.[^<]*?)<\/title>.*?<link>([^<]*?)<\/link>.*?{3}/si;
    return "Now on slashdot:\n[url=".$2."]".$1."[/url]\n[url=".$4."]".$3."[/url]\n[url=".$6."]".$5."[/url]";
}


Next, the script has to put the new sig into my profile, located at http://www.example.com/phpbb/profile.php?mode=editprofile (this URL of course depends on the site at which the forum is located). Before the script can make changes, it first has to log in. To log in we have to fill out the form at http://www.example.com/phpbb/login.php (or something like that) and submit the form:

Code:

    my $mech = WWW::Mechanize->new();
   $mech->get("http://www.example.com/phpbb/login.php");
   $mech->form_number(1);
   $mech->field("username", "Monkeys");
   $mech->field("password", "Bananas");
   $mech->click_button( name=> "login" );


That's all. WWW::Mechanize takes care of the details. It also stores the cookies it receives after logging in for the remainder of the session. Now that we are logged in, and have a cookie, we can change the profile page:

Code:

    $mech->get("http://www.example.com/phpbb/profile.php?mode=editprofile");
   $mech->form_number(2);
   $mech->field("signature", headlines());
   $mech->click_button( name=>"submit");


And that's it. Here is the entire script:

Code:

use strict;
use WWW::Mechanize;
use LWP::Simple;

my $mech = WWW::Mechanize->new();

sub headlines {
   my $source = get("http://rss.slashdot.org/Slashdot/slashdot/to");
   $source =~ /<item>.*?<title>(.[^<]*?)<\/title>.*?<link>([^<]*?)<\/link>.*?{3}/si;
   return "Now on slashdot:\n[url=".$2."]".$1."[/url]\n[url=".$4."]".$3."[/url]\n[url=".$6."]".$5."[/url]";
}

sub changesig {
   $mech->get("http://www.example.com/phpbb/login.php");
   $mech->form_number(1);
   $mech->field("username", "Monkey");
   $mech->field("password", "Bananas");
   $mech->click_button( name=> "login" );
   $mech->get("http://www.example.com/phpbb/profile.php?mode=editprofile");
   $mech->form_number(2);
   $mech->field("signature", headlines());
   $mech->click_button( name=>"submit");
}

changesig();


Of course there is some room for improvement. Instead of using a regular expression to get the headlines from the feed, it might be better to use a module which provides a parser for XML or RSS. Some error checking might also be useful. Finally it would be nice to have the script keep the cookie after login, and only have it login again when it turns out the cookies has been invalidated by the server. I leave this as excercises for the reader. Smile



0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.