Robot/Browser.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.4% 42 / 44 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Robot.pm ↑ Summary
1 # ABSTRACT: get/post url, return unicode content, auto detect CJK charset
2 package Novel::Robot::Browser;
3
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 our $VERSION = 0.23;
9
10 6 ▶use Encode::Detect::CJK qw/detect/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use File::Slurp qw/slurp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use HTTP::CookieJar;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use IO::Uncompress::Gunzip qw(gunzip);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 0 ▶use Firefox::Marionette();
Subroutine BEGIN: called CC=1 SCAR=2.6
18
19
20 our $DEFAULT_URL_CONTENT = '';
21 our %DEFAULT_HEADER = (
22 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
23 'Accept-Charset' => 'gb2312,utf-8;q=0.7,*;q=0.7',
24 'Accept-Encoding' => "gzip",
25 'Accept-Language' => 'zh,zh-cn;q=0.8,en-us;q=0.5,en;q=0.3',
26 'Connection' => 'keep-alive',
27 #'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0',
28 'User-Agent' => 'User-Agent: MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
29 'DNT' => 1,
30 );
31
32 sub new {
33 my ( $self, %opt ) = @_;
34 $opt{retry} ||= 5;
35 $opt{max_process_num} ||= 5;
36 $opt{agent} ||= 'default';
37
38 $opt{browser_agent} ||= init_browser_agent( \%opt );
39
40 bless {%opt}, __PACKAGE__;
41 }
42
43 sub init_browser_agent {
44 my ( $o ) = @_;
45
46 $o->{browser_headers} ||= {};
47 my %h = ( %DEFAULT_HEADER, %{$o->{browser_headers}} );
48
49 my $cookie_jar = HTTP::CookieJar->new;
50 my $agent = HTTP::Tiny->new(
51 default_headers => \%h,
52 cookie_jar => $cookie_jar,
53 );
54
55 return $agent;
56 }
57
58 sub request_url {
59 my ( $self, $url, $form ) = @_;
60 return $DEFAULT_URL_CONTENT unless ( $url );
61
62 ### $url
63
64 my $c;
65 for my $i ( 1 .. $self->{retry} ) {
66 if ( $self->{agent} eq 'firefox' ) {
67 $c = $self->request_url_firefox($url);
68 }elsif($self->{agent} eq 'chrome') {
69 $c = $self->request_url_chrome($url);
70 }else{
71 $c = $self->request_url_tiny($url);
72 }
73 last if ( $c );
74 sleep 2;
75 }
76
77 return $DEFAULT_URL_CONTENT unless($c);
78
79 my $charset = detect( $c );
80 $c = decode( $charset, $c, Encode::FB_XMLCREF );
81
82 return $c;
83 }
84
85
86 sub request_url_firefox {
87 my ($self, $url) = @_;
88 my $firefox = Firefox::Marionette->new()->go($url);
89 sleep 5;
90 my $c = $firefox->html();
91 return $c;
92 }
93
94 sub request_url_chrome {
95 my ($self, $url) = @_;
96 my $c = `chrome --no-sandbox --user-data-dir --headless --disable-gpu --dump-dom "$url" 2>/dev/null`;
97 return $c;
98 }
99
100 sub request_url_tiny {
101 my ( $self, $url, $form ) = @_;
102
103 my $res;
104 if ( $form ) {
105 $res = $self->{browser_agent}->request(
106 'POST', $url,
107 { content => $self->post_form_data( $form ),
108 headers => {
109 'content-type' => 'application/x-www-form-urlencoded',
110 },
111 } );
112 } else {
113 $res = $self->{browser_agent}->get( $url );
114 }
115
116
117 return unless ( $res->{success} );
118
119 my $content = $res->{content};
120
121
122 my $html;
123 if (
124 $res->{headers}
125 and $res->{headers}{'content-encoding'}
126 and $res->{headers}{'content-encoding'} eq 'gzip' ) {
127 gunzip \$content => \$html, MultiStream => 1, Append => 1;
128 }
129 $content = $html || $content;
130
131
132 return $content;
133 } ## end sub request_url_tiny
134
135 sub post_form_data {
136 my ( $self, $form ) = @_;
137
138 return $form unless ( ref( $form ) eq 'HASH' );
139
140 my @params;
141 while ( my ( $k, $v ) = each %$form ) {
142 push @params, uri_escape( $k ) . "=" . uri_escape( $v );
143 }
144
145 my $post_str = join( "&", @params );
146 return $post_str;
147 }
148
149 sub read_moz_cookie {
150 my ( $self, $cookie, $dom ) = @_;
151
152 my @segment;
153 if ( -f $cookie and $cookie =~ /\.sqlite$/ ) { # firefox sqlite3
154 my $sqlite3_cookie =
155 `sqlite3 "$cookie" "select host,isSecure,path,isHttpOnly,expiry,name,value from moz_cookies where baseDomain='$dom'"`;
156 @segment = map { [ split /\|/ ] } split /\n/, $sqlite3_cookie;
157 } elsif ( -f $cookie and $cookie =~ /\.txt$/ ) { # Netscape HTTP Cookie File
158 my @ck = slurp( $cookie );
159 @segment = grep { $_->[0] and $_->[0] =~ /(^|\.)\Q$dom\E$/ } map { [ split /\s+/ ] } @ck;
160 } else { # cookie string : name1=value1; name2=value2
161 my @ck = split /;\s*/, $cookie;
162 @segment = map { my @c = split /=/; [ $dom, undef, '/', undef, 0, $c[0], $c[1] ] } @ck;
163 }
164
165 @segment = grep { defined $_->[6] and $_->[6] =~ /\S/ } @segment;
166
167 my @jar = map { "$_->[5]=$_->[6]; Domain=$_->[0]; Path=$_->[2]; Expiry=$_->[4]" } @segment;
168 $self->{browser_agent}{cookie_jar}->load_cookies( @jar );
169
170 $cookie = join( "; ", map { "$_->[5]=$_->[6]" } @segment );
171
172 #$self->{browser}{cookie_jar}{cookie} = $cookie;
173
174 return $cookie;
175
176 } ## end sub read_moz_cookie
177
178 1;
« Robot.pm ↑ Summary