00001 #!/usr/bin/perl -w
00002
00003 # use LWP::Debug qw(+); # DEBUG
00004 use LWP::UserAgent;
00005 use HTTP::Cookies;
00006
00007
00008 use vars qw($opt_h $opt_d $opt_v $opt_i $opt_A $opt_R $opt_L $opt_r $opt_1 $opt_q);
00009 use Getopt::Std;
00010
00011
00012
00013 $title = "NetFlix";
00014 $version = "v1.0";
00015 $author = "John Petrocik";
00016
00017 # display usage
00018 sub usage
00019 {
00020 print "usage: $0 -hviMPD [parameters]\n";
00021 print " -h help\n";
00022 print " -v display version\n";
00023 print " -i display info\n";
00024 print " -q queue name\n";
00025 print " -d debug\n";
00026 print "\n";
00027 print " -L <userid> <password> login into Netflix. Only needed once, ever.\n";
00028 print " -A <movieid> adds movie to queue\n";
00029 print " -R <movieid> removes movie to queue\n";
00030 print " -1 <movieid> move movie to top of queue\n";
00031 exit(-1);
00032 }
00033
00034 # display 1-line of info that describes the version of the program
00035 sub version
00036 {
00037 print "$title ($version) by $author\n"
00038 }
00039
00040 # display 1-line of info that can describe the type of query used
00041 sub info
00042 {
00043 print "Performs Netflix queue management.\n";
00044 }
00045
00046 # display detailed help
00047 sub help
00048 {
00049 version();
00050 info();
00051 usage();
00052 }
00053
00054
00055 # sets up the user agent for requests
00056 sub userAgent
00057 {
00058 $ua = LWP::UserAgent->new;
00059 $ua->agent('Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.0 (like Gecko)');
00060 # $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt",
00061 # autosave => 1));
00062 $ua->cookie_jar(HTTP::Cookies::Netscape->new(file => "$ENV{\"HOME\"}/.mythtv/MythFlix/netflix.cookies$opt_q",
00063 autosave => 1));
00064
00065 push @{ $ua->requests_redirectable }, 'POST';
00066
00067 return $ua;
00068 }
00069
00070 # login into netflix a stores cookie for future requests
00071 sub login
00072 {
00073
00074 $ua = &userAgent();
00075 $res = $ua->post("https://www.netflix.com/Login", [
00076 nextpage=>'http://www.netflix.com/Default',
00077 email=>$userid,
00078 movieid=>'trkid',
00079 password1=>$password,
00080 RememberMe=>'True',
00081 SubmitButton=>'Click Here to Continue']);
00082
00083 &process($res);
00084
00085 }
00086
00087 # adds movie to queue
00088 sub addToQueue
00089 {
00090
00091 $url = "http://www.netflix.com/AddToQueue?movieid=$movieid";
00092 if (defined $opt_d) {printf("AddToQueue: $url\n");}
00093
00094 $ua = &userAgent();
00095 $res = $ua->get($url);
00096
00097 &process($res);
00098 }
00099
00100 # removes movie from queue
00101 sub removeFromQueue
00102 {
00103
00104 $ua = &userAgent();
00105 $res = $ua->post("http://www.netflix.com/Queue", [
00106 "R$movieid"=>'on',
00107 updateQueueBtn=>'Update+Your+Queue']);
00108 &process($res);
00109 }
00110
00111 # moves movie to top of queue
00112 sub moveToTop
00113 {
00114
00115 $url = "http://www.netflix.com/MoveToTop?movieid=$movieid&lnkctr=qmtt&fromq=true";
00116 if (defined $opt_d) {printf("MoveToTop: $url\n");}
00117
00118 $ua = &userAgent();
00119 $res = $ua->get($url);
00120
00121 &process($res);
00122 }
00123
00124 sub process
00125 {
00126
00127 print "Status Code: " ,$res->code(), "\n";
00128
00129 }
00130
00131
00132 #
00133 # Main Program
00134 #
00135
00136 # parse command line arguments
00137 getopts('hvidALR1q:');
00138
00139 # print out info
00140 if (defined $opt_v) { version(); exit 1; }
00141 if (defined $opt_i) { info(); exit 1; }
00142
00143 if (defined $opt_q) {
00144 $opt_q = "." . $opt_q;
00145 } else {
00146 $opt_q = "";
00147 }
00148
00149 # print out usage if needed
00150 if (defined $opt_h || $#ARGV<0) { help(); }
00151
00152 if (defined $opt_L)
00153 {
00154 # take movieid from cmdline arg
00155 $userid = shift || die "Usage : $0 -L <userid> <password>\n";
00156 $password = shift || die "Usage : $0 -L <userid> <password>\n";
00157 &login($userid,$password);
00158 }
00159
00160 if (defined $opt_A)
00161 {
00162 # take movieid from cmdline arg
00163 $movieid = shift || die "Usage : $0 -A <movieid>\n";
00164 &addToQueue($movieid);
00165 }
00166
00167 if (defined $opt_R)
00168 {
00169 # take movieid from cmdline arg
00170 $movieid = shift || die "Usage : $0 -R <movieid>";
00171 &removeFromQueue($movieid);
00172 }
00173
00174 if (defined $opt_1)
00175 {
00176 # take movieid from cmdline arg
00177 $movieid = shift || die "Usage : $0 -1 <movieid>";
00178 &moveToTop($movieid);
00179 }
00180
00181
00182