package Plugins::NetFlix; # # Here is the url for Crypt::SSLeay. You need to install it # and install the Perl modules. You can find the Perl module # install in the perl folder under the OpenSSL install. # http://www.slproweb.com/download/Win32OpenSSL-v0.9.7d.exe # use strict; use lib '\Perl\site\lib'; use LWP::UserAgent (); use HTTP::Cookies (); use URI::Escape (); use HTML::Parser (); use Slim::Utils::Prefs (); my $EMAIL = Slim::Utils::Prefs::get( 'netflix_email') || ''; my $PASSWD = Slim::Utils::Prefs::get( 'netflix_passwd') || ''; my $MAX_MOVIES = Slim::Utils::Prefs::get( 'netflix_maxmov') || '12'; my $UA = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"; my $SETUP = "http://www.netflix.com/Login"; my $LOGIN = "https://www.netflix.com/Login"; my $QUEUE = "http://www.netflix.com/Queue"; my @mainMenu = ( 'Movies Checked Out', 'Movies In Queue', 'Movies Awaiting Release'); my $mainMenuNum = 0; my @movieList = (); my $movieListNum = 0; my $lastLoad = 0; # # Movie menu code. # my %mainFunctions = ( 'up' => sub { my $client = shift; $mainMenuNum = Slim::Buttons::Common::scroll( $client, -1, $#mainMenu + 1, $mainMenuNum); $client->update(); }, 'down' => sub { my $client = shift; $mainMenuNum = Slim::Buttons::Common::scroll( $client, 1, $#mainMenu + 1, $mainMenuNum); $client->update(); }, 'left' => sub { my $client = shift; Slim::Buttons::Common::popModeRight( $client); }, 'right' => sub { my $client = shift; Slim::Buttons::Common::pushModeLeft( $client, 'listMoviesMode'); }, ); sub getFunctions { return \%mainFunctions; } sub menuLines { my $client = shift; my $line1 = "NetFlix (" . ($mainMenuNum+1) . ' of ' . ($#mainMenu+1) . ")"; my $line2 = $mainMenu[$mainMenuNum] . " ( " . ($#{$movieList[$mainMenuNum]}+1) . " movies)"; return( $line1, $line2, '', Slim::Hardware::VFD::symbol('rightarrow')); } # # Movie list code. # my %movieListFunctions = ( 'up' => sub { my $client = shift; $movieListNum = Slim::Buttons::Common::scroll( $client, -1, $#{$movieList[$mainMenuNum]} + 1, $movieListNum); $client->update(); }, 'down' => sub { my $client = shift; $movieListNum = Slim::Buttons::Common::scroll( $client, 1, $#{$movieList[$mainMenuNum]} + 1, $movieListNum); $client->update(); }, 'left' => sub { my $client = shift; Slim::Buttons::Common::popModeRight( $client); }, ); sub movieListLines { my $client = shift; my $line1 = ($movieListNum+1) . " = " . $movieList[$mainMenuNum][$movieListNum]; my $line2 = ($movieListNum+2) . " = " . $movieList[$mainMenuNum][$movieListNum+1] if $movieList[$mainMenuNum][$movieListNum+1]; return( $line1, $line2, '', ''); } my $movieListMode = sub { my $client = shift; $movieListNum = 0; $client->lines( \&movieListLines); }; my $leaveMovieListMode = sub { my $client = shift; setMode( $client); }; # # Web interface stuffs. # sub webPages { my %pages = ( "index\.(?:htm|xml)" => \&handleIndex, ); return( \%pages, "index.html"); } sub handleIndex { my $client = shift; my $args = shift; if( defined $args->{url_query}) { (my $qs = $args->{url_query}) =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; my %m = map { my($a,$b) = split( '=', $_); "$a", "$b"} split( '&', $qs); Slim::Utils::Prefs::set( 'netflix_email', $m{email}) if defined $m{email}; Slim::Utils::Prefs::set( 'netflix_passwd', $m{passwd}) if defined $m{passwd}; Slim::Utils::Prefs::set( 'netflix_maxmov', $m{maxmov}) if defined $m{maxmov}; Slim::Utils::Prefs::writePrefs(); } $EMAIL = Slim::Utils::Prefs::get( 'netflix_email') || ''; $PASSWD = Slim::Utils::Prefs::get( 'netflix_passwd') || ''; $MAX_MOVIES = Slim::Utils::Prefs::get( 'netflix_maxmov') || '12'; my $blah = q{
NetFlix Email
NetFlix Passwd
Max # Movies
}; $blah =~ s/%EMAIL%/$EMAIL/ if defined $EMAIL; $blah =~ s/%PASSWD%/$PASSWD/ if defined $PASSWD; $blah =~ s/%MAXMOV%/$MAX_MOVIES/ if defined $MAX_MOVIES; return \$blah; } # # Load and parse the move queue from NetFlix. # sub loadQueue { my $client = shift; return if $lastLoad != 0; Slim::Display::Animation::showBriefly( $client, 'Loading NetFlix Queue') if defined $client; # # Setup the user agent. # my $cookies = new HTTP::Cookies; my $ua = new LWP::UserAgent; $ua->agent( $UA); $ua->cookie_jar( $cookies); # # Make a request to Login to get the session and cookies all setup. # my $req = new HTTP::Request( 'GET', $SETUP); $req->header( 'Accept' => '*/*'); $req->header( 'Accept-Language' => 'en-us'); my $res = $ua->request( $req); print $res->content, "\n" if $res->code != 200; # # Log in. # my $QS = "email=$EMAIL&password1=$PASSWD&nextpage=http://www.netflix.com/Queue&movieid=&trkid=&SubmitButton=Click Here to Continue"; $req = new HTTP::Request( 'GET', "$LOGIN?$QS"); $req->header( 'Accept' => '*/*'); $req->header( 'Accept-Language' => 'en-us'); $res = $ua->request( $req); print $res->content, "\n" if $res->code != 200; # # Fetch the queue. # $req = new HTTP::Request( 'GET', $QUEUE); $req->header( 'Accept' => '*/*'); $req->header( 'Accept-Language' => 'en-us'); $res = $ua->request( $req); print $res->content, "\n" if $res->code != 200; # # Parse out the movie lists. # my $p = MyParser->new; $p->parse( $res->content); $lastLoad = time(); } # # Queue html parser. # { package MyParser; use strict; use base 'HTML::Parser'; my $dump = 0; my $movieName; my $state = 0; my $indx; sub start { my($self, $tagname, $attr, $attrseq, $origtext) = @_; $dump = 1 if $tagname eq "a" && defined $attr->{href} && $attr->{href} =~ /movieid/; if( defined $attr->{bgcolor}) { $state = 0 if $attr->{bgcolor} eq "#B3CADE"; $state = 1 if $attr->{bgcolor} eq "#FFFFFF"; $state = 2 if $attr->{bgcolor} eq "#B8CC7C"; } } sub end { my($self, $tagname, $origtext) = @_; if( $tagname eq "a" && $dump) { $dump = 0; $movieName = '', return if ($#{$movieList[$state]} + 1) >= $MAX_MOVIES; $movieName =~ s/\s+/ /sg; $movieName =~ s/&/&/sg; return if length( $movieName) == 0; push( @{$movieList[$state]}, $movieName); $movieName = ''; } } sub text { my($self, $origtext, $is_cdata) = @_; $movieName .= $origtext if $dump; } } sub getDisplayName { return "NetFlix"; } sub setMode { my $client = shift; loadQueue( $client); $client->lines( \&menuLines); } Slim::Buttons::Common::addMode( 'listMoviesMode', \%movieListFunctions, $movieListMode, $leaveMovieListMode); #loadQueue(); 1;