def getallsubs(url, allowed_languages, filename="", episode=""): subtitle_pattern = ("<td class=\"a1\">\s+<a href=\"(?P<link>/subtitles/[^\"]+)\">\s+" "<span class=\"[^\"]+ (?P<quality>\w+-icon)\">\s+(?P<language>[^\r\n\t]+)\s+</span>\s+" "<span>\s+(?P<filename>[^\r\n\t]+)\s+</span>\s+" "</a>\s+</td>\s+" "<td class=\"[^\"]+\">\s+(?P<numfiles>[^\r\n\t]*)\s+</td>\s+" "<td class=\"(?P<hiclass>[^\"]+)\">" "(?:.*?)<td class=\"a6\">\s+<div>\s+(?P<comment>[^\"]+) \s*</div>") codes = get_language_codes(allowed_languages) if len(codes) < 1: xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__, __language__(32004))).encode('utf-8')) return log(__name__, 'LanguageFilter='+','.join(codes)) content, response_url = geturl(url, 'LanguageFilter='+','.join(codes)) if content is None: return subtitles = [] h = HTMLParser.HTMLParser() episode_regex = None if episode != "": episode_regex = re.compile(get_episode_pattern(episode), re.IGNORECASE) log(__name__, "regex: %s" % get_episode_pattern(episode)) for matches in re.finditer(subtitle_pattern, content, re.IGNORECASE | re.DOTALL): numfiles = 1 if matches.group('numfiles') != "": numfiles = int(matches.group('numfiles')) languagefound = matches.group('language') language_info = subscene_languages[languagefound] if language_info and language_info['3let'] in allowed_languages: link = main_url + matches.group('link') subtitle_name = string.strip(matches.group('filename')) hearing_imp = (matches.group('hiclass') == "a41") rating = '0' if matches.group('quality') == "bad-icon": continue if matches.group('quality') == "positive-icon": rating = '5' comment = re.sub("[\r\n\t]+", " ", h.unescape(string.strip(matches.group('comment')))) sync = False if filename != "" and string.lower(filename) == string.lower(subtitle_name): sync = True if episode != "": log(__name__, "match: "+subtitle_name) if episode_regex.search(subtitle_name): subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment}) elif numfiles > 2: subtitle_name = subtitle_name + ' ' + (__language__(32001) % int(matches.group('numfiles'))) subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment, 'episode': episode}) else: subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment}) subtitles.sort(key=lambda x: [not x['sync'], not x['lang']['name'] == PreferredSub]) for s in subtitles: append_subtitle(s)
def download(link, episode=""): subtitle_list = [] exts = [".srt", ".sub", ".txt", ".smi", ".ssa", ".ass"] downloadlink_pattern = "...<a href=\"(.+?)\" rel=\"nofollow\" onclick=\"DownloadSubtitle" uid = uuid.uuid4() tempdir = os.path.join(__temp__, unicode(uid)) xbmcvfs.mkdirs(tempdir) content, response_url = geturl(link) match = re.compile(downloadlink_pattern).findall(content) if match: downloadlink = main_url + match[0] viewstate = 0 previouspage = 0 subtitleid = 0 typeid = "zip" filmid = 0 postparams = urllib.urlencode( {'__EVENTTARGET': 's$lc$bcr$downloadLink', '__EVENTARGUMENT': '', '__VIEWSTATE': viewstate, '__PREVIOUSPAGE': previouspage, 'subtitleId': subtitleid, 'typeId': typeid, 'filmId': filmid}) class MyOpener(urllib.FancyURLopener): version = ("User-Agent=Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) " "Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)") my_urlopener = MyOpener() my_urlopener.addheader('Referer', link) log(__name__, "Fetching subtitles using url '%s' with referer header '%s' and post parameters '%s'" % ( downloadlink, link, postparams)) response = my_urlopener.open(downloadlink, postparams) local_tmp_file = os.path.join(tempdir, "subscene.xxx") packed = False try: log(__name__, "Saving subtitles to '%s'" % local_tmp_file) local_file_handle = xbmcvfs.File(local_tmp_file, "wb") local_file_handle.write(response.read()) local_file_handle.close() # Check archive type (rar/zip/else) through the file header (rar=Rar!, zip=PK) myfile = xbmcvfs.File(local_tmp_file, "rb") myfile.seek(0,0) if myfile.read(1) == 'R': typeid = "rar" packed = True log(__name__, "Discovered RAR Archive") else: myfile.seek(0,0) if myfile.read(1) == 'P': typeid = "zip" packed = True log(__name__, "Discovered ZIP Archive") else: typeid = "srt" packed = False log(__name__, "Discovered a non-archive file") myfile.close() local_tmp_file = os.path.join(tempdir, "subscene." + typeid) xbmcvfs.rename(os.path.join(tempdir, "subscene.xxx"), local_tmp_file) log(__name__, "Saving to %s" % local_tmp_file) except: log(__name__, "Failed to save subtitle to %s" % local_tmp_file) if packed: xbmc.sleep(500) xbmc.executebuiltin(('XBMC.Extract("%s","%s")' % (local_tmp_file, tempdir,)).encode('utf-8'), True) episode_pattern = None if episode != '': episode_pattern = re.compile(get_episode_pattern(episode), re.IGNORECASE) for dir in xbmcvfs.listdir(tempdir)[0]: for file in xbmcvfs.listdir(os.path.join(tempdir, dir))[1]: if os.path.splitext(file)[1] in exts: log(__name__, 'match '+episode+' '+file) if episode_pattern and not episode_pattern.search(file): continue log(__name__, "=== returning subtitle file %s" % file) subtitle_list.append(os.path.join(tempdir, dir, file)) for file in xbmcvfs.listdir(tempdir)[1]: if os.path.splitext(file)[1] in exts: log(__name__, 'match '+episode+' '+file) if episode_pattern and not episode_pattern.search(file): continue log(__name__, "=== returning subtitle file %s" % file) subtitle_list.append(os.path.join(tempdir, file)) if len(subtitle_list) == 0: if episode: xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__, __language__(32002))).encode('utf-8')) else: xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__, __language__(32003))).encode('utf-8')) return subtitle_list
def download(link, episode=""): subtitle_list = [] exts = [".srt", ".sub", ".txt", ".smi", ".ssa", ".ass"] downloadlink_pattern = "...<a href=\"(.+?)\" rel=\"nofollow\" onclick=\"DownloadSubtitle" uid = uuid.uuid4() tempdir = os.path.join(__temp__, unicode(uid)) xbmcvfs.mkdirs(tempdir) content, response_url = geturl(link) match = re.compile(downloadlink_pattern).findall(content) if match: downloadlink = main_url + match[0] viewstate = 0 previouspage = 0 subtitleid = 0 typeid = "zip" filmid = 0 postparams = urllib.urlencode( {'__EVENTTARGET': 's$lc$bcr$downloadLink', '__EVENTARGUMENT': '', '__VIEWSTATE': viewstate, '__PREVIOUSPAGE': previouspage, 'subtitleId': subtitleid, 'typeId': typeid, 'filmId': filmid}) useragent = ("User-Agent=Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) " "Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)") headers = {'User-Agent': useragent, 'Referer': link} log(__name__, "Fetching subtitles using url '%s' with referer header '%s' and post parameters '%s'" % ( downloadlink, link, postparams)) request = urllib2.Request(downloadlink, postparams, headers) response = urllib2.urlopen(request) if response.getcode() != 200: log(__name__, "Failed to download subtitle file") return subtitle_list local_tmp_file = os.path.join(tempdir, "subscene.xxx") packed = False try: log(__name__, "Saving subtitles to '%s'" % local_tmp_file) local_file_handle = xbmcvfs.File(local_tmp_file, "wb") local_file_handle.write(response.read()) local_file_handle.close() # Check archive type (rar/zip/else) through the file header (rar=Rar!, zip=PK) myfile = xbmcvfs.File(local_tmp_file, "rb") myfile.seek(0,0) if myfile.read(1) == 'R': typeid = "rar" packed = True log(__name__, "Discovered RAR Archive") else: myfile.seek(0,0) if myfile.read(1) == 'P': typeid = "zip" packed = True log(__name__, "Discovered ZIP Archive") else: typeid = "srt" packed = False log(__name__, "Discovered a non-archive file") myfile.close() local_tmp_file = os.path.join(tempdir, "subscene." + typeid) xbmcvfs.rename(os.path.join(tempdir, "subscene.xxx"), local_tmp_file) log(__name__, "Saving to %s" % local_tmp_file) except: log(__name__, "Failed to save subtitle to %s" % local_tmp_file) if packed: xbmc.sleep(500) xbmc.executebuiltin(('XBMC.Extract("%s","%s")' % (local_tmp_file, tempdir,)).encode('utf-8'), True) episode_pattern = None if episode != '': episode_pattern = re.compile(get_episode_pattern(episode), re.IGNORECASE) for dir in xbmcvfs.listdir(tempdir)[0]: for file in xbmcvfs.listdir(os.path.join(tempdir, dir))[1]: if os.path.splitext(file)[1] in exts: log(__name__, 'match '+episode+' '+file) if episode_pattern and not episode_pattern.search(file): continue log(__name__, "=== returning subtitle file %s" % file) subtitle_list.append(os.path.join(tempdir, dir, file)) for file in xbmcvfs.listdir(tempdir)[1]: if os.path.splitext(file)[1] in exts: log(__name__, 'match '+episode+' '+file) if episode_pattern and not episode_pattern.search(file): continue log(__name__, "=== returning subtitle file %s" % file) subtitle_list.append(os.path.join(tempdir, file)) if len(subtitle_list) == 0: if episode: xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__, __language__(32002))).encode('utf-8')) else: xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__, __language__(32003))).encode('utf-8')) return subtitle_list
def getallsubs(url, allowed_languages, filename="", episode=""): subtitle_pattern = ("<td class=\"a1\">\s+<a href=\"(?P<link>/subtitles/[^\"]+)\">\s+" "<span class=\"[^\"]+ (?P<quality>\w+-icon)\">\s+(?P<language>[^\r\n\t]+)\s+</span>\s+" "<span>\s+(?P<filename>[^\r\n\t]+)\s+</span>\s+" "</a>\s+</td>\s+" "<td class=\"[^\"]+\">\s+(?P<numfiles>[^\r\n\t]*)\s+</td>\s+" "<td class=\"(?P<hiclass>[^\"]+)\">" "(?:.*?)<td class=\"a6\">\s+<div>\s+(?P<comment>[^\"]+) \s*</div>") codes = get_language_codes(allowed_languages) if len(codes) < 1: xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__, __language__(32004))).encode('utf-8')) return log(__name__, 'LanguageFilter='+','.join(codes)) content, response_url = geturl(url, 'LanguageFilter='+','.join(codes)) if content is None: return subtitles = [] h = HTMLParser.HTMLParser() episode_regex = None if episode != "": episode_regex = re.compile(get_episode_pattern(episode), re.IGNORECASE) log(__name__, "regex: %s" % get_episode_pattern(episode)) for matches in re.finditer(subtitle_pattern, content, re.IGNORECASE | re.DOTALL): numfiles = 1 if matches.group('numfiles') != "": numfiles = int(matches.group('numfiles')) languagefound = matches.group('language') language_info = subscene_languages[languagefound] if language_info and language_info['3let'] in allowed_languages: link = main_url + matches.group('link') subtitle_name = string.strip(matches.group('filename')) hearing_imp = (matches.group('hiclass') == "a41") rating = '0' if matches.group('quality') == "bad-icon": continue if matches.group('quality') == "positive-icon": rating = '5' comment = re.sub("[\r\n\t]+", " ", h.unescape(string.strip(matches.group('comment')))) sync = False if filename != "" and string.lower(filename) == string.lower(subtitle_name): sync = True if episode != "": # log(__name__, "match: "+subtitle_name) if episode_regex.search(subtitle_name): subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment}) elif numfiles > 2: subtitle_name = subtitle_name + ' ' + (__language__(32001) % int(matches.group('numfiles'))) subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment, 'episode': episode}) else: subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment}) subtitles.sort(key=lambda x: [not x['sync'], not x['lang']['name'] == PreferredSub]) for s in subtitles: append_subtitle(s)
def getallsubs(url, allowed_languages, filename="", episode=""): subtitle_pattern = ("<td class=\"a1\">\s+<a href=\"(?P<link>/subtitles/[^\"]+)\">\s+" "<span class=\"[^\"]+ (?P<quality>\w+-icon)\">\s+(?P<language>[^\r\n\t]+)\s+</span>\s+" "<span>\s+(?P<filename>[^\r\n\t]+)\s+</span>\s+" "</a>\s+</td>\s+" "<td class=\"[^\"]+\">\s+(?P<numfiles>[^\r\n\t]*)\s+</td>\s+" "<td class=\"(?P<hiclass>[^\"]+)\">" "(?:.*?)<td class=\"a6\">\s+<div>\s+(?P<comment>[^\"]+) \s*</div>") codes = get_language_codes(allowed_languages) if len(codes) < 1: _xbmc_notification(32004) return log(__name__, 'LanguageFilter='+','.join(codes)) #content, response_url = geturl(url, 'LanguageFilter='+','.join(codes)) content, response_url = geturl(url) if content is None: log(__name__, 'response empty') return subtitles = [] h = HTMLParser.HTMLParser() episode_regex = None any_episode_regex = None if episode != "": episode_regex = re.compile(get_episode_pattern(episode), re.IGNORECASE) any_episode_regex = re.compile("(?:s[0-9]{2}e[0-9]{2}|\D[0-9]{1,2}x[0-9]{2})", re.IGNORECASE) log(__name__, "regex: %s" % get_episode_pattern(episode)) for matches in re.finditer(subtitle_pattern, content, re.IGNORECASE | re.DOTALL): log(__name__, "Found subtitle: %s" % matches.groupdict()) numfiles = -1 if matches.group('numfiles') != "": numfiles = int(matches.group('numfiles')) languagefound = matches.group('language') language_info = None if languagefound in subscene_languages: language_info = subscene_languages[languagefound] else: log(__name__, "not in subscene_languages: %s" % languagefound) continue log(__name__, "language_info: %s, language_info['3let']: %s, allowed_languages: %s" % (language_info, language_info['3let'], allowed_languages)) if language_info is not None and language_info['3let'] in allowed_languages: link = main_url + matches.group('link') subtitle_name = string.strip(matches.group('filename')) hearing_imp = (matches.group('hiclass') == "a41") rating = '0' if matches.group('quality') == "bad-icon": continue if matches.group('quality') == "positive-icon": rating = '5' comment = re.sub("[\r\n\t]+", " ", h.unescape(string.strip(matches.group('comment')))) sync = False if filename != "" and string.lower(filename) == string.lower(subtitle_name): sync = True if episode != "": # log(__name__, "match: "+subtitle_name) # matching episode if episode_regex.search(subtitle_name): subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment}) # multiple files elif numfiles > 2: subtitle_name = subtitle_name + ' ' + (_xmbc_localized_string_utf8(32001) % int(matches.group('numfiles'))) subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment, 'episode': episode}) # not matching any episode (?) elif not any_episode_regex.search(subtitle_name): subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment, 'episode': episode}) else: subtitles.append({'rating': rating, 'filename': subtitle_name, 'sync': sync, 'link': link, 'lang': language_info, 'hearing_imp': hearing_imp, 'comment': comment}) subtitles.sort(key=lambda x: [not x['sync'], not x['lang']['name'] == PreferredSub]) log(__name__, "subtitles count: %s" % len(subtitles)) for s in subtitles: append_subtitle(s)
def download(link, episode=""): log(__name__, "Download Subscene") subtitle_list = [] exts = [".srt", ".sub", ".txt", ".smi", ".ssa", ".ass"] downloadlink_pattern = "...<a href=\"(.+?)\" rel=\"nofollow\" onclick=\"DownloadSubtitle" uid = uuid.uuid4() tempdir = os.path.join(TEMP, unicode(uid)) xbmcvfs.mkdirs(tempdir) content, resp = geturl(link) match = re.compile(downloadlink_pattern).findall(content) if match: downloadlink = main_url + match[0] viewstate = 0 previouspage = 0 subtitleid = 0 typeid = "zip" filmid = 0 jsondata = {"url": downloadlink} data = json.dumps(jsondata) request = urllib2.Request( "https://us-central1-kodi-thuongtin.cloudfunctions.net/subscene2", data, { 'Content-Type': 'application/json', 'Content-Length': len(data) }) response = urllib2.urlopen(request) if response.getcode() != 200: log(__name__, "Failed to download subtitle file") return subtitle_list local_tmp_file = os.path.join(tempdir, "subscene.xxx") packed = False try: log(__name__, "Saving subtitles to '%s'" % local_tmp_file) local_file_handle = xbmcvfs.File(local_tmp_file, "wb") local_file_handle.write(response.read()) # local_file_handle.write(content) local_file_handle.close() # Check archive type (rar/zip/else) through the file header (rar=Rar!, zip=PK) myfile = xbmcvfs.File(local_tmp_file, "rb") myfile.seek(0, 0) if myfile.read(1) == 'R': typeid = "rar" packed = True log(__name__, "Discovered RAR Archive") else: myfile.seek(0, 0) if myfile.read(1) == 'P': typeid = "zip" packed = True log(__name__, "Discovered ZIP Archive") else: typeid = "srt" packed = False log(__name__, "Discovered a non-archive file") myfile.close() local_tmp_file = os.path.join(tempdir, "subscene." + typeid) xbmcvfs.rename(os.path.join(tempdir, "subscene.xxx"), local_tmp_file) log(__name__, "Saving to %s" % local_tmp_file) except: log(__name__, "Failed to save subtitle to %s" % local_tmp_file) if packed: xbmc.sleep(500) xbmc.executebuiltin(('XBMC.Extract("%s","%s")' % ( local_tmp_file, tempdir, )).encode('utf-8'), True) episode_pattern = None if episode != '': episode_pattern = re.compile(get_episode_pattern(episode), re.IGNORECASE) for dir in xbmcvfs.listdir(tempdir)[0]: for file in xbmcvfs.listdir(os.path.join(tempdir, dir))[1]: if os.path.splitext(file)[1] in exts: log(__name__, 'match ' + episode + ' ' + file) if episode_pattern and not episode_pattern.search(file): continue log(__name__, "=== returning subtitle file %s" % file) subtitle_list.append(os.path.join(tempdir, dir, file)) for file in xbmcvfs.listdir(tempdir)[1]: if os.path.splitext(file)[1] in exts: log(__name__, 'match ' + episode + ' ' + file) if episode_pattern and not episode_pattern.search(file): continue log(__name__, "=== returning subtitle file %s" % file) subtitle_list.append(os.path.join(tempdir, file)) if len(subtitle_list) == 0: if episode: _xbmc_notification(32002) else: _xbmc_notification(32003) return subtitle_list
def download(link, episode=""): subtitle_list = [] exts = [".srt", ".sub", ".txt", ".smi", ".ssa", ".ass"] downloadlink_pattern = "...<a href=\"(.+?)\" rel=\"nofollow\" onclick=\"DownloadSubtitle" uid = uuid.uuid4() if sys.version_info.major == 3: tempdir = os.path.join(__temp__, str(uid)) else: tempdir = os.path.join(__temp__, unicode(uid)) try: os.makedirs(tempdir) except OSError: log(__name__, "Failed to create temp directory " + tempdir) else: log(__name__, "Successfully created temp directory " + tempdir) content, response_url = geturl(link) content = str(content) match = re.compile(downloadlink_pattern).findall(content) if match: downloadlink = main_url + match[0] viewstate = 0 previouspage = 0 subtitleid = 0 typeid = "zip" filmid = 0 if sys.version_info.major == 3: postparams = urllib.parse.urlencode( {'__EVENTTARGET': 's$lc$bcr$downloadLink', '__EVENTARGUMENT': '', '__VIEWSTATE': viewstate, '__PREVIOUSPAGE': previouspage, 'subtitleId': subtitleid, 'typeId': typeid, 'filmId': filmid}).encode() else: postparams = urllib.urlencode( {'__EVENTTARGET': 's$lc$bcr$downloadLink', '__EVENTARGUMENT': '', '__VIEWSTATE': viewstate, '__PREVIOUSPAGE': previouspage, 'subtitleId': subtitleid, 'typeId': typeid, 'filmId': filmid}) # useragent = ("User-Agent=Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) " # "Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)") # useragent = ("User-Agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.89 Safari/537.36") useragent = ("Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0") headers = {'User-Agent': useragent, 'Referer': link} log(__name__, "Fetching subtitles using url '%s' with referer header '%s' and post parameters '%s'" % ( downloadlink, link, postparams)) local_tmp_file = os.path.join(tempdir, "subscene.xxx") time_interval = 3 num_of_retries = 5 if sys.version_info.major == 3: request = urllib.request.Request(downloadlink, postparams, headers) for _ in range(num_of_retries): try: response = urllib.request.urlopen(request) except: typ, val, tb = sys.exc_info() log(__name__, traceback.format_exception(typ, val, tb)) num_of_retries -= 1 # If there aren't any retries - exit loop and raise error if not num_of_retries: log(__name__, "Download failure, giving up") pass log(__name__, "Download failure, %s attempts left" % num_of_retries) time.sleep(time_interval) else: local_tmp_file = os.path.join(tempdir, "subscene.xxx") log(__name__, "Saving downloaded file to '%s'" % local_tmp_file) local_file_handle = xbmcvfs.File(local_tmp_file, "w") local_file_handle.write(bytearray(response.read())) local_file_handle.close() break else: log(__name__, "Download failure, returning empty subtitle list") return subtitle_list else: request = urllib2.Request(downloadlink, postparams, headers) for _ in range(num_of_retries): try: response = urllib2.urlopen(request) except urllib2.URLError: typ, val, tb = sys.exc_info() log(__name__, traceback.format_exception(typ, val, tb)) num_of_retries -= 1 # If there aren't any retries - exit loop and raise error if not num_of_retries: log(__name__, "Download failure, giving up") raise log(__name__, "Download failure, %s attempts left" % num_of_retries) time.sleep(time_interval) else: log(__name__, "Saving downloaded file to '%s'" % local_tmp_file) local_file_handle = xbmcvfs.File(local_tmp_file, "wb") local_file_handle.write(response.read()) local_file_handle.close() break else: raise if response is not None and response.getcode() != 200: log(__name__, "Failed to download subtitle file, HTTP error %s" % response.getcode()) return subtitle_list packed = False try: log(__name__, "Checking archive type") # Check archive type (rar/zip/else) through the file header (rar=Rar!, zip=PK) myfile = xbmcvfs.File(local_tmp_file, "rb") myfile.seek(0, 0) if myfile.read(1) == 'R': typeid = "rar" packed = True log(__name__, "Discovered RAR Archive") else: myfile.seek(0, 0) if myfile.read(1) == 'P': typeid = "zip" packed = True log(__name__, "Discovered ZIP Archive") else: typeid = "srt" packed = False log(__name__, "Discovered a non-archive file") myfile.close() local_tmp_file = os.path.join(tempdir, "subscene." + typeid) xbmcvfs.rename(os.path.join(tempdir, "subscene.xxx"), local_tmp_file) log(__name__, "Saving to %s" % local_tmp_file) except: log(__name__, "Failed to save subtitle to %s" % local_tmp_file) if packed: xbmc.sleep(500) if (sys.platform == "linux" or sys.platform == "linux2") and not 'ANDROID_ROOT' in list(os.environ.keys()): platform = "linux" log(__name__, "Platform identified as Linux") else: platform = "non-linux" log(__name__, "Platform identified as Non-Linux") if sys.version_info.major == 3: log(__name__, "Checking '%s' for subtitle files to copy" % local_tmp_file) if platform == "linux": (dirs, files) = xbmcvfs.listdir('%s' % xbmcvfs.translatePath(local_tmp_file)) if len(files) == 0: log(__name__, "Trying trailing slash") (dirs, files) = xbmcvfs.listdir('%s/' % xbmcvfs.translatePath(local_tmp_file)) if len(files) == 0: log(__name__, "Trying zip://") (dirs, files) = xbmcvfs.listdir('zip://%s/' % urllib.parse.quote_plus(local_tmp_file)) else: # Kodi on windows and possibly Android requires archive:// protocol, so testing both log(__name__, "Trying archive:\\\\") (dirs, files) = xbmcvfs.listdir('archive:\\\\%s' % xbmcvfs.translatePath(urllib.parse.quote_plus(local_tmp_file))) if len(files) == 0: log(__name__, "Trying directly") (dirs, files) = xbmcvfs.listdir('%s' % xbmcvfs.translatePath(local_tmp_file)) if len(files) == 0: log(__name__, "Trying zip://") (dirs, files) = xbmcvfs.listdir('zip://%s/' % urllib.parse.quote_plus(local_tmp_file)) for file in files: dest = os.path.join(tempdir, file) log(__name__, "=== Found subtitle file %s" % dest) if platform == "linux": # Kodi on linux does not understand 'archive://' protocol src = os.path.join(local_tmp_file, file) log(__name__, "trying to copy '%s' to '%s'" % (src, dest)) if not xbmcvfs.copy(src, dest): log(__name__, "copying failed") else: log(__name__, "copying succeeded") else: # Kodi on windows and possibly Android requires archive:// protocol, so testing both src = xbmcvfs.translatePath(os.path.join("archive:\\\\%s" % urllib.parse.quote_plus(local_tmp_file), file)) log(__name__, "trying to copy '%s' to '%s'" % (src, dest)) if not xbmcvfs.copy(src, dest): log(__name__, "copying failed") # trying again src = os.path.join(local_tmp_file, file) log(__name__, "trying to copy '%s' to '%s'" % (src, dest)) if not xbmcvfs.copy(src, dest): # trying yet again src = 'zip://%s/' % urllib.parse.quote_plus(os.path.join(local_tmp_file, file)) if not xbmcvfs.copy(src, dest): log(__name__, "copying failed") else: log(__name__, "copying succeeded using zip://") else: log(__name__, "copying succeeded using directly") else: log(__name__, "copying succeeded using archive:\\\\") subtitle_list.append(dest) else: log(__name__, "Extracting '%s' to '%s'" % (local_tmp_file, tempdir)) xbmc.executebuiltin(('XBMC.Extract("%s","%s")' % (local_tmp_file, tempdir,)).encode('utf-8'), True) for file in xbmcvfs.listdir(local_tmp_file)[1]: file = os.path.join(tempdir, file) if os.path.splitext(file)[1] in exts: log(__name__, "=== Found subtitle file %s" % file) subtitle_list.append(file) episode_pattern = None if episode != '': episode_pattern = re.compile(get_episode_pattern(episode), re.IGNORECASE) log(__name__, "Checking temp dir subfolders for subtitle files...") for dir in xbmcvfs.listdir(tempdir)[0]: log(__name__, "Check dir subfolder %s" % dir) for file in xbmcvfs.listdir(os.path.join(tempdir, dir))[1]: log(__name__, "Check dir subfolder file %s" % file) if os.path.splitext(file)[1] in exts: log(__name__, 'match '+episode+' '+file) if episode_pattern and not episode_pattern.search(file): continue log(__name__, "=== returning subtitle file %s" % file) subtitle_list.append(os.path.join(tempdir, dir, file)) log(__name__, "Checking temp dir for subtitle files...") for file in xbmcvfs.listdir(tempdir)[1]: log(__name__, "Check dir file %s" % file) if os.path.splitext(file)[1] in exts: log(__name__, 'match '+episode+' '+file) if episode_pattern and not episode_pattern.search(file): continue log(__name__, "=== returning subtitle file %s" % file) subtitle_list.append(os.path.join(tempdir, file)) if len(subtitle_list) == 0: if sys.version_info.major == 3: if episode: log(__name__, "=== Could not find matching episode in subtitle pack") xbmc.executebuiltin('Notification(%s,%s)' % (__scriptname__, __language__(32002))) else: log(__name__, "=== Download didn't contain a subtitle file") xbmc.executebuiltin('Notification(%s,%s)' % (__scriptname__, __language__(32003))) else: if episode: log(__name__, "=== Could not find matching episode in subtitle pack") xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__, __language__(32002))).encode('utf-8')) else: log(__name__, "=== Download didn't contain a subtitle file") xbmc.executebuiltin((u'Notification(%s,%s)' % (__scriptname__, __language__(32003))).encode('utf-8')) return subtitle_list