# Buggy code
def _get_library(library_file=None):
    os_is_nt = os.name in ("nt", "dos", "os2", "ce")
    if os_is_nt:
        lib_type = ctypes.WinDLL
    else:
        lib_type = ctypes.CDLL
    if library_file is None:
        if os_is_nt:
            library_names = ("MediaInfo.dll",)
        elif sys.platform == "darwin":
            library_names = ("libmediainfo.0.dylib", "libmediainfo.dylib")
        else:
            library_names = ("libmediainfo.so.0",)
        script_dir = os.path.dirname(__file__)
        # Look for the library file in the script folder
        for library in library_names:
            lib_path = os.path.join(script_dir, library)
            if os.path.isfile(lib_path):
                # If we find it, don't try any other filename
                library_names = (lib_path,)
                break
    else:
        library_names = (library_file,)
    for i, library in enumerate(library_names, start=1):
        try:
            lib = lib_type(library)
            # Define arguments and return types
            lib.MediaInfo_Inform.restype = ctypes.c_wchar_p
            lib.MediaInfo_New.argtypes = []
            lib.MediaInfo_New.restype = ctypes.c_void_p
            lib.MediaInfo_Option.argtypes = [
                ctypes.c_void_p,
                ctypes.c_wchar_p,
                ctypes.c_wchar_p,
            ]
            lib.MediaInfo_Option.restype = ctypes.c_wchar_p
            lib.MediaInfo_Inform.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
            lib.MediaInfo_Inform.restype = ctypes.c_wchar_p
            lib.MediaInfo_Open.argtypes = [ctypes.c_void_p, ctypes.c_wchar_p]
            lib.MediaInfo_Open.restype = ctypes.c_size_t
            lib.MediaInfo_Delete.argtypes = [ctypes.c_void_p]
            lib.MediaInfo_Delete.restype = None
            lib.MediaInfo_Close.argtypes = [ctypes.c_void_p]
            lib.MediaInfo_Close.restype = None
            return lib
        except OSError:
            # If we've tried all possible filenames
            if i == len(library_names):
                raise


# Traceback (UnicodeDecodeError)
2019-06-07 16:18:01 ERROR    Thread_31 :: [6a21a60] Exception generated: 'utf-8' codec can't decode byte 0x94 in position 0: invalid start byte
Traceback (most recent call last):
File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 261, in async_call
result = function(**kwargs)
File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/error_logs.py", line 62, in submit_errors
results = self.issue_submitter.submit_github_issue(CheckVersion())
File "/Users/ovidijus/Apps/Medusa/medusa/issue_submitter.py", line 199, in submit_github_issue
return self.submit_issues(github, github_repo, loglines, similar_issues)
File "/Users/ovidijus/Apps/Medusa/medusa/issue_submitter.py", line 216, in submit_issues
gist = cls.create_gist(github, line)
File "/Users/ovidijus/Apps/Medusa/medusa/issue_submitter.py", line 75, in create_gist
context_loglines = logline.get_context_loglines()
File "/Users/ovidijus/Apps/Medusa/medusa/logger/__init__.py", line 448, in get_context_loglines
for logline in read_loglines(modification_time=start_timestamp):
File "/Users/ovidijus/Apps/Medusa/medusa/logger/__init__.py", line 166, in read_loglines
for line in reverse_readlines(f):
File "/Users/ovidijus/Apps/Medusa/medusa/logger/__init__.py", line 281, in reverse_readlines
for block in block_generator(filename, **block_generator_kws):
File "/Users/ovidijus/Apps/Medusa/medusa/logger/__init__.py", line 243, in blocks_r
block = file_obj.read(min(remaining_size, size))
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x94 in position 0: invalid start byte
2019-06-07 08:05:17 ERROR    SHOWQUEUE-REFRESH :: [6a21a60] 305075: Error while refreshing show Bungo Stray Dogs. Error: 'MediaInfo' object has no attribute 'xml_dom'
Traceback (most recent call last):
File "/Users/ovidijus/Apps/Medusa/ext/knowit/api.py", line 64, in know
result = provider.describe(video_path, context)
File "/Users/ovidijus/Apps/Medusa/ext/knowit/providers/mediainfo.py", line 296, in describe
video_path, self.executor.location, debug_data())
File "/Users/ovidijus/Apps/Medusa/ext/knowit/providers/mediainfo.py", line 289, in debug_data
xml = ensure_text(ElementTree.tostring(media_info.xml_dom)).replace('\r', '').replace('\n', '')
AttributeError: 'MediaInfo' object has no attribute 'xml_dom'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/ovidijus/Apps/Medusa/medusa/show_queue.py", line 688, in run
self.show.refresh_dir()
File "/Users/ovidijus/Apps/Medusa/medusa/tv/series.py", line 1792, in refresh_dir
self.load_episodes_from_dir()
File "/Users/ovidijus/Apps/Medusa/medusa/tv/series.py", line 1053, in load_episodes_from_dir
cur_episode = self.make_ep_from_file(os.path.join(self.location, media_file))
File "/Users/ovidijus/Apps/Medusa/medusa/tv/series.py", line 1397, in make_ep_from_file
cur_ep.update_status_quality(filepath)
File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2040, in update_status_quality
new_quality = Quality.name_quality(filepath, self.series.is_anime)
File "/Users/ovidijus/Apps/Medusa/medusa/common.py", line 284, in name_quality
return Quality._extend_quality(name)
File "/Users/ovidijus/Apps/Medusa/medusa/common.py", line 383, in _extend_quality
quality = Quality.quality_from_file_meta(file_path)
File "/Users/ovidijus/Apps/Medusa/medusa/common.py", line 405, in quality_from_file_meta
knowledge = knowit.know(file_path)
File "/Users/ovidijus/Apps/Medusa/ext/knowit/api.py", line 70, in know
raise KnowitException(debug_info(context=context, exc_info=True))
File "/Users/ovidijus/Apps/Medusa/ext/knowit/api.py", line 121, in debug_info
lines.append(debug_data())
File "/Users/ovidijus/Apps/Medusa/ext/knowit/providers/mediainfo.py", line 289, in debug_data
xml = ensure_text(ElementTree.tostring(media_info.xml_dom)).replace('\r', '').replace('\n', '')
AttributeError: 'MediaInfo' object has no attribute 'xml_dom'

# Fixed code
def _get_library(library_file=None):
    os_is_nt = os.name in ("nt", "dos", "os2", "ce")
    if os_is_nt:
        lib_type = WinDLL
    else:
        lib_type = CDLL
    if library_file is None:
        if os_is_nt:
            library_names = ("MediaInfo.dll",)
        elif sys.platform == "darwin":
            library_names = ("libmediainfo.0.dylib", "libmediainfo.dylib")
        else:
            library_names = ("libmediainfo.so.0",)
        script_dir = os.path.dirname(__file__)
        # Look for the library file in the script folder
        for library in library_names:
            lib_path = os.path.join(script_dir, library)
            if os.path.isfile(lib_path):
                # If we find it, don't try any other filename
                library_names = (lib_path,)
                break
    else:
        library_names = (library_file,)
    for i, library in enumerate(library_names, start=1):
        try:
            return lib_type(library)
        except OSError:
            # If we've tried all possible filenames
            if i == len(library_names):
                raise