blib/lib/WWW/Netflix.pm

filter: Statementstmt 15.0% 11 / 73 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 27.2% 3 / 11 Podpod 100% 8 / 8 totaltotal 15.4% 22 / 142 CC 24 SCAR 33.3
CC
24
Cov
9%
getQueue
56.0
login
42.0
getRatings
30.0
CRAP 27.9
↑ Summary
1 package WWW::Netflix;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = 0.05;
9  
10 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
11 0 my $ref = shift;
12 0 ▶ my $class = ref( $ref ) || $ref;
Condition0%0/3
ref $ref || $ref
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $ref || $ref
ref $ref $ref
Truth table0%0/3
A || B
A= ref $refB= $ref
ABresult
1X1
011
000
13  
14 0 my $self = bless {
15 www => new WWW::Mechanize(),
16 }, $class;
17  
18 0 return $self;
19 }
20  
21 0 ▶sub login {
Subroutine login: not called CC=6 SCAR=37.4
Pod documented
22 0 my ( $self, $user, $pass ) = @_;
23 0 ▶ die "Netflix requires a username and password"
Branch: 1 branch
BranchTrueFalseTotal
unless $user and $pass 0 0 0
Condition0%0/3
$user and $pass
!ll&&!rl&&r
000
MC/DC0%0/2
$user and $pass
$user $pass
Truth table0%0/3
A and B
A= $userB= $pass
ABresult
0X0
100
111
24 unless ( $user && $pass );
25  
26 0 $self->{ www }->get('http://www.netflix.com/Login');
27 die "couldn't find login form"
28 0 ▶ unless ( $self->{ www }->content =~ /login_form/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"www"}->content =~ /login_form/ 0 0 0
29
30 0 $self->{ www }->form_name( 'login_form' );
31 $self->{ www }->set_fields(
32 0 email => $user,
33 password1 => $pass,
34 );
35 0 $self->{ www }->submit();
36 die "login incorrect\n"
37 0 ▶ if ( $self->{ www }->content =~ /does not match an account/ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"www"}->content =~ /does not match an account/ 0 0 0
38 0 $self->{ www }->get("http://www.netflix.com");
39 die ( "not logged in" )
40 0 ▶ unless ( $self->{ www }->content =~ /Your Account/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"www"}->content =~ /Your Account/ 0 0 0
41 }
42  
43 0 ▶sub getRatings {
Subroutine getRatings: not called CC=5 SCAR=34.0
Pod documented
44 0 my ( $self ) = @_;
45  
46 0 my $movies;
47 0 my $body = 'alt="Next"';
48 0 my $cur = 1;
49  
50 # loop through each page of ratings
51 # the page that is being checked for a 'Next' button
52 # is not the page that is being scraped
53 0 ▶ while ( $body =~ /alt="Next"/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /alt="Next"/i) 0 0 0
54 0 $self->{ www }->get( "http://www.netflix.com/MoviesYouveSeen?lnkctr=wizMovUC&pageNum=$cur" );
55  
56 # the page gets returned even if not logged in or
57 # if you don't have any ratings,
58 # but this text doesn't appear unless you are logged in
59 # and you have ratings
60 return
61 0 ▶ unless ( $self->{ www }->content =~ /this is the list of movies you've seen/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"www"}->content =~ /this is the list of movies you've seen/ 0 0 0
62 0 $body = $self->{ www }->content();
63
64 # loop through each movie on the page
65 # id="stM60025026_1" class=star alt="5.0 Stars">
66 0 ▶ while ( $body =~ /id="stM(\d+)_[^ ]+ class=star alt="(\d).0 Stars/gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /id="stM(\d+)_[^ ]+ class=star alt="(\d).0 Stars/gs) 0 0 0
67 0 my ( $movie_id, $rating ) = ( $1, $2 );
68 0 $movies->{ $movie_id }->{ rating } = $rating;
69 } # end of looping through movies
70
71 # <a href="http://www.netflix.com/Movie/Adaptation/60025026&trkid=135440">Adaptation</a>
72 0 ▶ while ( $body =~ /<a href="[^>]+\/Movie\/([^\/]+)\/(\d+)\D/gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ m[<a href="[^>]+/Movie/([^/]+)/(\d+)\D]gs) 0 0 0
73 0 $movies->{ $2 }->{ title } = $1;
74 }
75 0 ++$cur;
76 } # end of looping through pages
77  
78 # return all of the ratings as a hashref
79 0 return $self->{ rated_movies } = $movies;
80 }
81  
82 0 ▶sub getRating {
Subroutine getRating: not called CC=2 SCAR=17.9
Pod documented
83 0 my ( $self, $movie_id ) = @_;
84 0 ▶ $self->getRatings unless ( exists $self->{ rated_movies } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"rated_movies"} 0 0 0
85 0 return $self->{ rated_movies }{ $movie_id }{ rating };
86 }
87  
88 0 ▶sub getTitle {
Subroutine getTitle: not called CC=2 SCAR=17.9
Pod documented
89 0 my ( $self, $movie_id ) = @_;
90 0 ▶ $self->getRatings unless ( exists $self->{ rated_movies } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"rated_movies"} 0 0 0
91 0 return $self->{ rated_movies }{ $movie_id }{ title };
92 }
93  
94  
95 0 ▶sub setRating {
Subroutine setRating: not called CC=4 SCAR=30.0
Pod documented
96 0 my ( $self, $movie_id, $rating ) = @_;
97  
98 0 ▶ die 'movie id must be an integer'
Branch: 1 branch
BranchTrueFalseTotal
unless $movie_id =~ /^\d+$/ 0 0 0
99 unless ( $movie_id =~ /^\d+$/ );
100 0 ▶ die 'rating must be an integer between 1 and 5'
Branch: 1 branch
BranchTrueFalseTotal
unless $rating =~ /^[1-5]$/ 0 0 0
101 unless ( $rating =~ /^[1-5]$/ );
102  
103 # my $uri = "http://www.netflix.com/SetRating?movieid=${movie_id}&value=${rating}&url=http%3A%2F%2Fwww.netflix.com%2FMovieDisplay%3Fmovieid%3D${movie_id}";
104 0 my $uri = "http://www.netflix.com/SetRating?movieid=${movie_id}&value=${rating}";
105 0 $self->{ www }->get( $uri );
106
107 # keep our hash up to date with reality
108 0 ▶ $self->{ rated_movies }{ $movie_id }{ rating } = $rating if ( defined $self->{ rated_movies } );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"rated_movies"} 0 0 0
109 }
110  
111  
112 0 ▶sub getQueue {
Subroutine getQueue: not called CC=7 SCAR=40.3
Pod documented
113 0 my ( $self ) = @_;
114  
115 0 $self->{www}->get( 'http://www.netflix.com/Queue' );
116 0 my $body = $self->{www}->content();
117  
118 # this relies on the current state of the HTML where
119 # each of the headers and movies is on its own line
120 0 $self->{ queue }{ home } = [];
121 0 $self->{ queue }{ queued } = [];
122 0 $self->{ queue }{ saved } = [];
123 0 my $section = 'throwaway';
124 0 for my $line ( split "\n", $body ) {
125 0 chomp $line;
126 0 ▶ if ( $line =~ m{<span class="dvd_hdr">DVDs</span> At Home} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ m[<span class="dvd_hdr">DVDs</span> At Home]) { } 0 0 0
elsif ($line =~ m[<span class="dvd_hdr">DVD</span> Queue]) { } 0 0 0
elsif ($line =~ /Saved <span class="dvd_hdr">DVDs/) { } 0 0 0
elsif ($line =~ m[<a href="http://www.netflix.com/Movie/\w+/(\d+)[^>]+>([^<]+)<]) { } 0 0 0
127 0 $section = 'home';
128 }
129 elsif ( $line =~ m{<span class="dvd_hdr">DVD</span> Queue} ) {
130 0 $section = 'queued';
131 }
132 elsif ( $line =~ m{Saved <span class="dvd_hdr">DVDs} ) {
133 0 $section = 'saved';
134 }
135 elsif ( $line =~ m{<a href="http://www.netflix.com/Movie/\w+/(\d+)[^>]+>([^<]+)<} ) {
136 # $1 is movie ID; $2 is movie title
137 0 ▶ push @{ $self->{ queue }{ $section } }, [ $1 => $2 ]
Branch: 1 branch
BranchTrueFalseTotal
unless $section eq "throwaway" 0 0 0
138 unless ( $section eq 'throwaway' ); # throwaway = recently watched movies
139 }
140 }
141  
142 # er, returning a hashref that the using code could modify ... that's a no-no.
143 0 return $self->{ queue };
144 }
145  
146 0 ▶sub queueMovie {
Subroutine queueMovie: not called CC=3 SCAR=24.8
Pod documented
147 0 my ( $self, $id ) = @_;
148  
149 0 ▶ die "Movie ID must be a series of one or more numerical digits.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /^\d+$/ 0 0 0
150 unless $id =~ /^\d+$/;
151  
152 #http://www.netflix.com/AddToQueue?movieid=70027897&ftype=DD&trkid=199898
153
154 0 my $url = "http://www.netflix.com/AddToQueue?movieid=$id";
155 0 $self->{ www }->get( $url );
156  
157 0 ▶ return $self->{ www }->uri() =~ /QueueAddConfirmation/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'www'}->uri =~ /QueueAddConfirmation/ ? : 0 0 0
158 }
159  
160  
161  
162 3 1;
163  
164 __END__
↑ Summary