-
Notifications
You must be signed in to change notification settings - Fork 3
/
netflix.py
510 lines (429 loc) · 18.3 KB
/
netflix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#
# Library for accessing the REST API from Netflix
# Represents each resource in an object-oriented way
#
import sys
import os.path
import re
import oauth.oauth as oauth
import httplib
import simplejson
from urlparse import urlparse
HOST = 'api.netflix.com'
PORT = '80'
REQUEST_TOKEN_URL = 'http://api.netflix.com/oauth/request_token'
ACCESS_TOKEN_URL = 'http://api.netflix.com/oauth/access_token'
AUTHORIZATION_URL = 'https://api-user.netflix.com/oauth/login'
class NetflixError(Exception):
pass
class NetflixUser():
def __init__(self, user, client):
self.request_token_url = REQUEST_TOKEN_URL
self.access_token_url = ACCESS_TOKEN_URL
self.authorization_url = AUTHORIZATION_URL
self.access_token = oauth.OAuthToken(user['access']['key'],
user['access']['secret'])
self.client = client
self.data = None
def get_request_token(self):
client = self.client
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
client.consumer,
http_url=self.request_token_url)
oauth_request.sign_request(client.signature_method_hmac_sha1,
client.consumer,
None)
client.connection.request(oauth_request.http_method,
self.request_token_url,
headers=oauth_request.to_header())
response = client.connection.getresponse()
request_token = oauth.OAuthToken.from_string(response.read())
params = {'application_name': client.CONSUMER_NAME,
'oauth_consumer_key': client.CONSUMER_KEY}
oauth_request = oauth.OAuthRequest.from_token_and_callback(
token=request_token,
callback=client.CONSUMER_CALLBACK,
http_url=self.authorization_url,
parameters=params)
return (request_token, oauth_request.to_url())
def get_access_token(self, request_token):
client = self.client
if not isinstance(request_token, oauth.OAuthToken):
request_token = oauth.OAuthToken(
request_token['key'],
request_token['secret'])
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
client.consumer,
token=request_token,
http_url=self.access_token_url)
oauth_request.sign_request(client.signature_method_hmac_sha1,
client.consumer,
request_token)
client.connection.request(oauth_request.http_method,
self.access_token_url,
headers=oauth_request.to_header())
response = client.connection.getresponse()
access_token = oauth.OAuthToken.from_string(response.read())
return access_token
def get_data(self):
access_token = self.access_token
if not isinstance(access_token, oauth.OAuthToken):
access_token = oauth.OAuthToken(access_token['key'],
access_token['secret'])
request_url = '/users/%s' % (access_token.key)
info = simplejson.loads(self.client._get_resource(request_url,
token=access_token))
self.data = info['user']
return self.data
def get_info(self, field):
access_token = self.access_token
if not self.data:
self.get_data()
fields = []
url = ''
for link in self.data['link']:
fields.append(link['title'])
if link['title'] == field:
url = link['href']
if not url:
error_string = "Invalid or missing field. "
error_string += "Acceptable fields for this object are: "
error_string += "\n\n".join(fields)
print error_string
sys.exit(1)
try:
info = simplejson.loads(self.client._get_resource(
url, token=access_token))
except:
return []
else:
return info
def get_ratings(self, disc_info=[], urls=[]):
access_token = self.access_token
if not isinstance(access_token, oauth.OAuthToken):
access_token = oauth.OAuthToken(access_token['key'],
access_token['secret'])
request_url = '/users/%s/ratings/title' % (access_token.key)
if not urls:
if isinstance(disc_info, list):
for disc in disc_info:
urls.append(disc['id'])
else:
urls.append(disc_info['id'])
parameters = {'title_refs': ','.join(urls)}
info = simplejson.loads(self.client._get_resource(
request_url,
parameters=parameters,
token=access_token))
ret = {}
for title in info['ratings']['ratings_item']:
ratings = {
'average': title['average_rating'],
'predicted': title['predicted_rating'],
}
try:
ratings['user'] = title['user_rating']
except:
pass
ret[title['title']['regular']] = ratings
return ret
def get_rental_history(self, history_type=None, start_index=None,
max_results=None, updated_min=None):
access_token = self.access_token
parameters = {}
if start_index:
parameters['start_index'] = start_index
if max_results:
parameters['max_results'] = max_results
if updated_min:
parameters['updated_min'] = updated_min
if not isinstance(access_token, oauth.OAuthToken):
access_token = oauth.OAuthToken(access_token['key'],
access_token['secret'])
if not history_type:
request_url = '/users/%s/rental_history' % (access_token.key)
else:
request_url = '/users/%s/rental_history/%s' % (access_token.key,
history_type)
try:
info = simplejson.loads(self.client._get_resource(
request_url,
parameters=parameters,
token=access_token))
except:
return {}
return info
class NetflixCatalog():
def __init__(self, client):
self.client = client
def search_titles(self, term, start_index=None, max_results=None):
request_url = '/catalog/titles'
parameters = {'term': term}
if start_index:
parameters['start_index'] = start_index
if max_results:
parameters['max_results'] = max_results
info = simplejson.loads(self.client._get_resource(
request_url,
parameters=parameters))
return info['catalog_titles']['catalog_title']
def search_string_titles(self, term, start_index=None, max_results=None):
request_url = '/catalog/titles/autocomplete'
parameters = {'term': term}
if start_index:
parameters['start_index'] = start_index
if max_results:
parameters['max_results'] = max_results
info = simplejson.loads(self.client._get_resource(
request_url,
parameters=parameters))
print simplejson.dumps(info)
return info['autocomplete']['autocomplete_item']
def get_title(self, url):
request_url = url
info = simplejson.loads(self.client._get_resource(request_url))
return info
def search_people(self, term, start_index=None, max_results=None):
request_url = '/catalog/people'
parameters = {'term': term}
if start_index:
parameters['start_index'] = start_index
if max_results:
parameters['max_results'] = max_results
try:
info = simplejson.loads(self.client._get_resource(
request_url,
parameters=parameters))
except:
return []
return info['people']['person']
def get_person(self, url):
request_url = url
try:
info = simplejson.loads(self.client._get_resource(request_url))
except:
return {}
return info
class NetflixUserQueue:
def __init__(self, user):
self.user = user
self.client = user.client
self.tag = None
def get_contents(self, sort=None, start_index=None,
max_results=None, updated_min=None):
parameters = {}
if start_index:
parameters['start_index'] = start_index
if max_results:
parameters['max_results'] = max_results
if updated_min:
parameters['updated_min'] = updated_min
if sort and sort in ('queue_sequence', 'date_added', 'alphabetical'):
parameters['sort'] = sort
request_url = '/users/%s/queues' % (self.user.access_token.key)
try:
info = simplejson.loads(self.client._get_resource(
request_url,
parameters=parameters,
token=self.user.access_token))
except:
return []
else:
return info
def get_available(self, sort=None, start_index=None,
max_results=None, updated_min=None,
queueType='disc'):
parameters = {}
if start_index:
parameters['start_index'] = start_index
if max_results:
parameters['max_results'] = max_results
if updated_min:
parameters['updated_min'] = updated_min
if sort and sort in ('queue_sequence', 'date_added', 'alphabetical'):
parameters['sort'] = sort
request_url = '/users/%s/queues/%s/available' % (
self.user.access_token.key,
queueType)
try:
info = simplejson.loads(self.client._get_resource(
request_url,
parameters=parameters,
token=self.user.access_token))
except:
return []
else:
return info
def get_saved(self, sort=None, start_index=None,
max_results=None, updated_min=None,
queueType='disc'):
parameters = {}
if start_index:
parameters['start_index'] = start_index
if max_results:
parameters['max_results'] = max_results
if updated_min:
parameters['updated_min'] = updated_min
if sort and sort in ('queue_sequence', 'date_added', 'alphabetical'):
parameters['sort'] = sort
request_url = '/users/%s/queues/%s/saved' % (
self.user.access_token.key,
queueType)
try:
info = simplejson.loads(self.client._get_resource(
request_url,
parameters=parameters,
token=self.user.access_token))
except:
return []
else:
return info
def addTitle(self, disc_info=[], urls=[], queueType='disc', position=None):
access_token = self.user.access_token
parameters = {}
if position:
parameters['position'] = position
if not isinstance(access_token, oauth.OAuthToken):
access_token = oauth.OAuthToken(
access_token['key'],
access_token['secret'])
request_url = '/users/%s/queues/disc' % (access_token.key)
if not urls:
for disc in disc_info:
urls.append(disc['id'])
parameters['title_ref'] = ','.join(urls)
if not self.tag:
response = self.client._get_resource(
request_url,
token=access_token)
response = simplejson.loads(response)
self.tag = response["queue"]["etag"]
parameters['etag'] = self.tag
response = self.client._post_resource(
request_url,
token=access_token,
parameters=parameters)
return response
def removeTitle(self, id, queueType='disc'):
access_token = self.user.access_token
entryID = None
parameters = {}
if not isinstance(access_token, oauth.OAuthToken):
access_token = oauth.OAuthToken(
access_token['key'],
access_token['secret'])
# First, we gotta find the entry to delete
queueparams = {'max_results': 500}
request_url = '/users/%s/queues/disc' % (access_token.key)
response = self.client._get_resource(
request_url,
token=access_token,
parameters=queueparams)
print "Response is " + response
response = simplejson.loads(response)
titles = response["queue"]["queue_item"]
for disc in titles:
discID = os.path.basename(urlparse(disc['id']).path)
if discID == id:
entryID = disc['id']
if not entryID:
return
self.client._get_resource(entryID,
token=access_token,
parameters=parameters)
response = self.client._delete_resource(entryID, token=access_token)
return response
class NetflixDisc:
def __init__(self, disc_info, client):
self.info = disc_info
self.client = client
def get_info(self, field):
fields = []
url = ''
for link in self.info['link']:
fields.append(link['title'])
if link['title'] == field:
url = link['href']
if not url:
error_string = "Invalid or missing field. " + \
"Acceptable fields for this object are:" + \
"\n\n".join(fields)
print error_string
sys.exit(1)
try:
info = simplejson.loads(self.client._get_resource(url))
except:
return []
else:
return info
class NetflixClient:
def __init__(self, name, key, secret, callback='', verbose=False):
self.connection = httplib.HTTPConnection("%s:%s" % (HOST, PORT))
self.server = HOST
self.verbose = verbose
self.user = None
self.catalog = NetflixCatalog(self)
self.CONSUMER_NAME = name
self.CONSUMER_KEY = key
self.CONSUMER_SECRET = secret
self.CONSUMER_CALLBACK = callback
self.consumer = oauth.OAuthConsumer(
self.CONSUMER_KEY,
self.CONSUMER_SECRET)
self.signature_method_hmac_sha1 = \
oauth.OAuthSignatureMethod_HMAC_SHA1()
def _get_resource(self, url, token=None, parameters={}):
if not re.match('http', url):
url = "http://%s%s" % (HOST, url)
parameters['output'] = 'json'
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
self.consumer,
http_url=url,
parameters=parameters,
token=token)
oauth_request.sign_request(self.signature_method_hmac_sha1,
self.consumer,
token)
if (self.verbose):
print oauth_request.to_url()
self.connection.request('GET', oauth_request.to_url())
response = self.connection.getresponse()
return response.read()
def _post_resource(self, url, token=None, parameters=None):
if not re.match('http', url):
url = "http://%s%s" % (HOST, url)
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
self.consumer,
http_url=url,
parameters=parameters,
token=token,
http_method='POST')
oauth_request.sign_request(
self.signature_method_hmac_sha1,
self.consumer,
token)
if (self.verbose):
print "POSTING TO" + oauth_request.to_url()
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
self.connection.request('POST', url,
body=oauth_request.to_postdata(),
headers=headers)
response = self.connection.getresponse()
return response.read()
def _delete_resource(self, url, token=None, parameters=None):
if not re.match('http', url):
url = "http://%s%s" % (HOST, url)
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
self.consumer,
http_url=url,
parameters=parameters,
token=token,
http_method='DELETE')
oauth_request.sign_request(self.signature_method_hmac_sha1,
self.consumer,
token)
if (self.verbose):
print "DELETING FROM" + oauth_request.to_url()
self.connection.request('DELETE', oauth_request.to_url())
response = self.connection.getresponse()
return response.read()