X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=resources%2Flib%2FNetflixSession.py;h=3f9173be29adac83336bf0ab289bfad350f16f29;hb=b6a1c1cd2250104b139115ef16475c539626a9a7;hp=8c3172482c2348cd3f9c771de01f7c5d671758da;hpb=9616c8fc595d3b3bf2fe379febeb2f498e8001d6;p=plugin.video.netflix.git diff --git a/resources/lib/NetflixSession.py b/resources/lib/NetflixSession.py index 8c31724..3f9173b 100644 --- a/resources/lib/NetflixSession.py +++ b/resources/lib/NetflixSession.py @@ -3,21 +3,18 @@ # 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 +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""" @@ -103,7 +100,7 @@ class NetflixSession: 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' @@ -275,7 +272,7 @@ class NetflixSession: """ payload = { 'switchProfileGuid': profile_id, - '_': int(time.time()), + '_': int(time()), 'authURL': self.user_data['authURL'] } @@ -1298,7 +1295,7 @@ class NetflixSession: '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') @@ -1324,7 +1321,7 @@ class NetflixSession: 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']], @@ -1427,7 +1424,7 @@ class NetflixSession: 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')) @@ -1816,10 +1813,10 @@ class NetflixSession: 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 @@ -1852,7 +1849,7 @@ class NetflixSession: :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) @@ -1862,6 +1859,7 @@ class NetflixSession: :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': @@ -1897,9 +1895,9 @@ class NetflixSession: 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() + end = time() self.log('[POST] Request for "' + url + '" took ' + str(end - start) + ' seconds') return response @@ -1923,9 +1921,9 @@ class NetflixSession: 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() + end = time() self.log('[GET] Request for "' + url + '" took ' + str(end - start) + ' seconds') return response @@ -2073,16 +2071,17 @@ class NetflixSession: :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' @@ -2272,7 +2271,7 @@ class NetflixSession: :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', '') @@ -2297,5 +2296,5 @@ class NetflixSession: 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('Found ESN "' + self.esn + '"') return netflix_page_data