1 | #!/usr/bin/perl -w |
---|
2 | |
---|
3 | use LWP::Debug qw(+); # DEBUG |
---|
4 | use LWP::UserAgent; |
---|
5 | use HTTP::Cookies; |
---|
6 | |
---|
7 | |
---|
8 | use vars qw($opt_h $opt_d $opt_v $opt_i $opt_A $opt_R $opt_L $opt_r $opt_1); |
---|
9 | use Getopt::Std; |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | $title = "NetFlix"; |
---|
14 | $version = "v1.0"; |
---|
15 | $author = "John Petrocik"; |
---|
16 | |
---|
17 | # display usage |
---|
18 | sub usage |
---|
19 | { |
---|
20 | print "usage: $0 -hviMPD [parameters]\n"; |
---|
21 | print " -h help\n"; |
---|
22 | print " -v display version\n"; |
---|
23 | print " -i display info\n"; |
---|
24 | print " -d debug\n"; |
---|
25 | print "\n"; |
---|
26 | print " -L <userid> <password> login into Netflix. Only needed once, ever.\n"; |
---|
27 | print " -A <movieid> <trkid> adds movie to queue\n"; |
---|
28 | print " -R <movieid> removes movie to queue\n"; |
---|
29 | print " -1 <movieid> move movie to top of queue\n"; |
---|
30 | exit(-1); |
---|
31 | } |
---|
32 | |
---|
33 | # display 1-line of info that describes the version of the program |
---|
34 | sub version |
---|
35 | { |
---|
36 | print "$title ($version) by $author\n" |
---|
37 | } |
---|
38 | |
---|
39 | # display 1-line of info that can describe the type of query used |
---|
40 | sub info |
---|
41 | { |
---|
42 | print "Performs Netflix queue management.\n"; |
---|
43 | } |
---|
44 | |
---|
45 | # display detailed help |
---|
46 | sub help |
---|
47 | { |
---|
48 | version(); |
---|
49 | info(); |
---|
50 | usage(); |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | # sets up the user agent for requests |
---|
55 | sub 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 |
---|
70 | sub 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 |
---|
87 | sub 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 |
---|
100 | sub 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 |
---|
111 | sub 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 | |
---|
123 | sub 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 |
---|
136 | getopts('hvidALR1'); |
---|
137 | |
---|
138 | # print out info |
---|
139 | if (defined $opt_v) { version(); exit 1; } |
---|
140 | if (defined $opt_i) { info(); exit 1; } |
---|
141 | |
---|
142 | # print out usage if needed |
---|
143 | if (defined $opt_h || $#ARGV<0) { help(); } |
---|
144 | |
---|
145 | if (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 | |
---|
153 | if (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 | |
---|
161 | if (defined $opt_R) |
---|
162 | { |
---|
163 | # take movieid from cmdline arg |
---|
164 | $movieid = shift || die "Usage : $0 -R <movieid>"; |
---|
165 | &removeFromQueue($movieid); |
---|
166 | } |
---|
167 | |
---|
168 | if (defined $opt_1) |
---|
169 | { |
---|
170 | # take movieid from cmdline arg |
---|
171 | $movieid = shift || die "Usage : $0 -1 <movieid>"; |
---|
172 | &moveToTop($movieid); |
---|
173 | } |
---|
174 | |
---|
175 | |
---|