From 67827742c66aa4f897a5f36863c3cb8e9d9af53d Mon Sep 17 00:00:00 2001 From: Tomer Zait Date: Sat, 7 Nov 2015 05:42:23 +0200 Subject: [PATCH] Fixed android rar support, works with smb shares --- addon.xml | 2 +- changelog.txt | 3 + resources/language/English/strings.po | 23 ++++ resources/lib/bsplayer.py | 155 +++++++++++++++++--------- service.py | 17 +-- 5 files changed, 137 insertions(+), 63 deletions(-) create mode 100644 resources/language/English/strings.po diff --git a/addon.xml b/addon.xml index a736046..3942f3b 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ diff --git a/changelog.txt b/changelog.txt index 37c8922..c196449 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,2 +1,5 @@ +0.2.0 - 07/11/2015 +- Fixed android rar support, works with smb shares + 0.1.0 - 27/09/2015 - Initial release \ No newline at end of file diff --git a/resources/language/English/strings.po b/resources/language/English/strings.po new file mode 100644 index 0000000..96400bc --- /dev/null +++ b/resources/language/English/strings.po @@ -0,0 +1,23 @@ +msgid "" +msgstr "" +"Project-Id-Version: XBMC Main Translation Project (Frodo)\n" +"Report-Msgid-Bugs-To: http://trac.xbmc.org/\n" +"POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: XBMC Translation Team\n" +"Language-Team: English (http://www.transifex.com/projects/p/XBMC-Main-Frodo/language/en/)\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Language: en\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#English + +msgctxt "#32001" +msgid "Streaming not supported." +msgstr "" + +msgctxt "#32002" +msgid "Manual search not supported." +msgstr "" \ No newline at end of file diff --git a/resources/lib/bsplayer.py b/resources/lib/bsplayer.py index a396e3f..cc5cf30 100644 --- a/resources/lib/bsplayer.py +++ b/resources/lib/bsplayer.py @@ -1,15 +1,16 @@ +import cookielib import gzip -import struct +import logging import random +import struct import urllib2 -import logging -from os import path from StringIO import StringIO -from xml.etree import ElementTree from httplib import HTTPConnection +from os import path +from time import sleep +from xml.etree import ElementTree -import rarfile - +import xbmcvfs # s1-9, s101-109 SUB_DOMAINS = ['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', @@ -17,49 +18,85 @@ API_URL_TEMPLATE = "http://{sub_domain}.api.bsplayer-subtitles.com/v1.php" -def check_connectivity(url, timeout=5): - try: - urllib2.urlopen(url, timeout=timeout) - except urllib2.URLError: - return False - return True - - def get_sub_domain(): sub_domains_end = len(SUB_DOMAINS) - 1 - url = API_URL_TEMPLATE.format(sub_domain=SUB_DOMAINS[random.randint(0, sub_domains_end)]) + return API_URL_TEMPLATE.format(sub_domain=SUB_DOMAINS[random.randint(0, sub_domains_end)]) - while not check_connectivity(url): - url = API_URL_TEMPLATE.format(sub_domain=SUB_DOMAINS[random.randint(0, sub_domains_end)]) - return url +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((u"### [%s] - %s" % (module, msg)), level=logging.DEBUG) - - -def generic_open(file_path): - rf = None - if path.splitext(file_path)[1] == '.rar': - rf = rarfile.RarFile(file_path) - rfi = rf.infolist()[0] - return rf, rf.open(rfi, 'r'), rfi.file_size - return rf, open(file_path, 'rb'), path.getsize(file_path) + 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) - req = urllib2.Request(self.search_url, data, headers) - return ElementTree.fromstring(urllib2.urlopen(req).read()) + self.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) + if func_name == 'logIn': + self.search_url = get_sub_domain() + sleep(1) + + raise Exception('Too many tries...') def login(self): # If already logged in @@ -175,6 +217,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)) root = self.api_request( func_name='searchSubtitles', params=( @@ -209,8 +252,9 @@ def search_subtitles(self, movie_path, language_ids='heb,eng', logout=False): return subtitles @staticmethod - def download_subtitles(download_url, dest_path=r"c:\tomerz.srt"): - opener = urllib2.build_opener(HTTP10Handler) + 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)'), ('Content-Length', 0)] res = opener.open(download_url) @@ -222,13 +266,14 @@ def download_subtitles(download_url, dest_path=r"c:\tomerz.srt"): gf.close() return True return False -# + # # if __name__ == '__main__': -# bsp = BSPlayer() +# bsp = BSPlayer(proxies={'http': '207.91.10.234:8080'}) # subs = bsp.search_subtitles( -# r'..\..\..\Jurassic.World.2015.720p.BluRay.x264-SPARKS\jurassic.world.2015.720p.bluray.x264-sparks.rar', +# 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 # ) -# print subs[0]['subDownloadLink'] -# print bsp.download_subtitles(subs[0]['subDownloadLink']) +# for sub in subs: +# print bsp.download_subtitles(sub['subDownloadLink'], proxies={'http': '207.91.10.234:8080'}) +# break diff --git a/service.py b/service.py index d276149..df46d61 100644 --- a/service.py +++ b/service.py @@ -40,11 +40,13 @@ def get_params(params_str=""): 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://', '') + elif xbmc_path.startswith('http://') or xbmc_path.startswith('https://'): + xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__ , __language__(32001))).encode('utf-8')) + log("BSPlayer.get_video_path", "Streaming not supported.") return xbmc_path @@ -62,12 +64,12 @@ def get_languages_dict(languages_param): params = get_params() -log("BSPlayers.params", "Current Action: %s." % params['action']) +log("BSPlayer.params", "Current Action: %s." % params['action']) if params['action'] == 'search': video_path = get_video_path() - log("BSPlayers.video_path", "Current Video Path: %s." % video_path) + log("BSPlayer.video_path", "Current Video Path: %s." % video_path) languages = get_languages_dict(params['languages']) - log("BSPlayers.languages", "Current Languages: %s." % languages) + log("BSPlayer.languages", "Current Languages: %s." % languages) with BSPlayer(log=log) as bsp: subtitles = bsp.search_subtitles(video_path, language_ids=languages.keys()) @@ -87,10 +89,11 @@ def get_languages_dict(languages_param): format=subtitle['subFormat'] )) ) - log("BSPlayers.plugin_url", "Plugin Url Created: %s." % plugin_url) + log("BSPlayer.plugin_url", "Plugin Url Created: %s." % plugin_url) xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=plugin_url, listitem=list_item, isFolder=False) elif params['action'] == 'manualsearch': - log("BSPlayer.manualsearch", "Cannot Search Manually.") + xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__ , __language__(32002))).encode('utf-8')) + log("BSPlayer.manualsearch", "Manual search not supported.") elif params['action'] == 'download': if xbmcvfs.exists(__temp__): shutil.rmtree(__temp__) @@ -101,7 +104,7 @@ def get_languages_dict(languages_param): if BSPlayer.download_subtitles(params['link'], subtitle_path): log("BSPlayer.download_subtitles", "Subtitles Download Successfully From: %s." % params['link']) list_item = xbmcgui.ListItem(label=subtitle_path) - log("BSPlayer.download", "Downloaded Subtitle Path: %s." % subtitle_path) + log("BSPlayer.download", "Downloaded Subtitle Path: %s" % subtitle_path) xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=subtitle_path, listitem=list_item, isFolder=False) xbmcplugin.endOfDirectory(int(sys.argv[1]))