1
#!/usr/bin/perl
2
use strict;
3
use warnings;
4
use feature qw(say);
5
use HTTP::Cookies;
6
use WWW::Mechanize;
7
use Getopt::Euclid;
8
use Term::ReadPassword;
9
10
use constant COOKIE   => "$ENV{HOME}/.legendas.tv.cookie";
11
12
use constant PT_BR => 1;
13
use constant EN    => 2;
14
use constant ES    => 3;
15
use constant PT    => 10;
16
use constant ALL   => 99;
17
use constant OTHER => 100;
18
19
use constant SELECT_LANG => PT_BR;
20
21
sub clean {
22
    my $cookie = shift;
23
    say ' * limpando cookie';
24
    $cookie->clear;
25
}
26
27
my $cookie = HTTP::Cookies->new(file => COOKIE, autosave => 1);
28
29
my $logged = 0;
30
$cookie->scan(sub {
31
      my ($key, $expires) = ($_[1], $_[8]);
32
      return unless $key eq 'Auth';
33
      $logged = ($expires > time ? 1 : 0);
34
});
35
36
my $browser = WWW::Mechanize->new(cookie_jar => $cookie);
37
say ' * acessando legendas.tv';
38
$browser->get('http://legendas.tv');
39
40
unless ($logged) {
41
   say ' * entre com seu login e senha';
42
   print '   login: '; chomp(my $LOGIN = <STDIN>);
43
   my $PASSWORD = read_password('   senha: ');
44
   say ' * logando';
45
   $browser->submit_form(
46
      form_name   => 'form1',
47
      form_number => 1,
48
      fields      => { txtLogin => $LOGIN, txtSenha => $PASSWORD, chkLogin => 1 },
49
      button      => 'entrar',
50
   );
51
   $browser->follow_link( text_regex => qr/clique aqui caso/i );
52
}
53
54
say " * pesquisando $ARGV{'<filme>'}";
55
$browser->submit_form(
56
   form_name   => 'form1',
57
   form_number => 1,
58
   fields      => { txtLegenda => $ARGV{'<filme>'}, selTipo => 1, int_idioma => SELECT_LANG },
59
   button      => 'btn_buscar',
60
);
61
62
my $result = $browser->content;
63
64
my @film = ();
65
while ($result =~ m/<span onmouseover.*?gpop\('([^']+)','([^']+)','([^']+)','(\d+)','\d+','(\d+MB)',.*?abredown\('(\w+)'\)/sigo) {
66
   push @film, {filme => $1, descricao => $2, release => $3, cds => $4, size => $5, id => $6};
67
}
68
69
if (@film > 0) {
70
   say " * ", scalar(@film), " legenda(s) encontrada(s)";
71
}
72
else {
73
   say " * nenhuma legenda encontrada!";
74
   clean($cookie) if defined $ARGV{'-c'};
75
   exit -1;
76
}
77
78
my $response = 's';
79
while ($response eq 's') {
80
   for my $index (0 .. $#film) {
81
      my $n = $index + 1;
82
      say "\n ", $n, " $film[$index]->{release}";
83
      say "   $film[$index]->{filme} - $film[$index]->{descricao}";
84
      say "   tamanho: $film[$index]->{size}, cds: $film[$index]->{cds}";
85
   }
86
87
   print "\n   Selecione uma legenda para baixar: (q para sair) ";
88
   chomp(my $n = <STDIN>);
89
   last if $n eq 'q';
90
   my $index = $n - 1;
91
92
   say "\n * baixando legenda $film[$index]->{release}";
93
   $browser->get("http://legendas.tv/info.php?d=$film[$index]->{id}&c=1");
94
95
   say ' * arquivo ' . $browser->response->filename . ' salvo';
96
   $browser->save_content('./'. $browser->response->filename);
97
98
   print "\n   baixar outra legenda? (s/n)";
99
   chomp($response = <STDIN>);
100
}
101
102
clean($cookie) if defined $ARGV{'-c'};
103
104
exit 0;
105
106
__END__
107
108
=head1 NAME
109
110
legendas.tv - pesquisa e baixa legendas do site legendas.tv
111
112
=head1 VERSION
113
114
0.03
115
116
=head1 USAGE
117
118
legendas.tv <filme> [options]
119
120
=head1 REQUIRED ARGUMENTS
121
122
=over
123
124
=item <filme>
125
126
Nome do filme
127
128
=for Euclid
129
   filme.type: string, length(filme) > 2
130
131
=back
132
133
=head1 OPTIONS
134
135
=over
136
137
=item -c
138
139
Limpa o cache
140
141
=back
142
143
=head1 DEPENDENCIES
144
145
L<HTTP::Cookies>,
146
L<WWW::Mechanize>,
147
L<Getopt::Euclid>,
148
L<Term::ReadPassword>
149
150
=head1 TODO
151
152
Exibir legendas das proximas paginas quando houver paginação
153
154
=head1 AUTHOR
155
156
Joenio Costa <joenio@perl.org.br>
157
158
=head1 COPYRIGHT
159
160
Copyright (c) 2008, Joenio Costa. All Rights Reserved.
161
162
This program is free software: you can redistribute it and/or modify
163
it under the terms of the GNU General Public License version 3 as
164
published by the Free Software Foundation.