Acme::SDUM::Renew - Renew your books from www.sdum.uminho.pt


Acme-SDUM-Renew documentation Contained in the Acme-SDUM-Renew distribution.

Index


Code Index:

NAME

Top

Acme::SDUM::Renew - Renew your books from www.sdum.uminho.pt

VERSION

Top

Version 0.02

SYNOPSIS

Top

This module just exports one function wich is responsible of renew all your books from SDUM. At the end a report is sent to an email, so you can manually check (yes, manually!) if the operation suceeded.

    use Acme::SDUM::Renew;

    sdum_renew($username, $password, $email, $smtp);

EXPORT

Top

sdum_renew

FUNCTIONS

Top

sdum_renew

This is where the magic happens. This function receives the following parameters:

username

Username to SDUM (don't forget to prepend a 'A' in case you are a student like me).

password

Your super ultra secret password.

email

A valid email address to send the report.

smtp [optional]

This argument is optional but should be usefull when Mail::Sender defaults doesn't suit your network configuration. Just pass here the SMTP where your email are relayed and everything should go smoothly.

AUTHOR

Top

Ruben Fonseca, <root at cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-acme-sdum-renew at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Acme-SDUM-Renew. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Acme::SDUM::Renew

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Acme-SDUM-Renew

* CPAN Ratings

http://cpanratings.perl.org/d/Acme-SDUM-Renew

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Acme-SDUM-Renew

* Search CPAN

http://search.cpan.org/dist/Acme-SDUM-Renew

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


Acme-SDUM-Renew documentation Contained in the Acme-SDUM-Renew distribution.
package Acme::SDUM::Renew;

use warnings;
use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use HTML::Form;
use File::Temp qw/tempfile/;
use Mail::Sender;
use Carp;

our $VERSION  = '0.02';
our (@ISA)    = qw/Exporter/;
our (@EXPORT) = qw/sdum_renew/;

sub sdum_renew {
    my ($username, $password, $email, $smtp) = @_;

    croak "Username required" unless $username;
    croak "Password required" unless $password;
    croak "Notification email required" unless $email;
    # smtp is optional

    my $browser = LWP::UserAgent->new(
        requests_redirectable => ['GET', 'HEAD', 'POST']
    );
    $browser->cookie_jar( {} );
    $browser->env_proxy;
    
    # Fase 1: Get the session
    my $res = $browser->get('http://aleph.sdum.uminho.pt/');
    $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 1)\n";
    
    $res->content =~ /Math\.rand/ or die "Content not expected (Phase 1)\n";
    
    # Fase 2: Generate the random session
    my $session = int(rand() * 1000000000);
    $res = $browser->get('http://aleph.sdum.uminho.pt/F?RN=' . $session);
    $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 2)\n";
    
    $res->content =~ /top\.location/ or die "Content not expected (Phase 2)\n";
    $res->content =~ /\'(http[^\']+)\'/;
    
    # Fase 3: Get main page
    $res = $browser->get($1);
    $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 3)\n";
    
    $res->content =~ /href\=\"([^\"]+login-session)\"/
    or die "Content not expected (Phase 4)\n";
    
    # Fase 4: Get login form
    $res = $browser->get($1);
    $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 4)\n";
    
    my @forms = HTML::Form->parse($res);
    my $form = shift @forms;
    
    $form->value('bor_id', $username);
    $form->value('bor_verification', $password);
    
    $res = $browser->request($form->click);
    $res->is_success or die "Error submiting login form (Phase 4)\n";
    
    # Fase 5: Get Area Pessoal Link
    $res->content =~ /rea Pessoal/ or die "Content not expected (check username/password) (Phase 5)\n";
    
    $res->content =~ /href\=\"([^\"]+bor\-info)\"/
    or die "Can't find Area Pessoal Link (Phase 5)\n";
    
    $res = $browser->get($1);
    $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 5)\n";
    
    # Fase 6: Get Empréstimos Link
    $res->content =~ /Irregularidades/ or die "Content not expected (Phase 6)\n";
    
    $res->content =~ /href\=\"([^\"]+bor\-loan)\"/
    or die "Can't find Empréstimos Link (Phase 6)\n";
    
    $res = $browser->get($1);
    $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 6)\n";
    
    # Fase 7: Renew
    $res->content =~ /Desc Exemplar/ or die "Content not expected (Phase 7)\n";
    
    $res->content =~ /href\=\"([^\"]+bor\-renew\-all)\"/
    or die "Can't find Renovar Todos Link (Phase 7)\n";
    
    $res = $browser->get($1);
    $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 7)\n";
    
    my ($tempfh, $tempfile) = tempfile();
    print $tempfh $res->content;
    close $tempfh;
    
    # Send to email
    my $sender = new Mail::Sender { 
        smtp => $smtp,
        from => 'robot@futurama.net'
    };
    $sender->MailFile({
        to => $email,
        subject => 'SDUM Renew Results',
        msg => 'Please check the results',
        file => $tempfile,
        ctype => 'text/html',
    });

    croak $Mail::Sender::Error if $Mail::Sender::Error;
}

1; # End of Acme::SDUM::Renew