#!/usr/bin/perl -w

use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use IO::Handle;

autoflush STDERR 1;

#
# LWP::UserAgent setup
#

my $cookies = HTTP::Cookies->new;
my $ua      = LWP::UserAgent->new(
  keep_alive              => 1,
  agent                   => 'SIBot/1.0',
  requests_redirectable   => ['HEAD', 'GET', 'POST'],
  cookie_jar              => $cookies,
);

$ua->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
#

my $login = $ua->post('http://freshmeat.net/login', {
  url         => '/',
  username    => $user,
  password    => $pass,
  persistent  => 1,
  submit      => 'Login',
});

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


#
# retrieve data
#

my $articles  = $ua->get('http://freshmeat.net/lounge/articles/');

die "failed retrieving articles" unless $articles->is_success;


#
# content parsing
#

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

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