line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Part of get-flash-videos. See get_flash_videos for copyright. |
2
|
|
|
|
|
|
|
package FlashVideo::Mechanize; |
3
|
3
|
|
|
3
|
|
6790
|
use WWW::Mechanize; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use FlashVideo::Downloader; |
5
|
|
|
|
|
|
|
use Encode (); |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use strict; |
8
|
|
|
|
|
|
|
use base "WWW::Mechanize"; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
|
|
|
|
|
|
my $class = shift; |
12
|
|
|
|
|
|
|
my $browser = $class->SUPER::new(autocheck => 0); |
13
|
|
|
|
|
|
|
$browser->agent_alias("Windows Mozilla"); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $proxy = $App::get_flash_videos::opt{proxy}; |
16
|
|
|
|
|
|
|
if($proxy && $proxy !~ /^\w+:/) { |
17
|
|
|
|
|
|
|
my $port = ($proxy =~ /:(\d+)/)[0] || 1080; |
18
|
|
|
|
|
|
|
$proxy = "socks://$1:$port"; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
if($proxy) { |
22
|
|
|
|
|
|
|
$browser->proxy([qw[http https]] => $proxy); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
if($browser->get_socks_proxy) { |
26
|
|
|
|
|
|
|
if(!eval { require LWP::Protocol::socks }) { |
27
|
|
|
|
|
|
|
die "LWP::Protocol::socks is required for SOCKS support, please install it\n"; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
return $browser; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub redirect_ok { |
35
|
|
|
|
|
|
|
my($self) = @_; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
return $self->{redirects_ok}; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub allow_redirects { |
41
|
|
|
|
|
|
|
my($self) = @_; |
42
|
|
|
|
|
|
|
$self->{redirects_ok} = 1; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub get { |
46
|
|
|
|
|
|
|
my($self, @rest) = @_; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
print STDERR "-> GET $rest[0]\n" if $App::get_flash_videos::opt{debug}; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $r = $self->SUPER::get(@rest); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
if($App::get_flash_videos::opt{debug}) { |
53
|
|
|
|
|
|
|
my $text = join " ", $self->response->code, |
54
|
|
|
|
|
|
|
$self->response->header("Content-type"), "(" . length($self->content) . ")"; |
55
|
|
|
|
|
|
|
$text .= ": " . DBI::data_string_desc($self->content) if eval { require DBI }; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
print STDERR "<- $text\n"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
return $r; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub update_html { |
64
|
|
|
|
|
|
|
my($self, $html) = @_; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $charset = _parse_charset($self->response->header("Content-type")); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# If we have no character set in the header (therefore it is worth looking |
69
|
|
|
|
|
|
|
# for a http-equiv in the body) or the content hasn't been decoded (older |
70
|
|
|
|
|
|
|
# versions of Mech). |
71
|
|
|
|
|
|
|
if($LWP::UserAgent::VERSION < 5.827 |
72
|
|
|
|
|
|
|
&& (!$charset || !Encode::is_utf8($html))) { |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# HTTP::Message helpfully decodes to iso-8859-1 by default. Therefore we |
75
|
|
|
|
|
|
|
# do the inverse. This is fucking frail and will probably break. |
76
|
|
|
|
|
|
|
$html = Encode::encode("iso-8859-1", $html) if Encode::is_utf8($html); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Check this doesn't look like a video.. |
79
|
|
|
|
|
|
|
if(!FlashVideo::Downloader->check_magic($html)) { |
80
|
|
|
|
|
|
|
my $p = HTML::TokeParser->new(\$html); |
81
|
|
|
|
|
|
|
while(my $token = $p->get_tag("meta")) { |
82
|
|
|
|
|
|
|
my($tag, $attr) = @$token; |
83
|
|
|
|
|
|
|
if($tag eq 'meta' && $attr->{"http-equiv"} =~ /Content-type/i) { |
84
|
|
|
|
|
|
|
$charset ||= _parse_charset($attr->{content}); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
if($charset) { |
89
|
|
|
|
|
|
|
eval { $html = Encode::decode($charset, $html) }; |
90
|
|
|
|
|
|
|
FlashVideo::Utils::error("Failed decoding as $charset: $@") if $@; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
return $self->SUPER::update_html($html); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub _parse_charset { |
99
|
|
|
|
|
|
|
my($field) = @_; |
100
|
|
|
|
|
|
|
return(($field =~ /;\s*charset=([-_.:a-z0-9]+)/i)[0]); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub get_socks_proxy { |
104
|
|
|
|
|
|
|
my $self = shift; |
105
|
|
|
|
|
|
|
my $proxy = $self->proxy("http"); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
if(defined $proxy && $proxy =~ m!^socks://(.*?):(\d+)!) { |
108
|
|
|
|
|
|
|
return "$1:$2"; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
return ""; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |