forked from shelfworthy/flixpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netflix.py
832 lines (701 loc) · 27.4 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
import sys, os.path, re, httplib, time, urllib2, gzip, StringIO, logging
import oauth.oauth as oauth
from xml.dom.minidom import parseString
from urlparse import urlparse
from datetime import datetime
try:
import simplejson
except ImportError:
from django.utils import simplejson
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'
TITLE_URL = 'http://schemas.netflix.com/catalog/title'
# parent URLS
SERIES_URL = 'http://schemas.netflix.com/catalog/titles.series'
SEASON_URL = 'http://schemas.netflix.com/catalog/title.season'
DISC_URL = 'http://schemas.netflix.com/catalog/title.disc'
# children URLs
SEASONS_URL = 'http://schemas.netflix.com/catalog/titles.seasons'
DISCS_URL = 'http://schemas.netflix.com/catalog/titles.discs'
EPISODES_URL = 'http://schemas.netflix.com/catalog/titles.programs'
disk_pattern = re.compile(':? ?([Vv]ol.? \d+|[dD]isc \d+):? ?')
log = logging.getLogger('py_netflix')
class NetflixError(Exception):
pass
class NetflixClient:
def __init__(self, name, key, secret, callback='',verbose=False):
self.connection = httplib.HTTPConnection(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 _getResource(self, url, token=None, parameters={}, xml=False):
if not re.match('http',url):
url = "http://%s%s" % (HOST, url)
if not xml:
parameters['output'] = 'json'
oauthRequest = oauth.OAuthRequest.from_consumer_and_token(
self.consumer,
http_url=url,
parameters=parameters,
token=token)
oauthRequest.sign_request(
self.signature_method_hmac_sha1,
self.consumer,
token)
if (self.verbose):
print oauthRequest.to_url()
headers = {'Accept-encoding':'gzip'}
req = urllib2.Request(oauthRequest.to_url(), None, headers)
response = urllib2.urlopen(req)
data = gzip.GzipFile(fileobj=StringIO.StringIO(response.read())).read()
return data
def _postResource(self, url, token=None, parameters=None):
if not re.match('http',url):
url = "http://%s%s" % (HOST, url)
oauthRequest = oauth.OAuthRequest.from_consumer_and_token(
self.consumer,
http_url=url,
parameters=parameters,
token=token,
http_method='POST')
oauthRequest.sign_request(
self.signature_method_hmac_sha1,
self.consumer,
token)
if (self.verbose):
print "POSTING TO", oauthRequest.to_url()
headers = {'Content-Type':'application/x-www-form-urlencoded',
'Accept-encoding':'gzip'}
data = oauthRequest.to_postdata()
req = urllib2.Request(oauthRequest.to_url(), data, headers)
try:
response = urllib2.urlopen(req)
except urllib2.HTTPError as e:
print e
print e.headers
raise e
data = gzip.GzipFile(fileobj=StringIO.StringIO(response.read())).read()
return data
def _deleteResource(self, url, token=None, parameters=None):
if not re.match('http',url):
url = "http://%s%s" % (HOST, url)
oauthRequest = oauth.OAuthRequest.from_consumer_and_token(
self.consumer,
http_url=url,
parameters=parameters,
token=token,
http_method='DELETE')
oauthRequest.sign_request(
self.signature_method_hmac_sha1,
self.consumer,
token)
if (self.verbose):
print "DELETING FROM" + oauthRequest.to_url()
self.connection.request('DELETE', oauthRequest.to_url())
response = self.connection.getresponse()
return response.read()
class NetflixBase(object):
''' This is the base netflix object class that User, Title, and Person are currently built on'''
def __init__(self, raw_json, client):
self.info = raw_json
self.client = client
def getInfo(self,field):
# try and get a token from the clients user object (if it exists)
try:
token = self.client.user.accessToken
except AttributeError:
token = None
fields = []
url = ''
for link in self.info['link']:
fields.append('title: %s | rel: %s' % (link['title'], link['rel']))
if link['title'] == field or link['rel'] == field:
url = link['href']
if not url:
errorString = "Invalid or missing field. " + \
"Acceptable fields for this object are:\n" + \
"\n\n".join(fields)
log.debug(errorString)
return None
try:
return simplejson.loads(self.client._getResource(url, token))
except:
return []
def getInfoLink(self,field):
for link in self.info['link']:
if link['title'] == field or link['rel'] == field:
return link['href']
return None
class NetflixUser(NetflixBase):
def __init__(self, user_auth_dict, client):
self.requestTokenUrl = REQUEST_TOKEN_URL
self.accessTokenUrl = ACCESS_TOKEN_URL
self.authorizationUrl = AUTHORIZATION_URL
if user_auth_dict:
self.accessToken = oauth.OAuthToken(
user_auth_dict['access']['key'],
user_auth_dict['access']['secret']
)
# get the actual user data
requestUrl = '/users/%s' % (self.accessToken.key)
raw_json = simplejson.loads(
client._getResource(
requestUrl,
token=self.accessToken
)
)
super(NetflixUser, self).__init__(raw_json['user'], client)
else:
self.accessToken = None
super(NetflixUser, self).__init__(None, client)
@property
def name(self):
first = self.info['first_name']
last = self.info['last_name']
return "%s %s" % (first, last)
@property
def preferred_formats(self):
if isinstance(self.info['preferred_formats']['category'], list):
formats = []
for format in self.info['preferred_formats']['category']:
formats.append(format['term'])
return formats
else:
return [self.info['preferred_formats']['category']['term']]
@property
def can_instant_watch(self):
return self.info['can_instant_watch'] == 'true'
def at_home(self):
if isinstance(self.getInfo('at home')['at_home']['at_home_item'], list):
return [NetflixTitle(title,self.client) for title in self.getInfo('at home')['at_home']['at_home_item']]
else:
return [NetflixTitle(self.getInfo('at home')['at_home']['at_home_item'],self.client)]
def getQueue(self):
# finish up member / queue interaction
pass
def getRequestToken(self):
client = self.client
oauthRequest = oauth.OAuthRequest.from_consumer_and_token(
client.consumer,
http_url=self.requestTokenUrl
)
oauthRequest.sign_request(
client.signature_method_hmac_sha1,
client.consumer,
None
)
client.connection.request(
oauthRequest.http_method,
self.requestTokenUrl,
headers=oauthRequest.to_header()
)
response = client.connection.getresponse()
requestToken = oauth.OAuthToken.from_string(response.read())
params = {'application_name': client.CONSUMER_NAME,
'oauth_consumer_key': client.CONSUMER_KEY}
oauthRequest = oauth.OAuthRequest.from_token_and_callback(
token=requestToken,
callback=client.CONSUMER_CALLBACK,
http_url=self.authorizationUrl,
parameters=params
)
return (requestToken, oauthRequest.to_url())
def getAccessToken(self, requestToken):
client = self.client
if not isinstance(requestToken, oauth.OAuthToken):
requestToken = oauth.OAuthToken(
requestToken['key'],
requestToken['secret'])
oauthRequest = oauth.OAuthRequest.from_consumer_and_token(
client.consumer,
token=requestToken,
http_url=self.accessTokenUrl
)
oauthRequest.sign_request(
client.signature_method_hmac_sha1,
client.consumer,
requestToken
)
client.connection.request(
oauthRequest.http_method,
self.accessTokenUrl,
headers=oauthRequest.to_header()
)
response = client.connection.getresponse()
return oauth.OAuthToken.from_string(response.read())
### I haven't cleaned up user stuff below this line. ###
# Should be working on a NetflixTitle object
def getRatings(self, titles=[]):
accessToken=self.accessToken
requestUrl = '/users/%s/ratings/title' % (accessToken.key)
urls = [ x.id for x in titles]
parameters = { 'title_refs': ','.join(urls) }
info = simplejson.loads( self.client._getResource(
requestUrl,
parameters=parameters,
token=accessToken ) )
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 getRentalHistory(self,type=None,startIndex=None,
maxResults=None,updatedMin=None):
accessToken=self.accessToken
parameters = {}
if startIndex:
parameters['start_index'] = startIndex
if maxResults:
parameters['max_results'] = maxResults
if updatedMin:
parameters['updated_min'] = updatedMin
if not isinstance(accessToken, oauth.OAuthToken):
accessToken = oauth.OAuthToken(
accessToken['key'],
accessToken['secret'])
if not type:
requestUrl = '/users/%s/rental_history' % (accessToken.key)
else:
requestUrl = '/users/%s/rental_history/%s' % (accessToken.key,type)
try:
info = simplejson.loads( self.client._getResource(
requestUrl,
parameters=parameters,
token=accessToken))
except:
return {}
return info
class NetflixUserQueue:
def __init__(self,user):
self.user = user
self.client = user.client
self.tag = None
def getContents(self, sort=None, startIndex=None,
maxResults=None, updatedMin=None):
parameters={}
if startIndex:
parameters['start_index'] = startIndex
if maxResults:
parameters['max_results'] = maxResults
if updatedMin:
parameters['updated_min'] = updatedMin
if sort and sort in ('queue_sequence','date_added','alphabetical'):
parameters['sort'] = sort
requestUrl = '/users/%s/queues' % (self.user.accessToken.key)
try:
info = simplejson.loads(self.client._getResource(
requestUrl,
parameters=parameters,
token=self.user.accessToken ))
except:
return []
else:
return info
def getAvailable(self, sort=None, startIndex=None,
maxResults=None, updatedMin=None,
type='disc'):
parameters={}
if startIndex:
parameters['start_index'] = startIndex
if maxResults:
parameters['max_results'] = maxResults
if updatedMin:
parameters['updated_min'] = updatedMin
if sort and sort in ('queue_sequence','date_added','alphabetical'):
parameters['sort'] = sort
requestUrl = '/users/%s/queues/%s/available' % (
self.user.accessToken.key,
type)
try:
info = simplejson.loads(self.client._getResource(
requestUrl,
parameters=parameters,
token=self.user.accessToken ))
except:
return []
else:
return info
def getSaved(self, sort=None, startIndex=None,
maxResults=None, updatedMin=None,
type='disc'):
parameters={}
if startIndex:
parameters['start_index'] = startIndex
if maxResults:
parameters['max_results'] = maxResults
if updatedMin:
parameters['updated_min'] = updatedMin
if sort and sort in ('queue_sequence','date_added','alphabetical'):
parameters['sort'] = sort
requestUrl = '/users/%s/queues/%s/saved' % (
self.user.accessToken.key,
type)
try:
info = simplejson.loads(self.client._getResource(
requestUrl,
parameters=parameters,
token=self.user.accessToken))
except:
return []
else:
return info
def addTitle(self, discInfo=[], urls=[],type='disc',position=None):
accessToken=self.user.accessToken
parameters={}
if position:
parameters['position'] = position
if not isinstance(accessToken, oauth.OAuthToken):
accessToken = oauth.OAuthToken(
accessToken['key'],
accessToken['secret'])
requestUrl = '/users/%s/queues/disc' % (accessToken.key)
if not urls:
for disc in discInfo:
urls.append(disc['id'])
parameters = {'title_ref': ','.join(urls)}
if not self.tag:
response = self.client._getResource(
requestUrl,
token=accessToken)
response = simplejson.loads(response)
self.tag = response["queue"]["etag"]
parameters['etag'] = self.tag
response = self.client._postResource(
requestUrl,
token=accessToken,
parameters=parameters)
return response
def removeTitle(self, id, type='disc'):
accessToken=self.user.accessToken
entryID = None
parameters={}
if not isinstance(accessToken, oauth.OAuthToken):
accessToken = oauth.OAuthToken(
accessToken['key'],
accessToken['secret'])
# First, we gotta find the entry to delete
queueparams = {'max_results': 500}
requestUrl = '/users/%s/queues/disc' % (accessToken.key)
response = self.client._getResource(
requestUrl,
token=accessToken,
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
firstResponse = self.client._getResource(
entryID,
token=accessToken,
parameters=parameters)
response = self.client._deleteResource(entryID, token=accessToken)
return response
class NetflixPerson(NetflixBase):
@property
def id(self):
return self.info['id'].split('/')[-1]
@property
def name(self):
return self.info['name']
def filmography(self):
raw_films = self.getInfo('filmography')['filmography']['filmography_item']
if isinstance(raw_films, list):
films = []
for film in raw_films:
films.append(NetflixTitle(film,self.client))
return films
else:
return NetflixTitle(raw_films,self.client)
class NetflixTitle(NetflixBase):
# helper functions for parent and child functions
def _get_title_single(self, url):
return NetflixTitle(self.getInfo(url)['catalog_title'],self.client)
def _get_title_list(self, url):
try:
return [NetflixTitle(title,self.client) for title in self.getInfo(url)['catalog_titles']['catalog_title']]
except TypeError:
return []
# These functions get the items parents, if they exist
def disc(self):
return self._get_title_single(DISC_URL)
def season(self):
return self._get_title_single(SEASON_URL)
def series(self):
return self._get_title_single(SERIES_URL)
# These functions get the items children, if they exist
def seasons(self):
return self._get_title_list(SEASONS_URL)
def discs(self):
return self._get_title_list(DISCS_URL)
def episodes(self):
return self._get_title_list(EPISODES_URL)
# These functions deal with the current items details
def __repr__(self):
return self.title
@property
def id(self):
return self.getInfoLink(TITLE_URL) or self.info['id']
@property
def int_id(self):
return self.info['id'].split('/')[-1]
@property
def type(self):
raw = self.id.split('/')[-2]
if raw != 'series':
# remove the 's' from the end of everything but series
return self.id.split('/')[-2][0:-1]
else:
return raw
@property
def title(self):
return self.info['title']['regular']
@property
def series_title(self):
if self.type == 'series':
return self.title
elif self.type != 'movie':
return self.series().title
return None
@property
def season_number(self):
try:
i = 1
for season in self.series().seasons():
if self.id == season.id:
return i
else:
i += 1
except TypeError:
return None
@property
def episode_title(self):
try:
return self.info['title']['episode_short']
except KeyError:
return None
@property
def episode_number(self):
try:
i = 1
for episode in self.season().episodes():
if self.id == episode.id:
return i
else:
i += 1
except TypeError:
return None
@property
def disc_title(self):
try:
raw_title = self.info['title']['regular']
if re.search(disk_pattern,raw_title):
title = re.split(disk_pattern,raw_title)[-1]
if len(title) > 2:
return title
return None
except:
return None
@property
def disc_number(self):
try:
i = 1
for disc in self.season().discs():
if self.id == disc.id:
return i
else:
i += 1
except TypeError:
return None
@property
def length(self):
seconds = int(self.info['runtime'])
return seconds/60
@property
def mpaa_rating(self):
for i in self.info['category']:
if i['scheme'] == 'http://api.netflix.com/categories/mpaa_ratings':
return i['term']
return None
@property
def tv_rating(self):
for i in self.info['category']:
if i['scheme'] == 'http://api.netflix.com/categories/tv_ratings':
return i['term']
return None
@property
def release_year(self):
try:
return self.info['release_year']
except KeyError:
return None
@property
def synopsis(self):
try:
return self.getInfo('synopsis')['synopsis']
except KeyError:
return None
@property
def shipped_date(self):
try:
return datetime.fromtimestamp(float(self.info['shipped_date']))
except KeyError:
return None
# deeper info about an item (requires more queries of the netflix api)
def formats(self):
raw_formats = self.getInfo('formats')['delivery_formats']['availability']
if isinstance(raw_formats, list):
formats = []
for format in raw_formats:
format_name = format['category']['term']
release_date = datetime.fromtimestamp(float(format['available_from']))
formats.append({'format':format_name, 'release_date': release_date})
return formats
else:
format_name = raw_formats['category']['term']
release_date = datetime.fromtimestamp(float(raw_formats['available_from']))
return [{'format':format_name, 'release_date': release_date}]
def directors(self):
raw_directors = self.getInfo('directors')
raw_directors = raw_directors['people']['person']
if isinstance(raw_directors, list):
directors = []
for director in raw_directors:
directors.append(NetflixPerson(director, self.client))
return directors
else:
return NetflixPerson(raw_directors, self.client)
def cast(self):
raw_cast = self.getInfo('cast')
raw_cast = raw_cast['people']['person']
if isinstance(raw_cast, list):
return [NetflixPerson(person, self.client) for person in raw_cast]
else:
return NetflixPerson(raw_cast, self.client)
def bonus_material(self):
raw_bonus = self.getInfo('bonus materials')
if raw_bonus:
return [self.client.catalog.title(title['href']) for title in raw_bonus['bonus_materials']['link']]
else:
return []
def similar_titles(self):
raw_sim = self.getInfo('similars')
if raw_sim:
raw_sim = raw_sim['similars']['similars_item']
if isinstance(raw_sim, list):
return [NetflixTitle(title,self.client) for title in raw_sim]
else:
return NetflixTitle(raw_sim, self.client)
else:
return []
def user_state(self):
user = self.client.user
try:
return simplejson.loads(
self.client._getResource(
url=user.getInfo('title states')['title_states']['url_template'].split('?')[0],
token=user.accessToken,
parameters={'title_refs':self.id}
)
)
except:
return []
class NetflixCatalog():
def __init__(self,client):
self.client = client
def index(self):
requestUrl = '/catalog/titles/index'
# only returns in XML format?
return self.client._getResource(
requestUrl,
xml=True,
)
def autocomplete(self, term,startIndex=None,maxResults=None):
requestUrl = '/catalog/titles/autocomplete'
parameters = {'term': term}
if startIndex:
parameters['start_index'] = startIndex
if maxResults:
parameters['max_results'] = maxResults
try:
info = simplejson.loads(
self.client._getResource(
requestUrl,
parameters=parameters
)
)
return [x['title']['short'] for x in info['autocomplete']['autocomplete_item']]
except KeyError:
return []
def search_titles(self, term,startIndex=None,maxResults=None):
requestUrl = '/catalog/titles'
parameters = {'term': term}
if startIndex:
parameters['start_index'] = startIndex
if maxResults:
parameters['max_results'] = maxResults
try:
info = simplejson.loads(
self.client._getResource(
requestUrl,
parameters=parameters
)
)
return [NetflixTitle(title,self.client) for title in info['catalog_titles']['catalog_title']]
except KeyError:
return []
def search_people(self, term,startIndex=None,maxResults=None):
requestUrl = '/catalog/people'
parameters = {'term': term}
if startIndex:
parameters['start_index'] = startIndex
if maxResults:
parameters['max_results'] = maxResults
try:
info = simplejson.loads(
self.client._getResource(
requestUrl,
parameters=parameters
)
)
return [NetflixPerson(person,self.client) for person in info['people']['person']]
except KeyError:
return []
def title(self, url):
requestUrl = url
try:
info = simplejson.loads(self.client._getResource(requestUrl))
return NetflixTitle(info['catalog_title'],self.client)
except urllib2.HTTPError:
return None
def person(self,url):
requestUrl = url
try:
info = simplejson.loads(self.client._getResource(requestUrl))
return NetflixPerson(info['person'],self.client)
except urllib2.HTTPError:
return None