Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __call(self, method, *args):
"""Calls XML-RPC method and checks its return code."""
reply = getattr(self.__connection, method)(*args)
if "status" not in reply:
raise Error("server returned an invalid response")
if reply["status"].split(" ")[0] != "200":
raise Error("server returned error '{0}'", reply["status"])
return reply
def __call(self, method, *args):
"""Calls XML-RPC method and checks its return code."""
reply = getattr(self.__connection, method)(*args)
if "status" not in reply:
raise Error("server returned an invalid response")
if reply["status"].split(" ")[0] != "200":
raise Error("server returned error '{0}'", reply["status"])
return reply
"""Options:\n"""
""" -l, --lang a comma separated list of subtitles languages to download ("en,ru")\n"""
""" -r, --recursive process subdirectories recursively\n"""
""" -o, --opensubtitles download subtitles also from www.opensubtitles.org (enhances the result,\n"""
""" but significantly increases the script work time + www.opensubtitles.org\n"""
""" servers are often down)\n"""
""" -h, --help show this help"""
.format(argv[0])
)
sys.exit(0)
elif option in ("-l", "--lang"):
for lang in value.split(","):
lang = lang.strip()
if lang not in LANGUAGES:
raise Error("invalid language '{0}'", lang)
languages.add(lang)
elif option in ("-r", "--recursive"):
recursive = True
elif option in ("-o", "--opensubtitles"):
use_opensubtitles = True
else:
raise Error("invalid option '{0}'", option)
if not cmd_args:
raise Error("there is no TV show video file or TV show directory specified")
if not languages:
raise Error("there is no subtitles languages specified")
return (languages, use_opensubtitles, cmd_args, recursive)
)
sys.exit(0)
elif option in ("-l", "--lang"):
for lang in value.split(","):
lang = lang.strip()
if lang not in LANGUAGES:
raise Error("invalid language '{0}'", lang)
languages.add(lang)
elif option in ("-r", "--recursive"):
recursive = True
elif option in ("-o", "--opensubtitles"):
use_opensubtitles = True
else:
raise Error("invalid option '{0}'", option)
if not cmd_args:
raise Error("there is no TV show video file or TV show directory specified")
if not languages:
raise Error("there is no subtitles languages specified")
return (languages, use_opensubtitles, cmd_args, recursive)
except Exception as e:
raise Fatal_error("Command line options parsing error: {0}. See `{1} -h` for more information.", e, argv[0])
if file_size < 65536 * 2:
raise Error("file too small")
for pos in (0, file_size - 65536):
hashing_file.seek(pos)
for i in range(65536 / buf_size):
buf = b""
data = b" "
while len(buf) != buf_size:
data = hashing_file.read(buf_size - len(buf))
if data:
buf += data
else:
raise Error("end of file error")
file_hash += struct.unpack(b"=q", buf)[0]
file_hash &= 0xFFFFFFFFFFFFFFFF
return (file_size, "{0:016x}".format(file_hash))
except IOError as e:
raise Error("Unable to hash file '{0}': {1}.", path, e)
except Exception as e:
raise Fatal_error("Error while hashing file '{0}': {1}.", path, e)
episode_list_url = self.__url_prefix + "tvshow-{0}-{1}.html".format(show["id"], season)
episode_list_html = get_url_contents(episode_list_url).decode("utf-8", errors = "replace")
all_episodes_regex = re.compile(r"""
]*){0,1}>\s*\s*
]*){0,1}>\s*
]*){0,1}\s+
href\s*=\s*["']{0,1}
/{0,1}episode-""" + str(show["id"]) + "-" + str(season) + r"""\.html
["']{0,1}
(\s[^>]*){0,1}>
""", re.IGNORECASE | re.VERBOSE)
if len( [ x for x in all_episodes_regex.finditer(episode_list_html) ] ) != 1:
raise Error("failed to parse a server response")
episode_regex = re.compile(r"""
]*){0,1}>\s*""" +
str(season) + r"""x0*(\d+)\s*
\s*
]*){0,1}>\s*
]*){0,1}\s+
href\s*=\s*["']{0,1}
/{0,1}episode-(\d+)\.html
["']{0,1}
(\s[^>]*){0,1}>
""", re.IGNORECASE | re.VERBOSE)
for match in episode_regex.finditer(episode_list_html):
episodes[int(match.group(2))] = { "id": match.group(5) }
"""Gets a TV show subtitles and returns the subtitles file contents."""
subtitles = self.__get_episode_subtitles(show_name, season, episode)
if language not in subtitles:
raise Not_found()
try:
zipfile_data = get_url_contents( self.__url_prefix + "download-{0}.html".format(subtitles[language]) )
except Exception as e:
raise Fatal_error("Unable to download the subtitles: {0}.", e)
try:
subtitles_zip = zipfile.ZipFile(BytesIO(zipfile_data))
if len(subtitles_zip.namelist()) != 1:
raise Error("zip file contains {0} files instead of 1", len(subtitles_zip.namelist()))
return subtitles_zip.open(subtitles_zip.namelist()[0]).read()
except Exception as e:
raise Error("Unable to unzip the subtitles file: {0}.", e)
buf = b""
data = b" "
while len(buf) != buf_size:
data = hashing_file.read(buf_size - len(buf))
if data:
buf += data
else:
raise Error("end of file error")
file_hash += struct.unpack(b"=q", buf)[0]
file_hash &= 0xFFFFFFFFFFFFFFFF
return (file_size, "{0:016x}".format(file_hash))
except IOError as e:
raise Error("Unable to hash file '{0}': {1}.", path, e)
except Exception as e:
raise Fatal_error("Error while hashing file '{0}': {1}.", path, e)
media_extensions = [ ext[1:] for ext in MEDIA_EXTENSIONS ]
subtitles_extensions = [ ext[1:] for ext in SUBTITLE_EXTENSIONS ]
# Gathering file list that we are going to process -->
for tv_show_path in tv_show_paths:
try:
try:
is_directory = stat.S_ISDIR(os.stat(tv_show_path)[stat.ST_MODE])
except Exception as e:
raise Error("Unable to find '{0}': {1}.", tv_show_path, e)
tv_show_path = os.path.abspath(tv_show_path)
media_dir = ( tv_show_path if is_directory else os.path.dirname(tv_show_path) )
if not is_directory and os.path.splitext(tv_show_path)[1].lower() not in media_extensions:
raise Error("'{0}' is not a media {1} file.", tv_show_path, MEDIA_EXTENSIONS)
try:
available_subtitles = [
file_name
for file_name in os.listdir(media_dir)
if (
os.path.splitext(file_name)[1].lower() in subtitles_extensions and
os.path.isfile(os.path.join(media_dir, file_name))
)
]
if is_directory:
media_files = [
file_name
for file_name in os.listdir(media_dir)
if (