# Buggy code
def start(self, args):
    """Start Application."""
    app.instance = self
    signal.signal(signal.SIGINT, self.sig_handler)
    signal.signal(signal.SIGTERM, self.sig_handler)

    # do some preliminary stuff
    app.MY_FULLNAME = os.path.normpath(
        os.path.abspath(os.path.join(__file__, "..", "..", "start.py"))
    )
    app.MY_NAME = os.path.basename(app.MY_FULLNAME)
    app.PROG_DIR = os.path.dirname(app.MY_FULLNAME)
    app.DATA_DIR = app.PROG_DIR
    app.MY_ARGS = args

    try:
        locale.setlocale(locale.LC_ALL, "")
        app.SYS_ENCODING = locale.getpreferredencoding()
    except (locale.Error, IOError):
        app.SYS_ENCODING = "UTF-8"

    # pylint: disable=no-member
    if (
        not app.SYS_ENCODING
        or app.SYS_ENCODING.lower()
        in ("ansi_x3.4-1968", "us-ascii", "ascii", "charmap")
        or (
            sys.platform.startswith("win")
            and sys.getwindowsversion()[0] >= 6
            and text_type(getattr(sys.stdout, "device", sys.stdout).encoding).lower()
            in ("cp65001", "charmap")
        )
    ):
        app.SYS_ENCODING = "UTF-8"

    # TODO: Continue working on making this unnecessary, this hack creates all sorts of hellish problems
    if not hasattr(sys, "setdefaultencoding"):
        reload(sys)

    try:
        # On non-unicode builds this will raise an AttributeError, if encoding type is not valid it throws a LookupError
        sys.setdefaultencoding(app.SYS_ENCODING)  # pylint: disable=no-member
    except (AttributeError, LookupError):
        sys.exit(
            "Sorry, you MUST add the Medusa folder to the PYTHONPATH environment variable or "
            "find another way to force Python to use {encoding} for string encoding.".format(
                encoding=app.SYS_ENCODING
            )
        )

    self.console_logging = (not hasattr(sys, "frozen")) or (
        app.MY_NAME.lower().find("-console") > 0
    )

    # Rename the main thread
    threading.currentThread().name = "MAIN"

    try:
        opts, _ = getopt.getopt(
            args,
            "hqdp::",
            [
                "help",
                "quiet",
                "nolaunch",
                "daemon",
                "pidfile=",
                "port=",
                "datadir=",
                "config=",
                "noresize",
            ],
        )
    except getopt.GetoptError:
        sys.exit(self.help_message())

    for option, value in opts:
        # Prints help message
        if option in ("-h", "--help"):
            sys.exit(self.help_message())

        # For now we'll just silence the logging
        if option in ("-q", "--quiet"):
            self.console_logging = False

        # Suppress launching web browser
        # Needed for OSes without default browser assigned
        # Prevent duplicate browser window when restarting in the app
        if option in ("--nolaunch",):
            self.no_launch = True

        # Override default/configured port
        if option in ("-p", "--port"):
            try:
                self.forced_port = int(value)
            except ValueError:
                sys.exit("Port: %s is not a number. Exiting." % value)

        # Run as a double forked daemon
        if option in ("-d", "--daemon"):
            self.run_as_daemon = True
            # When running as daemon disable console_logging and don't start browser
            self.console_logging = False
            self.no_launch = True

            if sys.platform == "win32" or sys.platform == "darwin":
                self.run_as_daemon = False

        # Write a pid file if requested
        if option in ("--pidfile",):
            self.create_pid = True
            self.pid_file = str(value)

            # If the pid file already exists, Medusa may still be running, so exit
            if os.path.exists(self.pid_file):
                sys.exit("PID file: %s already exists. Exiting." % self.pid_file)

        # Specify folder to load the config file from
        if option in ("--config",):
            app.CONFIG_FILE = os.path.abspath(value)

        # Specify folder to use as the data directory
        if option in ("--datadir",):
            app.DATA_DIR = os.path.abspath(value)

        # Prevent resizing of the banner/posters even if PIL is installed
        if option in ("--noresize",):
            app.NO_RESIZE = True

    # Keep backwards compatibility
    Application.backwards_compatibility()

    # The pid file is only useful in daemon mode, make sure we can write the file properly
    if self.create_pid:
        if self.run_as_daemon:
            pid_dir = os.path.dirname(self.pid_file)
            if not os.access(pid_dir, os.F_OK):
                sys.exit("PID dir: %s doesn't exist. Exiting." % pid_dir)
            if not os.access(pid_dir, os.W_OK):
                sys.exit(
                    "PID dir: %s must be writable (write permissions). Exiting."
                    % pid_dir
                )

        else:
            if self.console_logging:
                sys.stdout.write(
                    "Not running in daemon mode. PID file creation disabled.\n"
                )

            self.create_pid = False

    # If they don't specify a config file then put it in the data dir
    if not app.CONFIG_FILE:
        app.CONFIG_FILE = os.path.join(app.DATA_DIR, "config.ini")

    # Make sure that we can create the data dir
    if not os.access(app.DATA_DIR, os.F_OK):
        try:
            os.makedirs(app.DATA_DIR, 0o744)
        except os.error:
            raise SystemExit("Unable to create data directory: %s" % app.DATA_DIR)

    # Make sure we can write to the data dir
    if not os.access(app.DATA_DIR, os.W_OK):
        raise SystemExit("Data directory must be writeable: %s" % app.DATA_DIR)

    # Make sure we can write to the config file
    if not os.access(app.CONFIG_FILE, os.W_OK):
        if os.path.isfile(app.CONFIG_FILE):
            raise SystemExit("Config file must be writeable: %s" % app.CONFIG_FILE)
        elif not os.access(os.path.dirname(app.CONFIG_FILE), os.W_OK):
            raise SystemExit(
                "Config file root dir must be writeable: %s"
                % os.path.dirname(app.CONFIG_FILE)
            )

    os.chdir(app.DATA_DIR)

    # Check if we need to perform a restore first
    restore_dir = os.path.join(app.DATA_DIR, "restore")
    if os.path.exists(restore_dir):
        success = self.restore_db(restore_dir, app.DATA_DIR)
        if self.console_logging:
            sys.stdout.write(
                "Restore: restoring DB and config.ini %s!\n"
                % ("FAILED", "SUCCESSFUL")[success]
            )

    # Initialize all available themes
    app.AVAILABLE_THEMES = read_themes()
    app.DATA_ROOT = os.path.join(app.PROG_DIR, "themes")

    # Load the config and publish it to the application package
    if self.console_logging and not os.path.isfile(app.CONFIG_FILE):
        sys.stdout.write(
            "Unable to find %s, all settings will be default!\n" % app.CONFIG_FILE
        )

    app.CFG = ConfigObj(app.CONFIG_FILE, encoding="UTF-8", default_encoding="UTF-8")

    # Initialize the config and our threads
    self.initialize(console_logging=self.console_logging)

    if self.run_as_daemon:
        self.daemonize()

    # Get PID
    app.PID = os.getpid()

    # Build from the DB to start with
    self.load_shows_from_db()

    logger.info(
        "Starting Medusa [{branch}] using {config!r}",
        branch=app.BRANCH,
        config=app.CONFIG_FILE,
    )

    self.clear_cache()
    self.migrate_images()

    if self.forced_port:
        logger.info("Forcing web server to port {port}", port=self.forced_port)
        self.start_port = self.forced_port
    else:
        self.start_port = app.WEB_PORT

    if app.WEB_LOG:
        self.log_dir = app.LOG_DIR
    else:
        self.log_dir = None

    # app.WEB_HOST is available as a configuration value in various
    # places but is not configurable. It is supported here for historic reasons.
    if app.WEB_HOST and app.WEB_HOST != "0.0.0.0":
        self.web_host = app.WEB_HOST
    else:
        self.web_host = "" if app.WEB_IPV6 else "0.0.0.0"

    # web server options
    self.web_options = {
        "port": int(self.start_port),
        "host": self.web_host,
        "data_root": app.DATA_ROOT,
        "vue_root": os.path.join(app.PROG_DIR, "vue"),
        "web_root": app.WEB_ROOT,
        "log_dir": self.log_dir,
        "username": app.WEB_USERNAME,
        "password": app.WEB_PASSWORD,
        "enable_https": app.ENABLE_HTTPS,
        "handle_reverse_proxy": app.HANDLE_REVERSE_PROXY,
        "https_cert": os.path.join(app.PROG_DIR, app.HTTPS_CERT),
        "https_key": os.path.join(app.PROG_DIR, app.HTTPS_KEY),
    }

    # start web server
    self.web_server = AppWebServer(self.web_options)
    self.web_server.start()

    # Fire up all our threads
    self.start_threads()

    # Build internal name cache
    name_cache.build_name_cache()

    # Pre-populate network timezones, it isn't thread safe
    network_timezones.update_network_dict()

    # # why???
    # if app.USE_FAILED_DOWNLOADS:
    #     failed_history.trim_history()

    # # Check for metadata indexer updates for shows (Disabled until we use api)
    # app.show_update_scheduler.forceRun()

    # Launch browser
    if app.LAUNCH_BROWSER and not (self.no_launch or self.run_as_daemon):
        Application.launch_browser(
            "https" if app.ENABLE_HTTPS else "http", self.start_port, app.WEB_ROOT
        )

    # main loop
    while app.started:
        time.sleep(1)


# 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 start(self, args):
    """Start Application."""
    app.instance = self
    signal.signal(signal.SIGINT, self.sig_handler)
    signal.signal(signal.SIGTERM, self.sig_handler)

    # do some preliminary stuff
    app.MY_FULLNAME = os.path.normpath(
        os.path.abspath(os.path.join(__file__, "..", "..", "start.py"))
    )
    app.MY_NAME = os.path.basename(app.MY_FULLNAME)
    app.PROG_DIR = os.path.dirname(app.MY_FULLNAME)
    app.DATA_DIR = app.PROG_DIR
    app.MY_ARGS = args

    app.SYS_ENCODING = sys.getfilesystemencoding()
    if not is_valid_encoding(app.SYS_ENCODING):
        logger.warning(
            "Your system is using an invalid encoding: {enc}. Please change your encoding "
            "as soon as possible or you could encounter unexpected issues.",
            enc=app.SYS_ENCODING,
        )
        app.SYS_ENCODING = "utf-8"

    self.console_logging = (not hasattr(sys, "frozen")) or (
        app.MY_NAME.lower().find("-console") > 0
    )

    # Rename the main thread
    threading.currentThread().name = "MAIN"

    try:
        opts, _ = getopt.getopt(
            args,
            "hqdp::",
            [
                "help",
                "quiet",
                "nolaunch",
                "daemon",
                "pidfile=",
                "port=",
                "datadir=",
                "config=",
                "noresize",
            ],
        )
    except getopt.GetoptError:
        sys.exit(self.help_message())

    for option, value in opts:
        # Prints help message
        if option in ("-h", "--help"):
            sys.exit(self.help_message())

        # For now we'll just silence the logging
        if option in ("-q", "--quiet"):
            self.console_logging = False

        # Suppress launching web browser
        # Needed for OSes without default browser assigned
        # Prevent duplicate browser window when restarting in the app
        if option in ("--nolaunch",):
            self.no_launch = True

        # Override default/configured port
        if option in ("-p", "--port"):
            try:
                self.forced_port = int(value)
            except ValueError:
                sys.exit("Port: %s is not a number. Exiting." % value)

        # Run as a double forked daemon
        if option in ("-d", "--daemon"):
            self.run_as_daemon = True
            # When running as daemon disable console_logging and don't start browser
            self.console_logging = False
            self.no_launch = True

            if sys.platform == "win32" or sys.platform == "darwin":
                self.run_as_daemon = False

        # Write a pid file if requested
        if option in ("--pidfile",):
            self.create_pid = True
            self.pid_file = str(value)

            # If the pid file already exists, Medusa may still be running, so exit
            if os.path.exists(self.pid_file):
                sys.exit("PID file: %s already exists. Exiting." % self.pid_file)

        # Specify folder to load the config file from
        if option in ("--config",):
            app.CONFIG_FILE = os.path.abspath(value)

        # Specify folder to use as the data directory
        if option in ("--datadir",):
            app.DATA_DIR = os.path.abspath(value)

        # Prevent resizing of the banner/posters even if PIL is installed
        if option in ("--noresize",):
            app.NO_RESIZE = True

    # Keep backwards compatibility
    Application.backwards_compatibility()

    # The pid file is only useful in daemon mode, make sure we can write the file properly
    if self.create_pid:
        if self.run_as_daemon:
            pid_dir = os.path.dirname(self.pid_file)
            if not os.access(pid_dir, os.F_OK):
                sys.exit("PID dir: %s doesn't exist. Exiting." % pid_dir)
            if not os.access(pid_dir, os.W_OK):
                sys.exit(
                    "PID dir: %s must be writable (write permissions). Exiting."
                    % pid_dir
                )

        else:
            if self.console_logging:
                sys.stdout.write(
                    "Not running in daemon mode. PID file creation disabled.\n"
                )

            self.create_pid = False

    # If they don't specify a config file then put it in the data dir
    if not app.CONFIG_FILE:
        app.CONFIG_FILE = os.path.join(app.DATA_DIR, "config.ini")

    # Make sure that we can create the data dir
    if not os.access(app.DATA_DIR, os.F_OK):
        try:
            os.makedirs(app.DATA_DIR, 0o744)
        except os.error:
            raise SystemExit("Unable to create data directory: %s" % app.DATA_DIR)

    # Make sure we can write to the data dir
    if not os.access(app.DATA_DIR, os.W_OK):
        raise SystemExit("Data directory must be writeable: %s" % app.DATA_DIR)

    # Make sure we can write to the config file
    if not os.access(app.CONFIG_FILE, os.W_OK):
        if os.path.isfile(app.CONFIG_FILE):
            raise SystemExit("Config file must be writeable: %s" % app.CONFIG_FILE)
        elif not os.access(os.path.dirname(app.CONFIG_FILE), os.W_OK):
            raise SystemExit(
                "Config file root dir must be writeable: %s"
                % os.path.dirname(app.CONFIG_FILE)
            )

    os.chdir(app.DATA_DIR)

    # Check if we need to perform a restore first
    restore_dir = os.path.join(app.DATA_DIR, "restore")
    if os.path.exists(restore_dir):
        success = self.restore_db(restore_dir, app.DATA_DIR)
        if self.console_logging:
            sys.stdout.write(
                "Restore: restoring DB and config.ini %s!\n"
                % ("FAILED", "SUCCESSFUL")[success]
            )

    # Initialize all available themes
    app.AVAILABLE_THEMES = read_themes()
    app.DATA_ROOT = os.path.join(app.PROG_DIR, "themes")

    # Load the config and publish it to the application package
    if self.console_logging and not os.path.isfile(app.CONFIG_FILE):
        sys.stdout.write(
            "Unable to find %s, all settings will be default!\n" % app.CONFIG_FILE
        )

    app.CFG = ConfigObj(app.CONFIG_FILE, encoding="UTF-8", default_encoding="UTF-8")

    # Initialize the config and our threads
    self.initialize(console_logging=self.console_logging)

    if self.run_as_daemon:
        self.daemonize()

    # Get PID
    app.PID = os.getpid()

    # Build from the DB to start with
    self.load_shows_from_db()

    logger.info(
        "Starting Medusa [{branch}] using {config!r}",
        branch=app.BRANCH,
        config=app.CONFIG_FILE,
    )

    self.clear_cache()
    self.migrate_images()

    if self.forced_port:
        logger.info("Forcing web server to port {port}", port=self.forced_port)
        self.start_port = self.forced_port
    else:
        self.start_port = app.WEB_PORT

    if app.WEB_LOG:
        self.log_dir = app.LOG_DIR
    else:
        self.log_dir = None

    # app.WEB_HOST is available as a configuration value in various
    # places but is not configurable. It is supported here for historic reasons.
    if app.WEB_HOST and app.WEB_HOST != "0.0.0.0":
        self.web_host = app.WEB_HOST
    else:
        self.web_host = "" if app.WEB_IPV6 else "0.0.0.0"

    # web server options
    self.web_options = {
        "port": int(self.start_port),
        "host": self.web_host,
        "data_root": app.DATA_ROOT,
        "vue_root": os.path.join(app.PROG_DIR, "vue"),
        "web_root": app.WEB_ROOT,
        "log_dir": self.log_dir,
        "username": app.WEB_USERNAME,
        "password": app.WEB_PASSWORD,
        "enable_https": app.ENABLE_HTTPS,
        "handle_reverse_proxy": app.HANDLE_REVERSE_PROXY,
        "https_cert": os.path.join(app.PROG_DIR, app.HTTPS_CERT),
        "https_key": os.path.join(app.PROG_DIR, app.HTTPS_KEY),
    }

    # start web server
    self.web_server = AppWebServer(self.web_options)
    self.web_server.start()

    # Fire up all our threads
    self.start_threads()

    # Build internal name cache
    name_cache.build_name_cache()

    # Pre-populate network timezones, it isn't thread safe
    network_timezones.update_network_dict()

    # # why???
    # if app.USE_FAILED_DOWNLOADS:
    #     failed_history.trim_history()

    # # Check for metadata indexer updates for shows (Disabled until we use api)
    # app.show_update_scheduler.forceRun()

    # Launch browser
    if app.LAUNCH_BROWSER and not (self.no_launch or self.run_as_daemon):
        Application.launch_browser(
            "https" if app.ENABLE_HTTPS else "http", self.start_port, app.WEB_ROOT
        )

    # main loop
    while app.started:
        time.sleep(1)