Ticket #756: netflix.pl

File netflix.pl, 4.1 KB (added by anonymous, 15 years ago)
Line 
1#!/usr/bin/perl -w
2
3use LWP::Debug qw(+);  # DEBUG
4use LWP::UserAgent;
5use HTTP::Cookies;
6
7
8use vars qw($opt_h $opt_d $opt_v $opt_i $opt_A $opt_R $opt_L $opt_r $opt_1);
9use Getopt::Std;
10
11
12
13$title = "NetFlix";
14$version = "v1.0";
15$author = "John Petrocik";
16
17# display usage
18sub usage
19{
20print "usage: $0 -hviMPD [parameters]\n";
21print "       -h           help\n";
22print "       -v           display version\n";
23print "       -i           display info\n";
24print "       -d           debug\n";
25print "\n";
26print "       -L <userid> <password> login into Netflix. Only needed once, ever.\n";
27print "       -A <movieid> <trkid> adds movie to queue\n";
28print "       -R <movieid> removes movie to queue\n";
29print "       -1 <movieid> move movie to top of queue\n";
30exit(-1);
31}
32
33# display 1-line of info that describes the version of the program
34sub version
35{
36print "$title ($version) by $author\n"
37}
38
39# display 1-line of info that can describe the type of query used
40sub info
41{
42print "Performs Netflix queue management.\n";
43}
44
45# display detailed help
46sub help
47{
48version();
49info();
50usage();
51}
52
53
54# sets up the user agent for requests
55sub userAgent
56{
57    $ua = LWP::UserAgent->new;
58    $ua->agent('Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.0 (like Gecko)');
59#   $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt",
60#                                        autosave => 1));
61    $ua->cookie_jar(HTTP::Cookies::Netscape->new(file => "$ENV{\"HOME\"}/.mythtv/MythFlix/netflix.cookies",
62                                        autosave => 1));
63
64    push @{ $ua->requests_redirectable }, 'POST';
65
66    return $ua;
67}
68
69# login into netflix a stores cookie for future requests
70sub login
71{
72
73    $ua = &userAgent();
74    $res = $ua->post("https://www.netflix.com/Login", [
75                                                nextpage=>'http://www.netflix.com/Default',
76                                                email=>$userid,
77                                                movieid=>'trkid',
78                                                password1=>$password,
79                                                RememberMe=>'True',
80                                                SubmitButton=>'Click Here to Continue']);
81
82    &process($res);
83
84}
85
86# adds movie to queue
87sub addToQueue
88{
89   
90    $url = "http://www.netflix.com/AddToQueue?movieid=$movieid&trkid=$trkid";
91    if (defined $opt_d) {printf("AddToQueue: $url\n");}
92   
93    $ua = &userAgent();
94    $res = $ua->get($url);
95   
96    &process($res);
97}
98
99# removes movie from queue
100sub removeFromQueue
101{
102
103    $ua = &userAgent();
104    $res = $ua->post("http://www.netflix.com/Queue", [
105                                                    "R$movieid"=>'on',
106                                                    updateQueueBtn=>'Update+Your+Queue']);
107    &process($res);
108}
109
110# moves movie to top of queue
111sub moveToTop
112{
113   
114    $url = "http://www.netflix.com/MoveToTop?movieid=$movieid&lnkctr=qmtt&fromq=true";
115    if (defined $opt_d) {printf("MoveToTop: $url\n");}
116   
117    $ua = &userAgent();
118    $res = $ua->get($url);
119   
120    &process($res);
121}
122
123sub process
124{
125   
126    print "Status Code: " ,$res->code(), "\n";
127   
128}
129
130
131#
132# Main Program
133#
134
135# parse command line arguments
136getopts('hvidALR1');
137
138# print out info
139if (defined $opt_v) { version(); exit 1; }
140if (defined $opt_i) { info(); exit 1; }
141
142# print out usage if needed
143if (defined $opt_h || $#ARGV<0) { help(); }
144
145if (defined $opt_L)
146{
147    # take movieid from cmdline arg
148    $userid = shift || die "Usage : $0 -L <userid> <password>\n";
149    $password = shift || die "Usage : $0 -L <userid> <password>\n";
150    &login($userid,$password);
151}
152
153if (defined $opt_A)
154{
155    # take movieid from cmdline arg
156    $movieid = shift || die "Usage : $0 -A <movieid> <trkid>\n";
157    $trkid = shift || die "Usage : $0 -A <movieid> <trkid>\n";
158    &addToQueue($movieid,$trkid);
159}
160
161if (defined $opt_R)
162{
163    # take movieid from cmdline arg
164    $movieid = shift || die "Usage : $0 -R <movieid>";
165    &removeFromQueue($movieid);
166}
167
168if (defined $opt_1)
169{
170    # take movieid from cmdline arg
171    $movieid = shift || die "Usage : $0 -1 <movieid>";
172    &moveToTop($movieid);
173}
174
175