diff --git a/CHANGELOG.md b/CHANGELOG.md index abe3196..52ce466 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +* [v0.0.12](https://github.com/newt-sc/a4kSubtitles/releases/tag/service.subtitles.a4ksubtitles%2Fservice.subtitles.a4ksubtitles-0.0.12): + * Removal of ads (Experimental) + * Fix extract issue when file contains unicode symbols + * [v0.0.11](https://github.com/newt-sc/a4kSubtitles/releases/tag/service.subtitles.a4ksubtitles%2Fservice.subtitles.a4ksubtitles-0.0.11): * Changelog in KODI diff --git a/a4kSubtitles/download.py b/a4kSubtitles/download.py index 5e53185..279814d 100644 --- a/a4kSubtitles/download.py +++ b/a4kSubtitles/download.py @@ -6,30 +6,40 @@ def __download(core, filepath, request): with open(filepath, 'wb') as f: core.shutil.copyfileobj(r.raw, f) -def __extract(core, archivepath, filename, language): +def __extract(core, archivepath, filename, lang): path = core.utils.quote_plus(archivepath) ext = core.os.path.splitext(filename)[1].lower() (dirs, files) = core.kodi.xbmcvfs.listdir('archive://%s' % path) subfile = filename for file in files: + if core.utils.PY2: + file = file.decode('utf8') if file.lower().endswith(ext): subfile = file break src = 'archive://' + path + '/' + subfile - subfile = __insert_language_code_in_filename(core, filename, language) + subfile = __insert_lang_code_in_filename(core, filename, lang) dest = core.os.path.join(core.utils.temp_dir, subfile) core.kodi.xbmcvfs.copy(src, dest) return dest -def __insert_language_code_in_filename(core, filename, language_name): +def __insert_lang_code_in_filename(core, filename, lang): filename_chunks = filename.split('.') - lang_code = core.kodi.xbmc.convertLanguage(language_name, - core.kodi.xbmc.ISO_639_1) + lang_code = core.kodi.xbmc.convertLanguage(lang, core.kodi.xbmc.ISO_639_1) filename_chunks.insert(-1, lang_code) return '.'.join(filename_chunks) +def __cleanup(core, filepath): + with open(filepath, 'r') as f: + sub_contents = f.read() + + sub_contents = core.utils.cleanup_subtitles(sub_contents) + + with open(filepath, 'w') as f: + f.write(sub_contents) + def download(core, params): core.logger.debug(lambda: core.json.dumps(params, indent=2)) @@ -48,6 +58,9 @@ def download(core, params): __download(core, archivepath, request) filepath = __extract(core, archivepath, filename, language) + if core.kodi.get_bool_setting('general.remove_ads'): + __cleanup(core, filepath) + if core.api_mode_enabled: return filepath listitem = core.kodi.xbmcgui.ListItem(label=filepath) diff --git a/a4kSubtitles/lib/utils.py b/a4kSubtitles/lib/utils.py index a8cf7bd..07c4325 100644 --- a/a4kSubtitles/lib/utils.py +++ b/a4kSubtitles/lib/utils.py @@ -2,7 +2,9 @@ import os import sys +import re from .kodi import addon_profile +from . import logger try: from urlparse import unquote, parse_qsl @@ -10,10 +12,54 @@ except ImportError: from urllib.parse import quote_plus, unquote, parse_qsl +__url_regex = r'(([a-zA-Z0-9][a-zA-Z0-9-]{1,5}[a-zA-Z0-9]\.[a-zA-Z]{2,20})|(opensubtitles))\.[a-zA-Z]{2,5}' + PY2 = sys.version_info[0] == 2 PY3 = not PY2 + temp_dir = os.path.join(addon_profile, 'temp') def get_all_relative_py_files(file): files = os.listdir(os.path.dirname(file)) return [filename[:-3] for filename in files if not filename.startswith('__') and filename.endswith('.py')] + +def cleanup_subtitles(sub_contents): + all_lines = sub_contents.split('\n') + cleaned_lines = [] + buffer = [] + garbage = False + + if all_lines[0].strip() != '': + all_lines.insert(0, '') + + if all_lines[-1].strip() != '': + all_lines.append('') + + for line in all_lines: + line = line.strip() + + if garbage and line != '': + continue + + garbage = False + + if line == '': + if len(buffer) > 0: + buffer.insert(0, '') + cleaned_lines.extend(buffer) + buffer = [] + continue + + if re.search(__url_regex, line, re.IGNORECASE): + logger.notice('(detected ad) %s' % line) + if not re.match(r'^\{\d+\}\{\d+\}', line): + garbage = True + buffer = [] + continue + + buffer.append(line) + + if cleaned_lines[0] == '': + cleaned_lines.pop(0) + + return '\n'.join(cleaned_lines) diff --git a/addon.xml b/addon.xml index 8c3fc41..8e3cc19 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ @@ -24,6 +24,10 @@ Supports: OpenSubtitles icon.png +[v0.0.12]: + * Removal of ads (Experimental) + * Fix extract issue when file contains unicode symbols + [v0.0.11]: * Changelog in KODI diff --git a/packages/addons.xml b/packages/addons.xml index 323eadb..603deac 100644 --- a/packages/addons.xml +++ b/packages/addons.xml @@ -4,7 +4,7 @@ @@ -27,6 +27,10 @@ Supports: OpenSubtitles icon.png +[v0.0.12]: + * Removal of ads (Experimental) + * Fix extract issue when file contains unicode symbols + [v0.0.11]: * Changelog in KODI diff --git a/packages/addons.xml.crc b/packages/addons.xml.crc index c038bc5..53105ad 100644 --- a/packages/addons.xml.crc +++ b/packages/addons.xml.crc @@ -1 +1 @@ -7ff84516b598715f4820edd98ddf126bed2d6cd9 \ No newline at end of file +fdcca76e16ba5e22b6bf308c5a57a5defb52011b \ No newline at end of file diff --git a/resources/language/resource.language.en_gb/strings.po b/resources/language/resource.language.en_gb/strings.po index 0b48c1b..0be4c1f 100644 --- a/resources/language/resource.language.en_gb/strings.po +++ b/resources/language/resource.language.en_gb/strings.po @@ -40,3 +40,7 @@ msgstr "" msgctxt "#32105" msgid "Enabled" msgstr "" + +msgctxt "#32106" +msgid "Remove ads (Experimental)" +msgstr "" diff --git a/resources/settings.xml b/resources/settings.xml index fad9cb6..e499f19 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -7,6 +7,7 @@ + diff --git a/scripts/validate_commit.py b/scripts/validate_commit.py index 564f806..03c3a10 100644 --- a/scripts/validate_commit.py +++ b/scripts/validate_commit.py @@ -12,7 +12,7 @@ if is_pull_request: os.system('git fetch --deepen=1 --no-tags --quiet') - sha = github['event'].get('after', github['event']['pull_request']['sha']) + sha = github['event']['pull_request']['head']['sha'] command_args = ['git', 'log', '--format=%B', '-n', '1', sha] else: command_args = ['git', 'show', '-s', '--format=%s']