File Coverage

blib/lib/WWW/Netflix.pm
Criterion Covered Total %
statement 9 71 12.6
branch 0 34 0.0
condition 0 6 0.0
subroutine 3 11 27.2
pod 8 8 100.0
total 20 130 15.3


line stmt bran cond sub pod time code
1             package WWW::Netflix;
2              
3 3     3   100296 use strict;
  3         7  
  3         104  
4 3     3   15 use warnings;
  3         5  
  3         79  
5              
6 3     3   4354 use WWW::Mechanize;
  3         860522  
  3         5843  
7              
8             our $VERSION = 0.05;
9              
10             sub new {
11 0     0 1   my $ref = shift;
12 0   0       my $class = ref( $ref ) || $ref;
13              
14 0           my $self = bless {
15             www => new WWW::Mechanize(),
16             }, $class;
17              
18 0           return $self;
19             }
20              
21             sub login {
22 0     0 1   my ( $self, $user, $pass ) = @_;
23 0 0 0       die "Netflix requires a username and password"
24             unless ( $user && $pass );
25              
26 0           $self->{ www }->get('http://www.netflix.com/Login');
27             die "couldn't find login form"
28 0 0         unless ( $self->{ www }->content =~ /login_form/ );
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 0         if ( $self->{ www }->content =~ /does not match an account/ );
38 0           $self->{ www }->get("http://www.netflix.com");
39             die ( "not logged in" )
40 0 0         unless ( $self->{ www }->content =~ /Your Account/ );
41             }
42              
43             sub getRatings {
44 0     0 1   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 ) {
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 0         unless ( $self->{ www }->content =~ /this is the list of movies you've seen/ );
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 ) {
67 0           my ( $movie_id, $rating ) = ( $1, $2 );
68 0           $movies->{ $movie_id }->{ rating } = $rating;
69             } # end of looping through movies
70            
71             # Adaptation
72 0           while ( $body =~ / 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             sub getRating {
83 0     0 1   my ( $self, $movie_id ) = @_;
84 0 0         $self->getRatings unless ( exists $self->{ rated_movies } );
85 0           return $self->{ rated_movies }{ $movie_id }{ rating };
86             }
87              
88             sub getTitle {
89 0     0 1   my ( $self, $movie_id ) = @_;
90 0 0         $self->getRatings unless ( exists $self->{ rated_movies } );
91 0           return $self->{ rated_movies }{ $movie_id }{ title };
92             }
93              
94              
95             sub setRating {
96 0     0 1   my ( $self, $movie_id, $rating ) = @_;
97              
98 0 0         die 'movie id must be an integer'
99             unless ( $movie_id =~ /^\d+$/ );
100 0 0         die 'rating must be an integer between 1 and 5'
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 0         $self->{ rated_movies }{ $movie_id }{ rating } = $rating if ( defined $self->{ rated_movies } );
109             }
110              
111              
112             sub getQueue {
113 0     0 1   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 0         if ( $line =~ m{DVDs At Home} ) {
    0          
    0          
    0          
127 0           $section = 'home';
128             }
129             elsif ( $line =~ m{DVD Queue} ) {
130 0           $section = 'queued';
131             }
132             elsif ( $line =~ m{Saved DVDs} ) {
133 0           $section = 'saved';
134             }
135             elsif ( $line =~ m{ 136             # $1 is movie ID; $2 is movie title
137 0 0         push @{ $self->{ queue }{ $section } }, [ $1 => $2 ]
  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             sub queueMovie {
147 0     0 1   my ( $self, $id ) = @_;
148              
149 0 0         die "Movie ID must be a series of one or more numerical digits.\n"
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 0         return $self->{ www }->uri() =~ /QueueAddConfirmation/ ? 1 : 0;
158             }
159              
160              
161              
162             1;
163              
164             __END__