# Buggy code
def _get_root_dirs():
    if app.ROOT_DIRS == "":
        return {}

    root_dir = {}
    root_dirs = app.ROOT_DIRS
    default_index = int(app.ROOT_DIRS[0])

    root_dir["default_index"] = int(app.ROOT_DIRS[0])
    # remove default_index value from list (this fixes the offset)
    root_dirs.pop(0)

    if len(root_dirs) < default_index:
        return {}

    # clean up the list - replace %xx escapes by their single-character equivalent
    root_dirs = [unquote_plus(x) for x in root_dirs]

    default_dir = root_dirs[default_index]

    dir_list = []
    for root_dir in root_dirs:
        valid = 1
        try:
            os.listdir(root_dir)
        except Exception:
            valid = 0
        default = 0
        if root_dir is default_dir:
            default = 1

        cur_dir = {"valid": valid, "location": root_dir, "default": default}
        dir_list.append(cur_dir)

    return dir_list


# Traceback (ValueError)
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: 192.168.1.9 - gave correct API KEY. ACCESS GRANTED
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: all args: ()
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: all kwargs: {'cmd': 'sb.getrootdirs'}
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: sb.getrootdirs: {}
2017-11-12 22:05:04 ERROR    TORNADO :: [0f24093] API :: "invalid literal for int() with base 10: '/media/SAMSUNG/media/series'"
Traceback (most recent call last):
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 126, in get
    out_dict = _call_dispatcher(args, kwargs)
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 196, in call_dispatcher
    cur_out_dict = func(cur_args, cur_kwargs).run()  # call function and get response
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 1530, in run
    return _responds(RESULT_SUCCESS, _get_root_dirs())
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 555, in _get_root_dirs
    default_index = int(app.ROOT_DIRS[0])
ValueError: invalid literal for int() with base 10: '/media/SAMSUNG/media/series'
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: 192.168.1.9 - gave correct API KEY. ACCESS GRANTED
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: all args: ()
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: all kwargs: {'sort': 'name', 'cmd': 'shows'}
2017-11-12 22:05:04 DEBUG    TORNADO :: [0f24093] API :: shows: {'sort': 'name'}
2017-11-12 22:05:05 DEBUG    TORNADO :: [0f24093] API :: 192.168.1.9 - gave correct API KEY. ACCESS GRANTED
2017-11-12 22:05:05 DEBUG    TORNADO :: [0f24093] API :: all args: ()
2017-11-12 22:05:05 DEBUG    TORNADO :: [0f24093] API :: all kwargs: {'cmd': 'sb.getrootdirs'}
2017-11-12 22:05:05 DEBUG    TORNADO :: [0f24093] API :: sb.getrootdirs: {}
2017-11-12 22:05:05 ERROR    TORNADO :: [0f24093] API :: "invalid literal for int() with base 10: '/media/SAMSUNG/media/series'"
Traceback (most recent call last):
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 126, in get
    out_dict = _call_dispatcher(args, kwargs)
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 196, in call_dispatcher
    cur_out_dict = func(cur_args, cur_kwargs).run()  # call function and get response
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 1530, in run
    return _responds(RESULT_SUCCESS, _get_root_dirs())
  File "/home/osmc/Medusa/medusa/server/api/v1/core.py", line 555, in _get_root_dirs
    default_index = int(app.ROOT_DIRS[0])
ValueError: invalid literal for int() with base 10: '/media/SAMSUNG/media/series'

# Fixed code
def _get_root_dirs():
    if not app.ROOT_DIRS:
        return {}

    root_dir = {}
    default_index = int(app.ROOT_DIRS[0])
    root_dir["default_index"] = default_index
    # clean up the list: replace %xx escapes with single-character equivalent
    # and remove default_index value from list (this fixes the offset)
    root_dirs = [unquote_plus(x) for x in app.ROOT_DIRS[1:]]

    try:
        default_dir = root_dirs[default_index]
    except IndexError:
        return {}

    dir_list = []
    for root_dir in root_dirs:
        valid = 1
        try:
            os.listdir(root_dir)
        except OSError:
            valid = 0
        default = 0
        if root_dir is default_dir:
            default = 1

        cur_dir = {"valid": valid, "location": root_dir, "default": default}
        dir_list.append(cur_dir)

    return dir_list