#!/usr/bin/perl -w

use strict;
use WWW::Mechanize;
use IO::Handle;

autoflush STDERR 1;

#
# WWW::Mechanize setup
#

my $mech    = WWW::Mechanize->new(
  keep_alive              => 1,
  agent                   => 'SIBot/1.1',
  autocheck               => 1,
);

$mech->env_proxy;


#
# quick'n'dirty user/pass request
#

print STDERR "Login: ";
my $user = <STDIN>;
chomp $user;

print STDERR "Password: ";
my $pass = <STDIN>;
chomp $pass;


#
# login
#

$mech->get('http://freshmeat.net/');
$mech->submit_form(
  form_number => 4,
  fields      => {
    username  => $user,
    password  => $pass,
  }
);

die "login failed" unless $mech->content =~ /logged in as user /;


#
# retrieve data
#

$mech->follow_link(text_regex => qr/\[My\]/) or die;
$mech->follow_link(text_regex => qr/\d+ new articles/) or die;


#
# content parsing
#

my @list = grep(m#<a href="[^"]+"><b>[^<]+</b></a>#,
  split(/[\r\n]/, $mech->content));

foreach (@list[0 .. 9]) {
  m#<a href="([^"]+)"><b>([^<]+)</b></a>#;
  print "$2\nhttp://freshmeat.net$1\n";
}
