Perl commandline tool for reading google groups posts

3 06 2007

This is a little commandline tool I wrote with Perl to help myself read the posts in google groups. It fetches the thread urls with the latest posts
and opens them in firefox tabs. I use it from cygwin command prompt in my windows machine. You can easily customize it to help your needs. Long live open source.

Why did I create it: I use a feed reader to read the posts of the groups i subscribed from google groups. To read the posts, I click on the the links on summary/description of each post in my reader. Then I reach the page with only one post, then I click on another link to reach to the thread. I find doing that few times everyday really painful. So this one will keep my sanity intact for now.

#!/usr/bin/perl
#browse.pl

# example useage:
# browse.pl
# browse.pl perl.beginners
# browse.pl comp.unix.shell
# (comp.lang.perl.misc is the default group) 

use strict;
use warnings;
use WWW::Mechanize;

my $browser_path = '/cygdrive/c/Program\\ Files/Mozilla\\ Firefox/firefox.exe ';
my $group_name = 'comp.lang.perl.misc'; # default group. it will be used if you don't provide one as parameter
$group_name = $ARGV[0] if @ARGV; # user specified group, from commandline parameter
my $url = 'http://groups.google.com/group/'.$group_name.'/topics?gvc=2'; 
my $limit = 10; # limit number of posts to open

print "Group: [$group_name]\\n";
my $m = WWW::Mechanize->new();
print "Getting $limit links of threads...\\n";
$m->get($url);
die "oooops! could not load main page\\n" unless $m->success;
my $html = $m->content();
#print $html;

my @links = ($html =~ m{<td><a href="(/group/$group_name/browse_thread/thread/[^/]+/[^/]+)#[^/]+">}igs);
if (@links)	{
	@links = map {'http://groups.google.com' . $_} @links;
	my @links_limited = splice @links, 0, $limit;
	my $url_string = sprintf (qq/"%s"/, join q/" "/, @links_limited);
	#print $url_string."\\n";
	print "Opening browser...\\n";
	system ($browser_path . $url_string . ' &');
}else{
	print "oooops! could not load main page\\n";
}

exit 0;

Note: There are better ways to do this. Use of HTML::TreeBuilder or HTML::Parser would be more standard. But I like to write regex by hand (😉 actually that’s the main reason I bothered writing this script).

Click here to download the script.

technorati tags:, , , , , ,

Blogged with Flock


Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: