File Coverage

lib/WWW/Mixi/Scraper/Mech.pm
Criterion Covered Total %
statement 18 58 31.0
branch 0 12 0.0
condition 0 10 0.0
subroutine 6 15 40.0
pod 8 8 100.0
total 32 103 31.0


line stmt bran cond sub pod time code
1             package WWW::Mixi::Scraper::Mech;
2            
3 1     1   820 use strict;
  1         2  
  1         33  
4 1     1   5 use warnings;
  1         2  
  1         24  
5 1     1   974 use Encode;
  1         14579  
  1         103  
6 1     1   1275 use WWW::Mechanize 1.50;
  1         171841  
  1         18  
7 1     1   50 use WWW::Mixi::Scraper::Utils qw( _uri );
  1         2  
  1         74  
8 1     1   1107 use Time::HiRes qw( sleep );
  1         1833  
  1         5  
9            
10             sub new {
11 0     0 1   my ($class, %options) = @_;
12            
13 0           my $email = delete $options{email};
14 0           my $password = delete $options{password};
15 0           my $next_url = delete $options{next_url};
16            
17 0   0       $options{agent} ||= "WWW-Mixi-Scraper/$WWW::Mixi::Scraper::VERSION";
18 0   0       $options{cookie_jar} ||= {};
19            
20 0           my $mech = WWW::Mechanize->new( %options );
21 0           my $self = bless {
22             mech => $mech,
23             login => {
24             email => $email,
25             password => $password,
26             next_url => $next_url,
27             sticky => 'on',
28             }
29             }, $class;
30            
31 0           $self;
32             }
33            
34             sub login {
35 0     0 1   my $self = shift;
36            
37 0           sleep(1.0); # intentional delay not to access too frequently
38            
39 0           $self->{mech}->post( 'http://mixi.jp/login.pl' => $self->{login} );
40            
41 0           $self->may_have_errors('Login failed');
42            
43             # warn "logged in to mixi";
44             }
45            
46             sub logout {
47 0     0 1   my $self = shift;
48            
49 0           $self->get('/logout.pl');
50            
51 0           $self->may_have_errors('Failed to logout');
52             }
53            
54             sub may_have_errors {
55 0     0 1   my $self = shift;
56            
57 0 0         $self->{mech}->success or $self->_error(@_);
58             }
59            
60             sub _error {
61 0     0     my ($self, $message) = @_;
62            
63 0   0       $message ||= 'Mech error';
64            
65 0           die "$message: ".$self->{mech}->res->status_line;
66             }
67            
68             sub get {
69 0     0 1   my ($self, $uri) = @_;
70            
71 0 0         $uri = _uri($uri) unless ref $uri eq 'URI';
72            
73 0           sleep(1.0); # intentional delay not to access too frequently
74            
75 0           $self->{mech}->get($uri);
76            
77             # adapted from Plagger::Plugin::CustomFeed::Mixi
78 0 0         if ( $self->content =~ /action="(http:\/\/mixi\.jp)?\/?login\.pl/ ) {
79             # shouldn't be path but path_query, obviously
80 0           $self->{login}->{next_url} = $uri->path_query;
81 0           $self->login;
82            
83             # meta refresh
84 0 0         if ( $self->content =~ /"0;url=(.*?)"/ ) {
85 0           $self->{mech}->get($1);
86             }
87             }
88 0           $self->{mech}->success;
89             }
90            
91             sub content {
92 0     0 1   my $self = shift;
93            
94 0           $self->{mech}->content;
95             }
96            
97             sub get_content {
98 0     0 1   my ($self, $uri, $encoding) = @_;
99            
100 0 0         my $content = $self->get($uri) ? $self->content : undef;
101            
102 0 0 0       if ( $content && $encoding ) {
103 0           $content = encode( $encoding => $content );
104             }
105 0           $content;
106             }
107            
108             sub uri {
109 0     0 1   my $self = shift;
110 0           $self->{mech}->uri;
111             }
112            
113             1;
114            
115             __END__