From 15a1fa338379ae88bb8d13190d9abbe89e6da1a8 Mon Sep 17 00:00:00 2001 From: Tomer Zait Date: Sat, 7 Nov 2015 18:35:52 +0200 Subject: [PATCH] Fixed android non rar support, rearrange code --- .gitignore | 1 + addon.xml | 2 +- changelog.txt | 3 + resources/__init__.py | 1 + resources/lib/bsplayer.py | 160 ++++---------------------------------- resources/lib/utils.py | 156 +++++++++++++++++++++++++++++++++++++ service.py | 53 +++---------- 7 files changed, 188 insertions(+), 188 deletions(-) create mode 100644 resources/__init__.py create mode 100644 resources/lib/utils.py diff --git a/.gitignore b/.gitignore index d4a011c..9ac898e 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,7 @@ htmlcov/ nosetests.xml coverage.xml *,cover +bsplayer_test.py # Translations *.mo diff --git a/addon.xml b/addon.xml index 3942f3b..d67e56d 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ diff --git a/changelog.txt b/changelog.txt index c196449..bfa041b 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,6 @@ +0.2.1 - 07/11/2015 +- Fixed android non rar support, rearrange code + 0.2.0 - 07/11/2015 - Fixed android rar support, works with smb shares diff --git a/resources/__init__.py b/resources/__init__.py new file mode 100644 index 0000000..b93054b --- /dev/null +++ b/resources/__init__.py @@ -0,0 +1 @@ +# Dummy file to make this directory a package. diff --git a/resources/lib/bsplayer.py b/resources/lib/bsplayer.py index 7a93fc8..6052322 100644 --- a/resources/lib/bsplayer.py +++ b/resources/lib/bsplayer.py @@ -1,16 +1,10 @@ -import cookielib -import gzip -import logging +import gzip import random -import struct -import urllib2 -from StringIO import StringIO -from httplib import HTTPConnection -from os import path from time import sleep +from StringIO import StringIO from xml.etree import ElementTree -import xbmcvfs +from utils import movie_size_and_hash, get_session, log # s1-9, s101-109 SUB_DOMAINS = ['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', @@ -23,121 +17,11 @@ def get_sub_domain(): return API_URL_TEMPLATE.format(sub_domain=SUB_DOMAINS[random.randint(0, sub_domains_end)]) -def get_session(proxies=None): - cj = cookielib.CookieJar() - proxy_handler = urllib2.ProxyHandler(proxies) - return urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), proxy_handler) - - -def python_logger(module, msg): - logger = logging.getLogger('BSPlayer') - logger.log(logging.DEBUG, (u"### [%s] - %s" % (module, msg))) - - -def OpensubtitlesHashRar(firsrarfile): - # log(__name__, "Hash Rar file") - f = xbmcvfs.File(firsrarfile) - a = f.read(4) - if a != 'Rar!': - raise Exception('ERROR: This is not rar file.') - seek = 0 - for i in range(4): - f.seek(max(0, seek), 0) - a = f.read(100) - type, flag, size = struct.unpack('{params}' ).format(search_url=self.search_url, func_name=func_name, params=params) - self.log("BSPlayer.api_request", 'Sending request: %s' % func_name) + log('BSPlayer.api_request', 'Sending request: %s.' % func_name) for i in xrange(tries): try: self.session.addheaders.extend(headers.items()) res = self.session.open(self.search_url, data) return ElementTree.fromstring(res.read()) except Exception, ex: - self.log("BSPlayer.api_request", ex) + log("BSPlayer.api_request", "ERROR: %s." % ex) if func_name == 'logIn': self.search_url = get_sub_domain() sleep(1) - + log('BSPlayer.api_request', 'ERROR: Too many tries (%d)...' % tries) raise Exception('Too many tries...') def login(self): @@ -191,7 +75,7 @@ def login(self): res = root.find('.//return') if res.find('status').text == 'OK': self.token = res.find('data').text - self.log("BSPlayer.login", "Logged In Successfully.") + log("BSPlayer.login", "Logged In Successfully.") return True return False @@ -207,7 +91,7 @@ def logout(self): res = root.find('.//return') self.token = None if res.find('status').text == 'OK': - self.log("BSPlayer.logout", "Logged Out Successfully.") + log("BSPlayer.logout", "Logged Out Successfully.") return True return False @@ -219,7 +103,7 @@ def search_subtitles(self, movie_path, language_ids='heb,eng', logout=False): language_ids = ",".join(language_ids) movie_size, movie_hash = movie_size_and_hash(movie_path) - self.log('BSPlayer.search_subtitles', 'Movie Size: %s, Movie Hash: %s' % (movie_size, movie_hash)) + log('BSPlayer.search_subtitles', 'Movie Size: %s, Movie Hash: %s.' % (movie_size, movie_hash)) root = self.api_request( func_name='searchSubtitles', params=( @@ -238,7 +122,7 @@ def search_subtitles(self, movie_path, language_ids='heb,eng', logout=False): items = root.findall('.//return/data/item') subtitles = [] if items: - self.log("BSPlayer.search_subtitles", "Subtitles Found.") + log("BSPlayer.search_subtitles", "Subtitles Found.") for item in items: subtitles.append(dict( subID=item.find('subID').text, @@ -254,12 +138,11 @@ def search_subtitles(self, movie_path, language_ids='heb,eng', logout=False): return subtitles @staticmethod - def download_subtitles(download_url, dest_path="Subtitle.srt", proxies=None): - proxy_handler = urllib2.ProxyHandler(proxies) - opener = urllib2.build_opener(HTTP10Handler, proxy_handler) - opener.addheaders = [('User-Agent', 'Mozilla/4.0 (compatible; Synapse)'), + def download_subtitles(download_url, dest_path, proxies=None): + session = get_session(proxies=proxies, http_10=True) + session.addheaders = [('User-Agent', 'Mozilla/4.0 (compatible; Synapse)'), ('Content-Length', 0)] - res = opener.open(download_url) + res = session.open(download_url) if res: gf = gzip.GzipFile(fileobj=StringIO(res.read())) with open(dest_path, 'wb') as f: @@ -268,14 +151,3 @@ def download_subtitles(download_url, dest_path="Subtitle.srt", proxies=None): gf.close() return True return False - - -if __name__ == '__main__': - bsp = BSPlayer(proxies={'http': '207.91.10.234:8080'}) - subs = bsp.search_subtitles( - r'V:\Movies\Jackass.Presents.Bad.Grandpa.0.5.2014.720p.Bluray.x264.DTS-EVO\Jackass.Presents.Bad.Grandpa.0.5.2014.720p.Bluray.x264.DTS-EVO.mkv', - logout=True - ) - for sub in subs: - print bsp.download_subtitles(sub['subDownloadLink'], proxies={'http': '207.91.10.234:8080'}) - break diff --git a/resources/lib/utils.py b/resources/lib/utils.py new file mode 100644 index 0000000..8035de1 --- /dev/null +++ b/resources/lib/utils.py @@ -0,0 +1,156 @@ +import sys +import struct +import urllib2 +import urlparse +import cookielib +from os import path +from httplib import HTTPConnection + +import xbmc +import xbmcvfs + + +def log(module, msg): + xbmc.log((u"### [%s] - %s" % (module, msg)).encode('utf-8'), level=xbmc.LOGDEBUG) + + +def notify(script_name, language, string_id): + xbmc.executebuiltin((u'Notification(%s,%s)' % (script_name, language(string_id))).encode('utf-8')) + + +def get_params(params_str=""): + params_str = params_str or sys.argv[2] + return dict(urlparse.parse_qsl(params_str.lstrip('?'))) + + +def get_video_path(xbmc_path=''): + xbmc_path = xbmc_path or urlparse.unquote(xbmc.Player().getPlayingFile().decode('utf-8')) + if xbmc_path.startswith('rar://'): + return path.dirname(xbmc_path.replace('rar://', '')) + elif xbmc_path.startswith('stack://'): + return xbmc_path.split(" , ")[0].replace('stack://', '') + + return xbmc_path + + +def get_languages_dict(languages_param): + langs = {} + for lang in languages_param.split(','): + if lang == "Portuguese (Brazil)": + langs["pob"] = lang + elif lang == "Greek": + langs["ell"] = lang + else: + langs[xbmc.convertLanguage(lang, xbmc.ISO_639_2)] = lang + return langs + + +class HTTP10Connection(HTTPConnection): + _http_vsn = 10 + _http_vsn_str = "HTTP/1.0" + + +class HTTP10Handler(urllib2.HTTPHandler): + def http_open(self, req): + return self.do_open(HTTP10Connection, req) + + +def get_session(proxies=None, cookies=True, http_10=False): + handlers = [] + if proxies: + handlers.append(urllib2.ProxyHandler(proxies)) + if cookies: + cj = cookielib.CookieJar() + handlers.append(urllib2.HTTPCookieProcessor(cj)) + if http_10: + handlers.append(HTTP10Handler) + return urllib2.build_opener(*handlers) + + +def __get_last_split(firs_rar_file, x): + if firs_rar_file[-3:] == '001': + return firs_rar_file[:-3] + ('%03d' % (x + 1)) + if firs_rar_file[-11:-6] == '.part': + return firs_rar_file[0:-6] + ('%02d' % (x + 1)) + firs_rar_file[-4:] + if firs_rar_file[-10:-5] == '.part': + return firs_rar_file[0:-5] + ('%1d' % (x + 1)) + firs_rar_file[-4:] + return firs_rar_file[0:-2] + ('%02d' % (x - 1)) + + +def __add_file_hash(name, file_hash, seek): + f = xbmcvfs.File(name) + f.seek(max(0, seek), 0) + for i in range(8192): + file_hash += struct.unpack('