From 101b0ff19342c53aed56e55e2dd733f0bc89c09f Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:54:18 +1000 Subject: [PATCH 1/9] Add files via upload --- INFO | 13 ++++ Language.py | 133 ++++++++++++++++++++++++++++++++++++ OpenSubtitles.py | 112 +++++++++++++++++++++++++++++++ subtitle.py | 171 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 429 insertions(+) create mode 100644 INFO create mode 100644 Language.py create mode 100644 OpenSubtitles.py create mode 100644 subtitle.py diff --git a/INFO b/INFO new file mode 100644 index 0000000..8c396a2 --- /dev/null +++ b/INFO @@ -0,0 +1,13 @@ +{ + "id": "com.synology.OpenSubtitles", + "title": "OpenSubtitles", + "description": "", + "version": 1.0, + "site": "https://www.opensubtitles.com/", + "entry_file": "subtitle.py", + "category": "subtitle", + "language": ["eng"], + "test_example": { + "title": "Harry Potter And The Philosophers stone" + } +} diff --git a/Language.py b/Language.py new file mode 100644 index 0000000..e48ae53 --- /dev/null +++ b/Language.py @@ -0,0 +1,133 @@ +# get from api https://api.opensubtitles.com/api/v1/infos/languages +_LANGUAGE_CODE_TO_NAME_MAPPING = { + "af": "Afrikaans", + "sq": "Albanian", + "ar": "Arabic", + "an": "Aragonese", + "hy": "Armenian", + "at": "Asturian", + "eu": "Basque", + "be": "Belarusian", + "bn": "Bengali", + "bs": "Bosnian", + "br": "Breton", + "bg": "Bulgarian", + "my": "Burmese", + "ca": "Catalan", + "zh-cn": "Chinese (simplified)", + "cs": "Czech", + "da": "Danish", + "nl": "Dutch", + "en": "English", + "eo": "Esperanto", + "et": "Estonian", + "fi": "Finnish", + "fr": "French", + "ka": "Georgian", + "de": "German", + "gl": "Galician", + "el": "Greek", + "he": "Hebrew", + "hi": "Hindi", + "hr": "Croatian", + "hu": "Hungarian", + "is": "Icelandic", + "id": "Indonesian", + "it": "Italian", + "ja": "Japanese", + "kk": "Kazakh", + "km": "Khmer", + "ko": "Korean", + "lv": "Latvian", + "lt": "Lithuanian", + "lb": "Luxembourgish", + "mk": "Macedonian", + "ml": "Malayalam", + "ms": "Malay", + "ma": "Manipuri", + "mn": "Mongolian", + "no": "Norwegian", + "oc": "Occitan", + "fa": "Persian", + "pl": "Polish", + "pt-pt": "Portuguese", + "ru": "Russian", + "sr": "Serbian", + "si": "Sinhalese", + "sk": "Slovak", + "sl": "Slovenian", + "es": "Spanish", + "sw": "Swahili", + "sv": "Swedish", + "sy": "Syriac", + "ta": "Tamil", + "te": "Telugu", + "tl": "Tagalog", + "th": "Thai", + "tr": "Turkish", + "uk": "Ukrainian", + "ur": "Urdu", + "uz": "Uzbek", + "vi": "Vietnamese", + "ro": "Romanian", + "pt-br": "Portuguese (Brazilian)", + "me": "Montenegrin", + "zh-tw": "Chinese (traditional)", + "ze": "Chinese bilingual", + "nb": "Norwegian Bokmal", + "se": "Northern Sami", +} + +# opensubtitles language setting mapping +# see SYNO.SDS.VideoStation2.Setting.AdvancedPanel.SubtitleSet.LANGUAGE_MAPPING +# and SYNO.SDS.VideoStation2.Setting.AdvancedPanel.SubtitleSet.EXTRA_LANGUAGE +_VS_OPENSUBTITLES_LANGUAGE_SETTING_MAPPING = { + "cze": "cs", + "dan": "da", + "eng": "en", + "fre": "fr", + "ger": "de", + "hun": "hu", + "ita": "it", + "jpn": "ja", + "kor": "ko", + "dut": "nl", + "nor": "no", + "pol": "pl", + "pob": "pt-br", + "por": "pt-pt", + "rus": "ru", + "spa": "es", + "swe": "sv", + "tha": "th", + "tur": "tk", + "chi": "zh-cn", + "fin": "fi", + "ara": "ar", + "heb": "he", + "ell": "el", + "rum": "ro", + "scc": "sr", + "hrv": "hr", + "bul": "bg", + "slv": "sl", + "bos": "bs", +} + + +def get_language_name(language_code): + if language_code in _LANGUAGE_CODE_TO_NAME_MAPPING: + return _LANGUAGE_CODE_TO_NAME_MAPPING[language_code] + return language_code + + +def convert_language_code_639_2B_to_639_1(language_code): + language_list = language_code.split(",") + converted_language_list = [] + for lang in language_list: + if lang in _VS_OPENSUBTITLES_LANGUAGE_SETTING_MAPPING: + converted_language_list.append( + _VS_OPENSUBTITLES_LANGUAGE_SETTING_MAPPING[lang] + ) + + return ",".join(converted_language_list) diff --git a/OpenSubtitles.py b/OpenSubtitles.py new file mode 100644 index 0000000..f60ba04 --- /dev/null +++ b/OpenSubtitles.py @@ -0,0 +1,112 @@ +import json +import urllib +import urllib.request +import subprocess + + +class OpenSubtitles: + BASE_URL = "https://api.opensubtitles.com/api/v1" + HASH_BINARY_PATH = "/var/packages/VideoStation/target/bin/video_hash" + INFO_PATH = ( + "/var/packages/VideoStation/target/subtitle_plugins/syno_opensubtitles/INFO" + ) + token = "" + apikey = "" + plugin_version = "0.0" + + def __init__(self, user, password, apikey): + self.apikey = apikey + self.login_and_set_token(user, password) + + with open(self.INFO_PATH, "r") as f: + self.plugin_version = json.load(f)["version"] + + def __query(self, url, method="GET", data=None): + result = None + timeouts = 30 + + header = { + "User-Agent": f"Videostation_Opensubtitles_Plugin v{self.plugin_version}", + "Accept": "application/json", + } + + if self.token: + header["Authorization"] = f"Bearer {self.token}" + if self.apikey: + header["Api-Key"] = self.apikey + + try: + data_encoded = None + if data: + data_encoded = urllib.parse.urlencode(data).encode("ascii") + + request = urllib.request.Request( + url=url, headers=header, data=data_encoded, method=method + ) + response = urllib.request.urlopen(request, timeout=timeouts) + result = response.read().decode("utf-8") + + except urllib.error.HTTPError as http_e: + print("http error:", http_e) + + except Exception as e: + print("exception", e) + + return result + + def __get_size_and_hash(self, file_path): + execute_array = [self.HASH_BINARY_PATH, "--opensubtitles", file_path] + p = subprocess.Popen( + execute_array, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + out, err = p.communicate() + + if err: + return False + + response_string = str(out, "utf-8") + return json.loads(response_string) + + def login_and_set_token(self, user, password): + url = f"{self.BASE_URL}/login" + data = {"username": user, "password": password} + login_result = self.__query(url=url, method="POST", data=data) + self.token = json.loads(login_result)["token"] + + def search_subtitle_by_file_hash(self, path, lang): + info = self.__get_size_and_hash(path) + return self.__search_subtitle_by_hash(info["hash"], lang) + + def __search_subtitle_by_hash(self, hash, lang): + url = f"{self.BASE_URL}/subtitles?moviehash={hash}&languages={lang}" + result = self.__query(url=url, method="GET") + return json.loads(result) + + def search_subtitle_by_feature_id(self, id, lang): + url = f"{self.BASE_URL}/subtitles?id={id}&languages={lang}" + result = self.__query(url=url, method="GET") + return json.loads(result) + + def search_feature_by_title(self, title): + titleEncode = urllib.parse.quote_plus(title.lower()) + url = f"{self.BASE_URL}/features?query={titleEncode}" + result = self.__query(url=url, method="GET") + return json.loads(result) + + def run_download(self, site_subtitle_id, save_file_Path): + link = self.__get__download_link(site_subtitle_id) + self.__download_link(link, save_file_Path) + + def __get__download_link(self, fileId): + url = f"{self.BASE_URL}/download" + data = {"file_id": fileId} + result = self.__query(url=url, method="POST", data=data) + return json.loads(result)["link"] + + def __download_link(self, link, save_file_Path): + result = self.__query(url=link, method="GET") + + with open(save_file_Path, "w", encoding="utf-8") as f: + if not result: + result = "" + f.write(result) diff --git a/subtitle.py b/subtitle.py new file mode 100644 index 0000000..779f032 --- /dev/null +++ b/subtitle.py @@ -0,0 +1,171 @@ +import os +import json +import argparse +import OpenSubtitles +import html +import Language + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--action", type=str, required=True, help="search|download") + parser.add_argument( + "--path", + type=str, + default="", + help="search by file path, would be useless when set title", + ) + parser.add_argument( + "--title", + type=str, + default="", + help="search by title, prior than search by path", + ) + parser.add_argument("--id", type=str, default="", help="download id") + parser.add_argument( + "--lang", type=str, default="eng", help="search language, ex: eng or eng,fre" + ) + parser.add_argument( + "--limit", type=int, default=10, help="search limit, default 10" + ) + parser.add_argument("--username", type=str, required=True) + parser.add_argument("--password", type=str, default="") + parser.add_argument("--apikey", type=str, default="") + + args = parser.parse_known_args()[0] + + username = args.username + password = args.password + apikey = args.apikey + + if not password: + password = os.environ["SUBTITLE_PLUGIN_PASSWORD"] + if not apikey: + apikey = os.environ["SUBTITLE_PLUGIN_APIKEY"] + + site = OpenSubtitles.OpenSubtitles(username, password, apikey) + action = args.action + lang = Language.convert_language_code_639_2B_to_639_1(args.lang) + if not lang: + lang = "en" + path = args.path + title = args.title + limit = args.limit + + if action == "search": + items = do_search(site, title, lang, path, limit) + + process_output({"items": items, "total": len(items)}) + + elif action == "download": + id = args.id + save_file_path = do_download(site, id, path) + + process_output({"id": save_file_path}) + + else: + parser.print_help() + + +def do_search(site, title, lang, path, limit): + items = [] + if title != "": + searchFeatureResult = site.search_feature_by_title(title) + if searchFeatureResult["data"]: + feature_id = searchFeatureResult["data"][0]["id"] + search_subtitle_result = site.search_subtitle_by_feature_id( + feature_id, lang + ) + items = transform_search_subtitle_result( + search_subtitle_result, path, limit + ) + else: + search_subtitle_result = site.search_subtitle_by_file_hash(path, lang) + items = transform_search_subtitle_result(search_subtitle_result, path, limit) + + if len(items) == 0: + base_name = os.path.basename(path) + file_name = os.path.splitext(base_name)[0] + searchFeatureResult = site.search_feature_by_title(file_name) + + if searchFeatureResult["data"]: + feature_id = searchFeatureResult["data"][0]["id"] + search_subtitle_result = site.search_subtitle_by_feature_id( + feature_id, lang + ) + items = transform_search_subtitle_result( + search_subtitle_result, path, limit + ) + return items + + +def do_download(site, id, path): + directory = os.path.dirname(path) + save_file_path = f"{directory}/{id}.srt" + + language_id, site_subtitle_id = decompose_id(id) + site.run_download(site_subtitle_id, save_file_path) + return save_file_path + + +def get_subtitle_full_path(prefix): + dirname = os.path.dirname(prefix) + files = [f for f in os.listdir(dirname) if os.path.isfile(os.path.join(dirname, f))] + for f in files: + filename = os.path.join(dirname, f) + if 0 == filename.find(prefix): + return filename + return False + + +def compose_id(file_name, language_id, site_subtitle_id): + return f"{file_name}.{language_id}.{site_subtitle_id}" + + +def decompose_id(id): + split_result = id.split(".") + site_subtitle_id = split_result[len(split_result) - 1] + language_id = split_result[len(split_result) - 2] + return language_id, site_subtitle_id + + +def transform_search_subtitle_result(search_subtitle_result, file_path, limit): + items = [] + for data in search_subtitle_result["data"]: + site_subtitle_id = data["id"] + language_id = data["attributes"]["language"].lower() + base_name = os.path.basename(file_path) + file_name = os.path.splitext(base_name)[0] + file_directory = os.path.dirname(file_path) + + id = compose_id(file_name, language_id, site_subtitle_id) + + prefix = f"{file_directory}/{id}" + + subtitle_id = get_subtitle_full_path(prefix) + + item = { + "id": id, + "downloaded": bool(subtitle_id), + "filename": data["attributes"]["release"], + "language": Language.get_language_name(language_id), + "language_id": language_id, + "plugin_id": "com.synology.OpenSubtitles", + "plugin_title": "OpenSubtitles", + "subtitle_id": subtitle_id, + } + items.append(item) + if limit > 0 and len(items) >= limit: + break + return items + + +def process_output(output): + json_string = json.dumps(output, ensure_ascii=False, separators=(",", ":")) + json_string = html.unescape(json_string) + print(json_string) + + +if __name__ == "__main__": + main() + exit(0) From 1b45104475e54c309a4b78a9774dd10653e11b35 Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:55:24 +1000 Subject: [PATCH 2/9] Update CHANGES.txt --- CHANGES.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index 69e4668..b031aaf 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,2 +1,5 @@ +v2.0.0 + - Added fix for OpenSubtitles. + v1.0.0 - First release. From 64ddb09fede6ce61c5a79fb85411a8137d966ba6 Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:57:21 +1000 Subject: [PATCH 3/9] Update CHANGES.txt --- CHANGES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.txt b/CHANGES.txt index b031aaf..30d35a5 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,6 @@ v2.0.0 - Added fix for OpenSubtitles. + - You need manually download v2.0.0 because v1.0.0 does not know about the extra files. v1.0.0 - First release. From eb6041ee36128ddd5768e70c33d5628d8de5ed43 Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Fri, 30 Aug 2024 08:08:52 +1000 Subject: [PATCH 4/9] Update CHANGES.txt --- CHANGES.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 30d35a5..425096f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,6 +1,5 @@ -v2.0.0 - - Added fix for OpenSubtitles. - - You need manually download v2.0.0 because v1.0.0 does not know about the extra files. +v1.0.1 + - Bug fix for not installing AME. Issue #3 v1.0.0 - First release. From 2629727b52051d5d0d8d8dd5923f344b33e3a323 Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Fri, 30 Aug 2024 08:09:14 +1000 Subject: [PATCH 5/9] Delete Language.py --- Language.py | 133 ---------------------------------------------------- 1 file changed, 133 deletions(-) delete mode 100644 Language.py diff --git a/Language.py b/Language.py deleted file mode 100644 index e48ae53..0000000 --- a/Language.py +++ /dev/null @@ -1,133 +0,0 @@ -# get from api https://api.opensubtitles.com/api/v1/infos/languages -_LANGUAGE_CODE_TO_NAME_MAPPING = { - "af": "Afrikaans", - "sq": "Albanian", - "ar": "Arabic", - "an": "Aragonese", - "hy": "Armenian", - "at": "Asturian", - "eu": "Basque", - "be": "Belarusian", - "bn": "Bengali", - "bs": "Bosnian", - "br": "Breton", - "bg": "Bulgarian", - "my": "Burmese", - "ca": "Catalan", - "zh-cn": "Chinese (simplified)", - "cs": "Czech", - "da": "Danish", - "nl": "Dutch", - "en": "English", - "eo": "Esperanto", - "et": "Estonian", - "fi": "Finnish", - "fr": "French", - "ka": "Georgian", - "de": "German", - "gl": "Galician", - "el": "Greek", - "he": "Hebrew", - "hi": "Hindi", - "hr": "Croatian", - "hu": "Hungarian", - "is": "Icelandic", - "id": "Indonesian", - "it": "Italian", - "ja": "Japanese", - "kk": "Kazakh", - "km": "Khmer", - "ko": "Korean", - "lv": "Latvian", - "lt": "Lithuanian", - "lb": "Luxembourgish", - "mk": "Macedonian", - "ml": "Malayalam", - "ms": "Malay", - "ma": "Manipuri", - "mn": "Mongolian", - "no": "Norwegian", - "oc": "Occitan", - "fa": "Persian", - "pl": "Polish", - "pt-pt": "Portuguese", - "ru": "Russian", - "sr": "Serbian", - "si": "Sinhalese", - "sk": "Slovak", - "sl": "Slovenian", - "es": "Spanish", - "sw": "Swahili", - "sv": "Swedish", - "sy": "Syriac", - "ta": "Tamil", - "te": "Telugu", - "tl": "Tagalog", - "th": "Thai", - "tr": "Turkish", - "uk": "Ukrainian", - "ur": "Urdu", - "uz": "Uzbek", - "vi": "Vietnamese", - "ro": "Romanian", - "pt-br": "Portuguese (Brazilian)", - "me": "Montenegrin", - "zh-tw": "Chinese (traditional)", - "ze": "Chinese bilingual", - "nb": "Norwegian Bokmal", - "se": "Northern Sami", -} - -# opensubtitles language setting mapping -# see SYNO.SDS.VideoStation2.Setting.AdvancedPanel.SubtitleSet.LANGUAGE_MAPPING -# and SYNO.SDS.VideoStation2.Setting.AdvancedPanel.SubtitleSet.EXTRA_LANGUAGE -_VS_OPENSUBTITLES_LANGUAGE_SETTING_MAPPING = { - "cze": "cs", - "dan": "da", - "eng": "en", - "fre": "fr", - "ger": "de", - "hun": "hu", - "ita": "it", - "jpn": "ja", - "kor": "ko", - "dut": "nl", - "nor": "no", - "pol": "pl", - "pob": "pt-br", - "por": "pt-pt", - "rus": "ru", - "spa": "es", - "swe": "sv", - "tha": "th", - "tur": "tk", - "chi": "zh-cn", - "fin": "fi", - "ara": "ar", - "heb": "he", - "ell": "el", - "rum": "ro", - "scc": "sr", - "hrv": "hr", - "bul": "bg", - "slv": "sl", - "bos": "bs", -} - - -def get_language_name(language_code): - if language_code in _LANGUAGE_CODE_TO_NAME_MAPPING: - return _LANGUAGE_CODE_TO_NAME_MAPPING[language_code] - return language_code - - -def convert_language_code_639_2B_to_639_1(language_code): - language_list = language_code.split(",") - converted_language_list = [] - for lang in language_list: - if lang in _VS_OPENSUBTITLES_LANGUAGE_SETTING_MAPPING: - converted_language_list.append( - _VS_OPENSUBTITLES_LANGUAGE_SETTING_MAPPING[lang] - ) - - return ",".join(converted_language_list) From ea2f919b3b4b70309e262adc1a38c686fa229592 Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Fri, 30 Aug 2024 08:09:28 +1000 Subject: [PATCH 6/9] Delete INFO --- INFO | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 INFO diff --git a/INFO b/INFO deleted file mode 100644 index 8c396a2..0000000 --- a/INFO +++ /dev/null @@ -1,13 +0,0 @@ -{ - "id": "com.synology.OpenSubtitles", - "title": "OpenSubtitles", - "description": "", - "version": 1.0, - "site": "https://www.opensubtitles.com/", - "entry_file": "subtitle.py", - "category": "subtitle", - "language": ["eng"], - "test_example": { - "title": "Harry Potter And The Philosophers stone" - } -} From 858b62308e289ea50d4f4dce356823dfc6e213a8 Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Fri, 30 Aug 2024 08:09:40 +1000 Subject: [PATCH 7/9] Delete OpenSubtitles.py --- OpenSubtitles.py | 112 ----------------------------------------------- 1 file changed, 112 deletions(-) delete mode 100644 OpenSubtitles.py diff --git a/OpenSubtitles.py b/OpenSubtitles.py deleted file mode 100644 index f60ba04..0000000 --- a/OpenSubtitles.py +++ /dev/null @@ -1,112 +0,0 @@ -import json -import urllib -import urllib.request -import subprocess - - -class OpenSubtitles: - BASE_URL = "https://api.opensubtitles.com/api/v1" - HASH_BINARY_PATH = "/var/packages/VideoStation/target/bin/video_hash" - INFO_PATH = ( - "/var/packages/VideoStation/target/subtitle_plugins/syno_opensubtitles/INFO" - ) - token = "" - apikey = "" - plugin_version = "0.0" - - def __init__(self, user, password, apikey): - self.apikey = apikey - self.login_and_set_token(user, password) - - with open(self.INFO_PATH, "r") as f: - self.plugin_version = json.load(f)["version"] - - def __query(self, url, method="GET", data=None): - result = None - timeouts = 30 - - header = { - "User-Agent": f"Videostation_Opensubtitles_Plugin v{self.plugin_version}", - "Accept": "application/json", - } - - if self.token: - header["Authorization"] = f"Bearer {self.token}" - if self.apikey: - header["Api-Key"] = self.apikey - - try: - data_encoded = None - if data: - data_encoded = urllib.parse.urlencode(data).encode("ascii") - - request = urllib.request.Request( - url=url, headers=header, data=data_encoded, method=method - ) - response = urllib.request.urlopen(request, timeout=timeouts) - result = response.read().decode("utf-8") - - except urllib.error.HTTPError as http_e: - print("http error:", http_e) - - except Exception as e: - print("exception", e) - - return result - - def __get_size_and_hash(self, file_path): - execute_array = [self.HASH_BINARY_PATH, "--opensubtitles", file_path] - p = subprocess.Popen( - execute_array, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - out, err = p.communicate() - - if err: - return False - - response_string = str(out, "utf-8") - return json.loads(response_string) - - def login_and_set_token(self, user, password): - url = f"{self.BASE_URL}/login" - data = {"username": user, "password": password} - login_result = self.__query(url=url, method="POST", data=data) - self.token = json.loads(login_result)["token"] - - def search_subtitle_by_file_hash(self, path, lang): - info = self.__get_size_and_hash(path) - return self.__search_subtitle_by_hash(info["hash"], lang) - - def __search_subtitle_by_hash(self, hash, lang): - url = f"{self.BASE_URL}/subtitles?moviehash={hash}&languages={lang}" - result = self.__query(url=url, method="GET") - return json.loads(result) - - def search_subtitle_by_feature_id(self, id, lang): - url = f"{self.BASE_URL}/subtitles?id={id}&languages={lang}" - result = self.__query(url=url, method="GET") - return json.loads(result) - - def search_feature_by_title(self, title): - titleEncode = urllib.parse.quote_plus(title.lower()) - url = f"{self.BASE_URL}/features?query={titleEncode}" - result = self.__query(url=url, method="GET") - return json.loads(result) - - def run_download(self, site_subtitle_id, save_file_Path): - link = self.__get__download_link(site_subtitle_id) - self.__download_link(link, save_file_Path) - - def __get__download_link(self, fileId): - url = f"{self.BASE_URL}/download" - data = {"file_id": fileId} - result = self.__query(url=url, method="POST", data=data) - return json.loads(result)["link"] - - def __download_link(self, link, save_file_Path): - result = self.__query(url=link, method="GET") - - with open(save_file_Path, "w", encoding="utf-8") as f: - if not result: - result = "" - f.write(result) From 64a1e10651a4d5633132dc53409071cd62651b4c Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Fri, 30 Aug 2024 08:09:54 +1000 Subject: [PATCH 8/9] Delete subtitle.py --- subtitle.py | 171 ---------------------------------------------------- 1 file changed, 171 deletions(-) delete mode 100644 subtitle.py diff --git a/subtitle.py b/subtitle.py deleted file mode 100644 index 779f032..0000000 --- a/subtitle.py +++ /dev/null @@ -1,171 +0,0 @@ -import os -import json -import argparse -import OpenSubtitles -import html -import Language - - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("--action", type=str, required=True, help="search|download") - parser.add_argument( - "--path", - type=str, - default="", - help="search by file path, would be useless when set title", - ) - parser.add_argument( - "--title", - type=str, - default="", - help="search by title, prior than search by path", - ) - parser.add_argument("--id", type=str, default="", help="download id") - parser.add_argument( - "--lang", type=str, default="eng", help="search language, ex: eng or eng,fre" - ) - parser.add_argument( - "--limit", type=int, default=10, help="search limit, default 10" - ) - parser.add_argument("--username", type=str, required=True) - parser.add_argument("--password", type=str, default="") - parser.add_argument("--apikey", type=str, default="") - - args = parser.parse_known_args()[0] - - username = args.username - password = args.password - apikey = args.apikey - - if not password: - password = os.environ["SUBTITLE_PLUGIN_PASSWORD"] - if not apikey: - apikey = os.environ["SUBTITLE_PLUGIN_APIKEY"] - - site = OpenSubtitles.OpenSubtitles(username, password, apikey) - action = args.action - lang = Language.convert_language_code_639_2B_to_639_1(args.lang) - if not lang: - lang = "en" - path = args.path - title = args.title - limit = args.limit - - if action == "search": - items = do_search(site, title, lang, path, limit) - - process_output({"items": items, "total": len(items)}) - - elif action == "download": - id = args.id - save_file_path = do_download(site, id, path) - - process_output({"id": save_file_path}) - - else: - parser.print_help() - - -def do_search(site, title, lang, path, limit): - items = [] - if title != "": - searchFeatureResult = site.search_feature_by_title(title) - if searchFeatureResult["data"]: - feature_id = searchFeatureResult["data"][0]["id"] - search_subtitle_result = site.search_subtitle_by_feature_id( - feature_id, lang - ) - items = transform_search_subtitle_result( - search_subtitle_result, path, limit - ) - else: - search_subtitle_result = site.search_subtitle_by_file_hash(path, lang) - items = transform_search_subtitle_result(search_subtitle_result, path, limit) - - if len(items) == 0: - base_name = os.path.basename(path) - file_name = os.path.splitext(base_name)[0] - searchFeatureResult = site.search_feature_by_title(file_name) - - if searchFeatureResult["data"]: - feature_id = searchFeatureResult["data"][0]["id"] - search_subtitle_result = site.search_subtitle_by_feature_id( - feature_id, lang - ) - items = transform_search_subtitle_result( - search_subtitle_result, path, limit - ) - return items - - -def do_download(site, id, path): - directory = os.path.dirname(path) - save_file_path = f"{directory}/{id}.srt" - - language_id, site_subtitle_id = decompose_id(id) - site.run_download(site_subtitle_id, save_file_path) - return save_file_path - - -def get_subtitle_full_path(prefix): - dirname = os.path.dirname(prefix) - files = [f for f in os.listdir(dirname) if os.path.isfile(os.path.join(dirname, f))] - for f in files: - filename = os.path.join(dirname, f) - if 0 == filename.find(prefix): - return filename - return False - - -def compose_id(file_name, language_id, site_subtitle_id): - return f"{file_name}.{language_id}.{site_subtitle_id}" - - -def decompose_id(id): - split_result = id.split(".") - site_subtitle_id = split_result[len(split_result) - 1] - language_id = split_result[len(split_result) - 2] - return language_id, site_subtitle_id - - -def transform_search_subtitle_result(search_subtitle_result, file_path, limit): - items = [] - for data in search_subtitle_result["data"]: - site_subtitle_id = data["id"] - language_id = data["attributes"]["language"].lower() - base_name = os.path.basename(file_path) - file_name = os.path.splitext(base_name)[0] - file_directory = os.path.dirname(file_path) - - id = compose_id(file_name, language_id, site_subtitle_id) - - prefix = f"{file_directory}/{id}" - - subtitle_id = get_subtitle_full_path(prefix) - - item = { - "id": id, - "downloaded": bool(subtitle_id), - "filename": data["attributes"]["release"], - "language": Language.get_language_name(language_id), - "language_id": language_id, - "plugin_id": "com.synology.OpenSubtitles", - "plugin_title": "OpenSubtitles", - "subtitle_id": subtitle_id, - } - items.append(item) - if limit > 0 and len(items) >= limit: - break - return items - - -def process_output(output): - json_string = json.dumps(output, ensure_ascii=False, separators=(",", ":")) - json_string = html.unescape(json_string) - print(json_string) - - -if __name__ == "__main__": - main() - exit(0) From fee9da48129f5dbcf343535e20c99af223b04e28 Mon Sep 17 00:00:00 2001 From: Dave Russell <39733752+007revad@users.noreply.github.com> Date: Fri, 30 Aug 2024 08:11:35 +1000 Subject: [PATCH 9/9] Update videostation_for_722.sh --- videostation_for_722.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/videostation_for_722.sh b/videostation_for_722.sh index e0e669f..b0a33f9 100644 --- a/videostation_for_722.sh +++ b/videostation_for_722.sh @@ -27,7 +27,7 @@ # or add OpenSubtitle changes from 3.1.1-3168 to 3.1.0-3153 #------------------------------------------------------------------------------ -scriptver="v1.0.0" +scriptver="v1.0.1" script=VideoStation_for_DSM722 repo="007revad/VideoStation_for_DSM722" scriptname=videostation_for_722 @@ -478,7 +478,7 @@ if [[ ${ame_version:0:1} -gt "3" ]]; then fi # CodecPack (Advanced Media Extensions) -if ! check_pkg_installed CodecPack && [[ $ame_version == "30.1.0-3005" ]]; then +if ! check_pkg_installed CodecPack && [[ $ame_version != "30.1.0-3005" ]]; then download_pkg CodecPack "3.1.0-3005" "CodecPack-${arch}-3.1.0-3005.spk" package_install "CodecPack-${arch}-3.1.0-3005.spk" "Advanced Media Extensions" package_stop CodecPack "Advanced Media Extensions"