# Buggy code
def initialize():
    """Replace original functions if the fs encoding is not utf-8."""
    if hasattr(sys, "_called_from_test"):
        return

    affected_functions = {
        certifi: ["where", "old_where"],
        glob: ["glob"],
        io: ["open"],
        os: [
            "access",
            "chdir",
            "listdir",
            "makedirs",
            "mkdir",
            "remove",
            "rename",
            "renames",
            "rmdir",
            "stat",
            "unlink",
            "utime",
            "walk",
        ],
        os.path: [
            "abspath",
            "basename",
            "dirname",
            "exists",
            "getctime",
            "getmtime",
            "getsize",
            "isabs",
            "isdir",
            "isfile",
            "islink",
            "join",
            "normcase",
            "normpath",
            "realpath",
            "relpath",
            "split",
            "splitext",
        ],
        shutil: ["copyfile", "copymode", "move", "rmtree"],
        tarfile: ["is_tarfile"],
        rarfile: ["is_rarfile"],
    }

    # pyOpenSSL 0.14-1 bug: it can't handle unicode input.
    # pyOpenSSL fix -> https://github.com/pyca/pyopenssl/pull/209
    # Our bug: https://github.com/pymedusa/Medusa/issues/1422
    handle_output_map = {certifi: _handle_output_b}

    if os.name != "nt":
        affected_functions[os].extend(["chmod", "chown", "link", "statvfs", "symlink"])

    if not fs_encoding or fs_encoding.lower() not in ("utf-8", "mbcs"):
        handle_input = _handle_input
    else:
        handle_input = None

    for k, v in viewitems(affected_functions):
        handle_output = handle_output_map.get(k, _handle_output_u)
        for f in v:
            setattr(k, f, make_closure(getattr(k, f), handle_input, handle_output))


# Traceback (UnicodeDecodeError)
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/text/80379-3.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: text/80379-2.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/text/80379-2.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g12.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g12.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g11.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g11.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g13.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g13.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g17.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g17.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g18.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g18.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g19.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g19.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g33.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g33.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g22.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g22.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g23.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g23.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g10.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g10.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g15.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g15.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g20.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g20.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g25.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g25.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g27.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g27.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g28.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g28.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g30.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g30.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: graphical/80379-g29.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Adding base url for image: _cache/graphical/80379-g29.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] No banners found for 80379
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Found 15 fanarts at 1280x720 (1) resolution for series 80379
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Found 30 fanarts at 1920x1080 (2) resolution for series 80379
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Found 45 total fanarts for series 80379
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Fanart threshold set to 26.6481943835 votes
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Weighted fanart results for series 80379:
     Score     Rating  Votes      Resolution	URL
     4.290      7.000     84       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-2.jpg
     4.176      7.100     39       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-34.jpg
     3.898      6.900     13       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-29.jpg
     3.889      6.700     15       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-40.jpg
     3.871      6.600     15       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-14.jpg
     3.871      6.600     15       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-16.jpg
     3.868      6.800     12       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-37.jpg
     3.839      6.800     10       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-60.jpg
     3.818      6.900      8       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-31.jpg
     3.794     10.000      2       1920x1080	https://www.thetvdb.com/banners/fanart/original/5b8b22b2cecfb.jpg
     3.789      7.000      6       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-50.jpg
     3.743      6.500      6       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-66.jpg
     3.731      5.800     16       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-30.jpg
     3.719      6.400      5       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-48.jpg
     3.719      6.400      5       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-45.jpg
     3.719      6.400      5       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-41.jpg
     3.666      6.000      3       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-32.jpg
     3.666      6.000      3       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-64.jpg
     3.636      5.300     14       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-9.jpg
     3.632      5.300      4       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-33.jpg
     3.629      0.000      0       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-36.jpg
     3.629      0.000      0       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-65.jpg
     3.629      0.000      0       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-56.jpg
     3.627      6.900     41        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-4.jpg
     3.622      5.200      9       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-19.jpg
     3.612      6.900     38        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-1.jpg
     3.605      5.000      6       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-42.jpg
     3.580      4.900     10       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-5.jpg
     3.570      4.900     13       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-21.jpg
     3.558      4.700      9       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-23.jpg
     3.547      4.000      4       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-35.jpg
     3.389      6.700     15        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-11.jpg
     3.346      6.300     19        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-24.jpg
     3.336      6.900      9        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-49.jpg
     3.129      0.000      0        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-61.jpg
     3.121      5.200     10        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-13.jpg
     3.102      5.000      7        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-28.jpg
     3.091      5.000     11        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-27.jpg
     3.067      4.900     14        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-10.jpg
     3.039      4.600     10        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-12.jpg
     3.018      4.300      8        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-26.jpg
     2.938      3.600      8        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-25.jpg
     2.912      4.000     14        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-3.jpg
     2.903      3.600     10        1280x720	https://www.thetvdb.com/banners/fanart/original/80379-7.jpg
     2.823      1.500     20       1920x1080	https://www.thetvdb.com/banners/fanart/original/80379-59.jpg
2018-09-19 02:09:59 INFO     SHOWQUEUE-UPDATE :: [4614efc] Selected fanart for series 80379 (score=4.29020409372, score_bay=6.58040818745, rating=7, votes=84, res=1920x1080): https://www.thetvdb.com/banners/fanart/original/80379-2.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Full info for best fanart for series 80379: {u'rating': 7, u'_thumbnailpath': u'https://www.thetvdb.com/banners/_cache/fanart/original/80379-2.jpg', u'language': 7, u'score_weighted': 4.2902040937235695, u'ratingcount': 84, u'bannertype': u'fanart', u'score_rated': 6.580408187447139, u'bannertype2': u'1920x1080', u'res_index': 2, u'_bannerpath': u'https://www.thetvdb.com/banners/fanart/original/80379-2.jpg', u'resolution': u'1920x1080', u'sub_key': u''}
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Found 48 posters at 680x1000 (1) resolution for series 80379
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Found 48 total posters for series 80379
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Poster threshold set to 34.5915309796 votes
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Weighted poster results for series 80379:
     Score     Rating  Votes      Resolution	URL
     3.642      7.300     41        680x1000	https://www.thetvdb.com/banners/posters/80379-43.jpg
     3.418      6.200     72        680x1000	https://www.thetvdb.com/banners/posters/80379-18.jpg
     3.355      6.100     56        680x1000	https://www.thetvdb.com/banners/posters/80379-25.jpg
     3.351      6.100     54        680x1000	https://www.thetvdb.com/banners/posters/80379-9.jpg
     3.310      6.100     39        680x1000	https://www.thetvdb.com/banners/posters/80379-10.jpg
     3.300      6.600     18        680x1000	https://www.thetvdb.com/banners/posters/80379-48.jpg
     3.283      6.300     23        680x1000	https://www.thetvdb.com/banners/posters/80379-44.jpg
     3.280      5.900     49        680x1000	https://www.thetvdb.com/banners/posters/80379-13.jpg
     3.270      6.300     21        680x1000	https://www.thetvdb.com/banners/posters/80379-27.jpg
     3.270      6.300     21        680x1000	https://www.thetvdb.com/banners/posters/80379-4.jpg
     3.267      5.800     59        680x1000	https://www.thetvdb.com/banners/posters/80379-1.jpg
     3.263      6.100     27        680x1000	https://www.thetvdb.com/banners/posters/80379-3.jpg
     3.249      5.800     48        680x1000	https://www.thetvdb.com/banners/posters/80379-22.jpg
     3.193      6.200     13        680x1000	https://www.thetvdb.com/banners/posters/80379-49.jpg
     3.184      6.200     12        680x1000	https://www.thetvdb.com/banners/posters/80379-38.jpg
     3.174     10.000      2        680x1000	https://www.thetvdb.com/banners/posters/5b8d57d601c5f.jpg
     3.164      5.900     15        680x1000	https://www.thetvdb.com/banners/posters/80379-26.jpg
     3.129      5.700     14        680x1000	https://www.thetvdb.com/banners/posters/80379-11.jpg
     3.127      5.800     11        680x1000	https://www.thetvdb.com/banners/posters/80379-21.jpg
     3.125      5.600     17        680x1000	https://www.thetvdb.com/banners/posters/80379-39.jpg
     3.124      5.500     23        680x1000	https://www.thetvdb.com/banners/posters/80379-19.jpg
     3.095      9.000      1        680x1000	https://www.thetvdb.com/banners/posters/5b8b1e4b31701.jpg
     3.094      5.400     18        680x1000	https://www.thetvdb.com/banners/posters/80379-2.jpg
     3.064      5.300     10        680x1000	https://www.thetvdb.com/banners/posters/80379-7.jpg
     3.057      5.200     14        680x1000	https://www.thetvdb.com/banners/posters/80379-24.jpg
     3.043      5.100     14        680x1000	https://www.thetvdb.com/banners/posters/80379-42.jpg
     3.043      5.100     14        680x1000	https://www.thetvdb.com/banners/posters/80379-8.jpg
     3.040      0.000      0        680x1000	https://www.thetvdb.com/banners/posters/5af4b7b3468ca.jpg
     3.040      0.000      0        680x1000	https://www.thetvdb.com/banners/posters/5b64871cca599.jpg
     3.025      4.800      4        680x1000	https://www.thetvdb.com/banners/posters/80379-28.jpg
     3.025      4.800      4        680x1000	https://www.thetvdb.com/banners/posters/80379-34.jpg
     3.025      4.900      7        680x1000	https://www.thetvdb.com/banners/posters/80379-35.jpg
     3.022      4.800      5        680x1000	https://www.thetvdb.com/banners/posters/80379-46.jpg
     3.013      4.800      8        680x1000	https://www.thetvdb.com/banners/posters/80379-41.jpg
     3.011      4.900     16        680x1000	https://www.thetvdb.com/banners/posters/80379-30.jpg
     3.010      3.000      1        680x1000	https://www.thetvdb.com/banners/posters/80379-47.jpg
     3.010      3.000      1        680x1000	https://www.thetvdb.com/banners/posters/80379-32.jpg
     3.010      4.500      4        680x1000	https://www.thetvdb.com/banners/posters/80379-15.jpg
     3.009      4.600      5        680x1000	https://www.thetvdb.com/banners/posters/80379-37.jpg
     3.000      4.700      9        680x1000	https://www.thetvdb.com/banners/posters/80379-16.jpg
     2.999      4.600      7        680x1000	https://www.thetvdb.com/banners/posters/80379-23.jpg
     2.996      2.000      1        680x1000	https://www.thetvdb.com/banners/posters/80379-14.jpg
     2.995      4.600      8        680x1000	https://www.thetvdb.com/banners/posters/80379-31.jpg
     2.984      4.000      4        680x1000	https://www.thetvdb.com/banners/posters/80379-36.jpg
     2.982      1.000      1        680x1000	https://www.thetvdb.com/banners/posters/80379-53.jpg
     2.976      4.400      8        680x1000	https://www.thetvdb.com/banners/posters/80379-33.jpg
     2.975      4.500     10        680x1000	https://www.thetvdb.com/banners/posters/80379-20.jpg
     2.806      3.000     10        680x1000	https://www.thetvdb.com/banners/posters/80379-12.jpg
2018-09-19 02:09:59 INFO     SHOWQUEUE-UPDATE :: [4614efc] Selected poster for series 80379 (score=3.64186089993, score_bay=6.28372179985, rating=7.3, votes=41, res=680x1000): https://www.thetvdb.com/banners/posters/80379-43.jpg
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Full info for best poster for series 80379: {u'rating': 7.3, u'_thumbnailpath': u'https://www.thetvdb.com/banners/_cache/posters/80379-43.jpg', u'language': 7, u'score_weighted': 3.6418608999251174, u'ratingcount': 41, u'bannertype': u'poster', u'score_rated': 6.283721799850235, u'bannertype2': u'680x1000', u'res_index': 1, u'_bannerpath': u'https://www.thetvdb.com/banners/posters/80379-43.jpg', u'resolution': u'680x1000', u'sub_key': u''}
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Getting actors for 80379
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] GET URL: https://api.thetvdb.com/series/80379/actors [Status: 200]
2018-09-19 02:09:59 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] User-Agent: Medusa/0.2.10 (Linux; 3.4.39+; 17366960-bbca-11e8-a673-04e67633b3b8)
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Fetching image from https://www.thetvdb.com/banners/graphical/80379-g30.jpg
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] GET URL: https://www.thetvdb.com/banners/graphical/80379-g30.jpg [Status: 200]
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] User-Agent: Medusa/0.2.10 (Linux; 3.4.39+; 17366960-bbca-11e8-a673-04e67633b3b8)
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Searching for banner_thumb for series indexerid: 80379
indexer: 1
name: The Big Bang Theory
location: /media/Media/TVShows/The Big Bang Theory
network: CBS
airs: Thursday 8:00 PM
status: Continuing
start_year: 2007
genre: Comedy
classification: Scripted
runtime: 25
quality: 672
scene: False
sports: False
anime: False
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Fetching image from https://www.thetvdb.com/banners/_cache/graphical/80379-g30.jpg
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] GET URL: https://www.thetvdb.com/banners/_cache/graphical/80379-g30.jpg [Status: 200]
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] User-Agent: Medusa/0.2.10 (Linux; 3.4.39+; 17366960-bbca-11e8-a673-04e67633b3b8)
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Searching for poster_thumb for series indexerid: 80379
indexer: 1
name: The Big Bang Theory
location: /media/Media/TVShows/The Big Bang Theory
network: CBS
airs: Thursday 8:00 PM
status: Continuing
start_year: 2007
genre: Comedy
classification: Scripted
runtime: 25
quality: 672
scene: False
sports: False
anime: False
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Fetching image from https://www.thetvdb.com/banners/_cache/posters/80379-43.jpg
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] GET URL: https://www.thetvdb.com/banners/_cache/posters/80379-43.jpg [Status: 200]
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] User-Agent: Medusa/0.2.10 (Linux; 3.4.39+; 17366960-bbca-11e8-a673-04e67633b3b8)
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Searching for fanart for series indexerid: 80379
indexer: 1
name: The Big Bang Theory
location: /media/Media/TVShows/The Big Bang Theory
network: CBS
airs: Thursday 8:00 PM
status: Continuing
start_year: 2007
genre: Comedy
classification: Scripted
runtime: 25
quality: 672
scene: False
sports: False
anime: False
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] Fetching image from https://www.thetvdb.com/banners/fanart/original/80379-2.jpg
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] GET URL: https://www.thetvdb.com/banners/fanart/original/80379-2.jpg [Status: 200]
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] User-Agent: Medusa/0.2.10 (Linux; 3.4.39+; 17366960-bbca-11e8-a673-04e67633b3b8)
2018-09-19 02:10:00 INFO     SHOWQUEUE-UPDATE :: [4614efc] Cache check completed
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] 80379: Finished update of The Big Bang Theory
2018-09-19 02:10:00 DEBUG    SHOWQUEUE-UPDATE :: [4614efc] 80379: Queueing show refresh for The Big Bang Theory
2018-09-19 02:10:03 INFO     SHOWQUEUE-REFRESH :: [4614efc] 80379: Performing refresh on The Big Bang Theory
2018-09-19 02:10:03 DEBUG    SHOWQUEUE-REFRESH :: [4614efc] 80379: Loading all episodes from the show directory: /media/Media/TVShows/The Big Bang Theory
2018-09-19 02:10:03 ERROR    SHOWQUEUE-REFRESH :: [4614efc] 80379: Error while refreshing show The Big Bang Theory. Error: 'utf8' codec can't decode byte 0xf1 in position 43: invalid continuation byte
Traceback (most recent call last):
  File "/usr/local/medusa/medusa/show_queue.py", line 686, in run
    self.show.refresh_dir()
  File "/usr/local/medusa/medusa/tv/series.py", line 1730, in refresh_dir
    self.load_episodes_from_dir()
  File "/usr/local/medusa/medusa/tv/series.py", line 985, in load_episodes_from_dir
    media_files = helpers.list_media_files(self.location)
  File "/usr/local/medusa/medusa/helpers/__init__.py", line 262, in list_media_files
    files += list_media_files(full_cur_file)
  File "/usr/local/medusa/medusa/helpers/__init__.py", line 257, in list_media_files
    for cur_file in os.listdir(path):
  File "/usr/local/medusa/medusa/init/filesystem.py", line 113, in patched_output
    return handle_output(f(*args, **kwargs))
  File "/usr/local/medusa/medusa/init/filesystem.py", line 50, in _handle_output_u
    return list(map(_handle_output_u, result))
  File "/usr/local/medusa/medusa/init/filesystem.py", line 47, in _handle_output_u
    return decode(result)
  File "/usr/local/medusa/medusa/init/filesystem.py", line 32, in decode
    return text_type(value, 'utf-8' if os.name != 'nt' else fs_encoding)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf1 in position 43: invalid continuation byte

# Fixed code
def initialize():
    """Replace original functions if the fs encoding is invalid."""
    if hasattr(sys, "_called_from_test"):
        return

    affected_functions = {
        certifi: ["where", "old_where"],
        glob: ["glob"],
        io: ["open"],
        os: [
            "access",
            "chdir",
            "listdir",
            "makedirs",
            "mkdir",
            "remove",
            "rename",
            "renames",
            "rmdir",
            "stat",
            "unlink",
            "utime",
            "walk",
        ],
        os.path: [
            "abspath",
            "basename",
            "dirname",
            "exists",
            "getctime",
            "getmtime",
            "getsize",
            "isabs",
            "isdir",
            "isfile",
            "islink",
            "join",
            "normcase",
            "normpath",
            "realpath",
            "relpath",
            "split",
            "splitext",
        ],
        shutil: ["copyfile", "copymode", "move", "rmtree"],
        tarfile: ["is_tarfile"],
        rarfile: ["is_rarfile"],
    }

    # pyOpenSSL 0.14-1 bug: it can't handle unicode input.
    # pyOpenSSL fix -> https://github.com/pyca/pyopenssl/pull/209
    # Our bug: https://github.com/pymedusa/Medusa/issues/1422
    handle_output_map = {certifi: _handle_output_b}

    if os.name != "nt":
        affected_functions[os].extend(["chmod", "chown", "link", "statvfs", "symlink"])

    if is_valid_encoding(fs_encoding):
        handle_input = None
    else:
        global valid_encoding
        valid_encoding = False
        handle_input = _handle_input

    for k, v in viewitems(affected_functions):
        handle_output = handle_output_map.get(k, _handle_output_u)
        for f in v:
            setattr(k, f, make_closure(getattr(k, f), handle_input, handle_output))