File Coverage

blib/lib/Acme/SDUM/Renew.pm
Criterion Covered Total %
statement 24 68 35.2
branch 0 42 0.0
condition n/a
subroutine 8 9 88.8
pod 1 1 100.0
total 33 120 27.5


line stmt bran cond sub pod time code
1             package Acme::SDUM::Renew;
2              
3 1     1   96013 use warnings;
  1         2  
  1         37  
4 1     1   5 use strict;
  1         2  
  1         31  
5 1     1   2989 use LWP::UserAgent;
  1         183168  
  1         42  
6 1     1   2109 use HTTP::Cookies;
  1         32978  
  1         46  
7 1     1   1830 use HTML::Form;
  1         48649  
  1         40  
8 1     1   1337 use File::Temp qw/tempfile/;
  1         61057  
  1         79  
9 1     1   1544 use Mail::Sender;
  1         213845  
  1         176  
10 1     1   18 use Carp;
  1         2  
  1         1141  
11              
12             =head1 NAME
13              
14             Acme::SDUM::Renew - Renew your books from www.sdum.uminho.pt
15              
16             =head1 VERSION
17              
18             Version 0.02
19              
20             =cut
21              
22             our $VERSION = '0.02';
23             our (@ISA) = qw/Exporter/;
24             our (@EXPORT) = qw/sdum_renew/;
25              
26             =head1 SYNOPSIS
27              
28             This module just exports one function wich is responsible
29             of renew all your books from SDUM. At the end a report
30             is sent to an email, so you can manually check (yes, manually!)
31             if the operation suceeded.
32              
33             use Acme::SDUM::Renew;
34              
35             sdum_renew($username, $password, $email, $smtp);
36              
37             =head1 EXPORT
38              
39             sdum_renew
40              
41             =head1 FUNCTIONS
42              
43             =head2 sdum_renew
44              
45             This is where the magic happens. This function receives the
46             following parameters:
47              
48             =over 4
49              
50             =item username
51              
52             Username to SDUM (don't forget to prepend a 'A' in case you are a student like me).
53              
54             =item password
55              
56             Your super ultra secret password.
57              
58             =item email
59              
60             A valid email address to send the report.
61              
62             =item smtp [optional]
63              
64             This argument is optional but should be usefull when Mail::Sender defaults
65             doesn't suit your network configuration. Just pass here the SMTP where
66             your email are relayed and everything should go smoothly.
67              
68             =back
69              
70             =cut
71              
72             sub sdum_renew {
73 0     0 1   my ($username, $password, $email, $smtp) = @_;
74              
75 0 0         croak "Username required" unless $username;
76 0 0         croak "Password required" unless $password;
77 0 0         croak "Notification email required" unless $email;
78             # smtp is optional
79              
80 0           my $browser = LWP::UserAgent->new(
81             requests_redirectable => ['GET', 'HEAD', 'POST']
82             );
83 0           $browser->cookie_jar( {} );
84 0           $browser->env_proxy;
85            
86             # Fase 1: Get the session
87 0           my $res = $browser->get('http://aleph.sdum.uminho.pt/');
88 0 0         $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 1)\n";
89            
90 0 0         $res->content =~ /Math\.rand/ or die "Content not expected (Phase 1)\n";
91            
92             # Fase 2: Generate the random session
93 0           my $session = int(rand() * 1000000000);
94 0           $res = $browser->get('http://aleph.sdum.uminho.pt/F?RN=' . $session);
95 0 0         $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 2)\n";
96            
97 0 0         $res->content =~ /top\.location/ or die "Content not expected (Phase 2)\n";
98 0           $res->content =~ /\'(http[^\']+)\'/;
99            
100             # Fase 3: Get main page
101 0           $res = $browser->get($1);
102 0 0         $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 3)\n";
103            
104 0 0         $res->content =~ /href\=\"([^\"]+login-session)\"/
105             or die "Content not expected (Phase 4)\n";
106            
107             # Fase 4: Get login form
108 0           $res = $browser->get($1);
109 0 0         $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 4)\n";
110            
111 0           my @forms = HTML::Form->parse($res);
112 0           my $form = shift @forms;
113            
114 0           $form->value('bor_id', $username);
115 0           $form->value('bor_verification', $password);
116            
117 0           $res = $browser->request($form->click);
118 0 0         $res->is_success or die "Error submiting login form (Phase 4)\n";
119            
120             # Fase 5: Get Area Pessoal Link
121 0 0         $res->content =~ /rea Pessoal/ or die "Content not expected (check username/password) (Phase 5)\n";
122            
123 0 0         $res->content =~ /href\=\"([^\"]+bor\-info)\"/
124             or die "Can't find Area Pessoal Link (Phase 5)\n";
125            
126 0           $res = $browser->get($1);
127 0 0         $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 5)\n";
128            
129             # Fase 6: Get Empréstimos Link
130 0 0         $res->content =~ /Irregularidades/ or die "Content not expected (Phase 6)\n";
131            
132 0 0         $res->content =~ /href\=\"([^\"]+bor\-loan)\"/
133             or die "Can't find Empréstimos Link (Phase 6)\n";
134            
135 0           $res = $browser->get($1);
136 0 0         $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 6)\n";
137            
138             # Fase 7: Renew
139 0 0         $res->content =~ /Desc Exemplar/ or die "Content not expected (Phase 7)\n";
140            
141 0 0         $res->content =~ /href\=\"([^\"]+bor\-renew\-all)\"/
142             or die "Can't find Renovar Todos Link (Phase 7)\n";
143            
144 0           $res = $browser->get($1);
145 0 0         $res->is_success or die "Error reading from aleph.sdum.uminho.pt (Phase 7)\n";
146            
147 0           my ($tempfh, $tempfile) = tempfile();
148 0           print $tempfh $res->content;
149 0           close $tempfh;
150            
151             # Send to email
152 0           my $sender = new Mail::Sender {
153             smtp => $smtp,
154             from => 'robot@futurama.net'
155             };
156 0           $sender->MailFile({
157             to => $email,
158             subject => 'SDUM Renew Results',
159             msg => 'Please check the results',
160             file => $tempfile,
161             ctype => 'text/html',
162             });
163              
164 0 0         croak $Mail::Sender::Error if $Mail::Sender::Error;
165             }
166              
167             =head1 AUTHOR
168              
169             Ruben Fonseca, C<< >>
170              
171             =head1 BUGS
172              
173             Please report any bugs or feature requests to
174             C, or through the web interface at
175             L.
176             I will be notified, and then you'll automatically be notified of progress on
177             your bug as I make changes.
178              
179             =head1 SUPPORT
180              
181             You can find documentation for this module with the perldoc command.
182              
183             perldoc Acme::SDUM::Renew
184              
185             You can also look for information at:
186              
187             =over 4
188              
189             =item * AnnoCPAN: Annotated CPAN documentation
190              
191             L
192              
193             =item * CPAN Ratings
194              
195             L
196              
197             =item * RT: CPAN's request tracker
198              
199             L
200              
201             =item * Search CPAN
202              
203             L
204              
205             =back
206              
207             =head1 ACKNOWLEDGEMENTS
208              
209             =head1 COPYRIGHT & LICENSE
210              
211             Copyright 2007 Ruben Fonseca, all rights reserved.
212              
213             This program is free software; you can redistribute it and/or modify it
214             under the same terms as Perl itself.
215              
216             =cut
217              
218             1; # End of Acme::SDUM::Renew