# Module: NetflixSession
# Created on: 13.01.2017
-import sys
import os
-import base64
-import time
-import urllib
import json
-import requests
-import platform
+from requests import session, cookies
+from urllib import quote, unquote
+from time import time
+from base64 import urlsafe_b64encode
+from bs4 import BeautifulSoup, SoupStrainer
+from utils import noop
try:
import cPickle as pickle
except:
import pickle
-from bs4 import BeautifulSoup, SoupStrainer
-from pyjsparser import PyJsParser
-from utils import noop
class NetflixSession:
"""Helps with login/session management of Netflix users & API data fetching"""
urls = {
'login': '/login',
- 'browse': '/browse',
- 'video_list_ids': '/warmer',
+ 'browse': '/profiles/manage',
+ 'video_list_ids': '/preflight',
'shakti': '/pathEvaluator',
- 'profiles': '/browse',
+ 'profiles': '/profiles/manage',
'switch_profiles': '/profiles/switch',
'adult_pin': '/pin/service',
'metadata': '/metadata',
'set_video_rating': '/setVideoRating',
- 'update_my_list': '/playlistop'
+ 'update_my_list': '/playlistop',
+ 'kids': '/Kids'
}
""":obj:`dict` of :obj:`str` List of all static endpoints for HTML/JSON POST/GET requests"""
self.log = log_fn
# start session, fake chrome on the current platform (so that we get a proper widevine esn) & enable gzip
- self.session = requests.session()
+ self.session = session()
self.session.headers.update({
'User-Agent': self._get_user_agent_for_current_platform(),
'Accept-Encoding': 'gzip'
List of all the serialized data pulled out of the pagws <script/> tags
"""
scripts = page_soup.find_all('script', attrs={'src': None});
- self.log('Trying sloppy inline data parser')
+ self.log(msg='Trying sloppy inline data parser')
inline_data = self._sloppy_parse_inline_data(scripts=scripts)
if self._verfify_auth_and_profiles_data(data=inline_data) != False:
- self.log('Sloppy inline data parsing successfull')
+ self.log(msg='Sloppy inline data parsing successfull')
return inline_data
- self.log('Sloppy inline parser failed, trying JS parser')
+ self.log(msg='Sloppy inline parser failed, trying JS parser')
return self._accurate_parse_inline_data(scripts=scripts)
def is_logged_in (self, account):
"""
payload = {
'switchProfileGuid': profile_id,
- '_': int(time.time()),
+ '_': int(time()),
'authURL': self.user_data['authURL']
}
if response.status_code != 200:
return False
- # fetch the index page again, so that we can fetch the corresponding user data
- browse_response = self._session_get(component='browse')
- only_script_tags = SoupStrainer('script')
- browse_soup = BeautifulSoup(browse_response.text, 'html.parser', parse_only=only_script_tags)
account_hash = self._generate_account_hash(account=account)
self.user_data['guid'] = profile_id;
- self._save_data(filename=self.data_path + '_' + account_hash)
- return True
+ return self._save_data(filename=self.data_path + '_' + account_hash)
def send_adult_pin (self, pin):
"""Send the adult pin to Netflix in case an adult rated video requests it
for key in self.video_list_keys:
video_list_ids[key] = {}
+ # check if the list items are hidden behind a `value` sub key
+ # this is the case when we fetch the lists via POST, not via a GET preflight request
+ if 'value' in response_data.keys():
+ response_data = response_data['value']
+
# subcatogorize the lists by their context
video_lists = response_data['lists']
for video_list_id in video_lists.keys():
video_list = video_lists[video_list_id]
- if video_list['context'] == 'genre':
- video_list_ids['genres'].update(self.parse_video_list_ids_entry(id=video_list_id, entry=video_list))
- elif video_list['context'] == 'similars' or video_list['context'] == 'becauseYouAdded':
- video_list_ids['recommendations'].update(self.parse_video_list_ids_entry(id=video_list_id, entry=video_list))
- else:
- video_list_ids['user'].update(self.parse_video_list_ids_entry(id=video_list_id, entry=video_list))
-
+ if video_list.get('context', False) != False:
+ if video_list['context'] == 'genre':
+ video_list_ids['genres'].update(self.parse_video_list_ids_entry(id=video_list_id, entry=video_list))
+ elif video_list['context'] == 'similars' or video_list['context'] == 'becauseYouAdded':
+ video_list_ids['recommendations'].update(self.parse_video_list_ids_entry(id=video_list_id, entry=video_list))
+ else:
+ video_list_ids['user'].update(self.parse_video_list_ids_entry(id=video_list_id, entry=video_list))
return video_list_ids
def parse_video_list_ids_entry (self, id, entry):
'synopsis': video['synopsis'],
'regular_synopsis': video['regularSynopsis'],
'type': video['summary']['type'],
- 'rating': video['userRating']['average'],
+ 'rating': video['userRating'].get('average', 0) if video['userRating'].get('average', None) != None else video['userRating'].get('predicted', 0),
'episode_count': season_info['episode_count'],
'seasons_label': season_info['seasons_label'],
'seasons_count': season_info['seasons_count'],
'mpaa': str(episode['maturity']['rating']['board']) + ' ' + str(episode['maturity']['rating']['value']),
'maturity': episode['maturity'],
'playcount': (0, 1)[episode['watched']],
- 'rating': episode['userRating']['average'],
+ 'rating': episode['userRating'].get('average', 0) if episode['userRating'].get('average', None) != None else episode['userRating'].get('predicted', 0),
'thumb': episode['info']['interestingMoments']['url'],
'fanart': episode['interestingMoment']['_1280x720']['jpg']['url'],
'poster': episode['boxarts']['_1280x720']['jpg']['url'],
response = self._session_get(component='browse')
return BeautifulSoup(response.text, 'html.parser')
- def fetch_video_list_ids (self, list_from=0, list_to=50):
+ def fetch_video_list_ids_via_preflight (self, list_from=0, list_to=50):
"""Fetches the JSON with detailed information based on the lists on the landing page (browse page) of Netflix
+ via the preflight (GET) request
Parameters
----------
'toRow': list_to,
'opaqueImageExtension': 'jpg',
'transparentImageExtension': 'png',
- '_': int(time.time()),
+ '_': int(time()),
'authURL': self.user_data['authURL']
}
+
response = self._session_get(component='video_list_ids', params=payload, type='api')
return self._process_response(response=response, component=self._get_api_url_for(component='video_list_ids'))
+ def fetch_video_list_ids (self, list_from=0, list_to=50):
+ """Fetches the JSON with detailed information based on the lists on the landing page (browse page) of Netflix
+
+ Parameters
+ ----------
+ list_from : :obj:`int`
+ Start entry for pagination
+
+ list_to : :obj:`int`
+ Last entry for pagination
+
+ Returns
+ -------
+ :obj:`dict` of :obj:`dict` of :obj:`str`
+ Raw Netflix API call response or api call error
+ """
+ paths = [
+ ['lolomo', {'from': list_from, 'to': list_to}, ['displayName', 'context', 'id', 'index', 'length']]
+ ]
+
+ response = self._path_request(paths=paths)
+ return self._process_response(response=response, component='Video list ids')
+
def fetch_search_results (self, search_str, list_from=0, list_to=10):
"""Fetches the JSON which contains the results for the given search query
Raw Netflix API call response or api call error
"""
# properly encode the search string
- encoded_search_string = urllib.quote(search_str)
+ encoded_search_string = quote(search_str)
paths = [
['search', encoded_search_string, 'titles', {'from': list_from, 'to': list_to}, ['summary', 'title']],
payload = {
'movieid': id,
'imageformat': 'jpg',
- '_': int(time.time())
+ '_': int(time())
}
response = self._session_get(component='metadata', params=payload, type='api')
return self._process_response(response=response, component=self._get_api_url_for(component='metadata'))
})
params = {
- 'withSize': True,
- 'materialize': True,
'model': self.user_data['gpsModel']
}
return False
with open(filename) as f:
- cookies = pickle.load(f)
- if cookies:
- jar = requests.cookies.RequestsCookieJar()
- jar._cookies = cookies
+ _cookies = pickle.load(f)
+ if _cookies:
+ jar = cookies.RequestsCookieJar()
+ jar._cookies = _cookies
self.session.cookies = jar
else:
return False
:obj:`str`
Account data hash
"""
- return base64.urlsafe_b64encode(account['email'])
+ return urlsafe_b64encode(account['email'])
def _get_user_agent_for_current_platform (self):
"""Determines the user agent string for the current platform (to retrieve a valid ESN)
:obj:`str`
User Agent for platform
"""
+ import platform
if platform == 'linux' or platform == 'linux2':
return 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
elif platform == 'darwin':
Contents of the field to match
"""
url = self._get_document_url_for(component=component) if type == 'document' else self._get_api_url_for(component=component)
- start = time.time()
+ start = time()
response = self.session.post(url=url, data=data, params=params, headers=headers, verify=self.verify_ssl)
- end = time.time()
- self.log('[POST] Request for "' + url + '" took ' + str(end - start) + ' seconds')
+ end = time()
+ self.log(msg='[POST] Request for "' + url + '" took ' + str(end - start) + ' seconds')
return response
def _session_get (self, component, type='document', params={}):
Contents of the field to match
"""
url = self._get_document_url_for(component=component) if type == 'document' else self._get_api_url_for(component=component)
- start = time.time()
+ start = time()
response = self.session.get(url=url, verify=self.verify_ssl, params=params)
- end = time.time()
- self.log('[GET] Request for "' + url + '" took ' + str(end - start) + ' seconds')
+ end = time()
+ self.log(msg='[GET] Request for "' + url + '" took ' + str(end - start) + ' seconds')
return response
def _sloppy_parse_user_and_api_data (self, key, contents):
:obj:`dict` of :obj:`str`
Dict containing user, api & profile data
"""
- inline_data = [];
+ inline_data = []
+ from pyjsparser import PyJsParser
parser = PyJsParser()
for script in scripts:
- data = {};
+ data = {}
# unicode escape that incoming script stuff
contents = self._to_unicode(str(script.contents[0]))
# parse the JS & load the declarations we´re interested in
parsed = parser.parse(contents)
if len(parsed['body']) > 1 and parsed['body'][1]['expression']['right'].get('properties', None) != None:
- declarations = parsed['body'][1]['expression']['right']['properties'];
+ declarations = parsed['body'][1]['expression']['right']['properties']
for declaration in declarations:
for key in declaration:
# we found the correct path if the declaration is a dict & of type 'ObjectExpression'
'profileName',
'isActive',
'isFirstUse',
- 'isAccountOwner'
+ 'isAccountOwner',
+ 'isKids'
]
-
# values are accessible via dict (sloppy parsing successfull)
if type(netflix_page_data) == dict:
for profile_id in netflix_page_data.get('profiles'):
:obj:`str` of :obj:`str
ESN, something like: NFCDCH-MC-D7D6F54LOPY8J416T72MQXX3RD20ME
"""
- esn = '';
+ esn = ''
# values are accessible via dict (sloppy parsing successfull)
if type(netflix_page_data) == dict:
return netflix_page_data.get('esn', '')
self.esn = self._parse_esn_data(netflix_page_data=netflix_page_data)
self.api_data = self._parse_api_base_data(netflix_page_data=netflix_page_data)
self.profiles = self._parse_profile_data(netflix_page_data=netflix_page_data)
- self.log('Found ESN "' + self.esn + '" for platform "' + str(platform.system()) + '"')
+ self.log(msg='Found ESN "' + self.esn + '"')
return netflix_page_data