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.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)