# Buggy code
def _create_api(self):
    """Creates a new CrunchyrollAPI object, initiates it's session and
    tries to authenticate it either by using saved credentials or the
    user's username and password.
    """
    if self.options.get("purge_credentials"):
        self.cache.set("session_id", None, 0)
        self.cache.set("auth", None, 0)
        self.cache.set("session_id", None, 0)

    # use the crunchyroll locale as an override, for backwards compatibility
    locale = self.get_option("locale") or self.session.localization.language_code
    api = CrunchyrollAPI(
        self.cache,
        self.session,
        session_id=self.get_option("session_id"),
        locale=locale,
    )

    if not self.get_option("session_id"):
        self.logger.debug("Creating session with locale: {0}", locale)
        api.start_session()

        if api.auth:
            self.logger.debug("Using saved credentials")
            login = api.authenticate()
            self.logger.info(
                "Successfully logged in as '{0}'",
                login["user"]["username"] or login["user"]["email"],
            )
        elif self.options.get("username"):
            try:
                self.logger.debug("Attempting to login using username and password")
                api.login(self.options.get("username"), self.options.get("password"))
                login = api.authenticate()
                self.logger.info(
                    "Logged in as '{0}'",
                    login["user"]["username"] or login["user"]["email"],
                )

            except CrunchyrollAPIError as err:
                raise PluginError("Authentication error: {0}".format(err.msg))
        else:
            self.logger.warning(
                "No authentication provided, you won't be able to access "
                "premium restricted content"
            )

    return api


# Traceback (streamlink.plugin.crunchyroll.CrunchyrollAPIError)
Traceback (most recent call last):
File "runpy.py", line 193, in _run_module_as_main
File "runpy.py", line 85, in _run_code
File "C:\Users\kg\AppData\Local\Streamlink\bin\streamlink.exe\__main__.py", line 18, in <module>
File "C:\Users\kg\AppData\Local\Streamlink\pkgs\streamlink_cli\main.py", line 1024, in main
handle_url()
File "C:\Users\kg\AppData\Local\Streamlink\pkgs\streamlink_cli\main.py", line 585, in handle_url
streams = fetch_streams(plugin)
File "C:\Users\kg\AppData\Local\Streamlink\pkgs\streamlink_cli\main.py", line 465, in fetch_streams
sorting_excludes=args.stream_sorting_excludes)
File "C:\Users\kg\AppData\Local\Streamlink\pkgs\streamlink\plugin\plugin.py", line 317, in streams
ostreams = self._get_streams()
File "C:\Users\kg\AppData\Local\Streamlink\pkgs\streamlink\plugins\crunchyroll.py", line 306, in _get_streams
api = self._create_api()
File "C:\Users\kg\AppData\Local\Streamlink\pkgs\streamlink\plugins\crunchyroll.py", line 369, in _create_api
login = api.authenticate()
File "C:\Users\kg\AppData\Local\Streamlink\pkgs\streamlink\plugins\crunchyroll.py", line 217, in authenticate
data = self._api_call("authenticate", {"auth": self.auth}, schema=_login_schema)
File "C:\Users\kg\AppData\Local\Streamlink\pkgs\streamlink\plugins\crunchyroll.py", line 173, in _api_call
raise CrunchyrollAPIError(err_msg, err_code)
streamlink.plugin.crunchyroll.CrunchyrollAPIError: User could not be found.

# Fixed code
def _create_api(self):
    """Creates a new CrunchyrollAPI object, initiates it's session and
    tries to authenticate it either by using saved credentials or the
    user's username and password.
    """
    if self.options.get("purge_credentials"):
        self.cache.set("session_id", None, 0)
        self.cache.set("auth", None, 0)
        self.cache.set("session_id", None, 0)

    # use the crunchyroll locale as an override, for backwards compatibility
    locale = self.get_option("locale") or self.session.localization.language_code
    api = CrunchyrollAPI(
        self.cache,
        self.session,
        session_id=self.get_option("session_id"),
        locale=locale,
    )

    if not self.get_option("session_id"):
        log.debug("Creating session with locale: {0}", locale)
        api.start_session()

        if api.auth:
            log.debug("Using saved credentials")
            login = api.authenticate()
            if login:
                log.info(
                    "Successfully logged in as '{0}'",
                    login["user"]["username"] or login["user"]["email"],
                )
        if not api.auth and self.options.get("username"):
            try:
                log.debug("Attempting to login using username and password")
                api.login(self.options.get("username"), self.options.get("password"))
                login = api.authenticate()
                log.info(
                    "Logged in as '{0}'",
                    login["user"]["username"] or login["user"]["email"],
                )

            except CrunchyrollAPIError as err:
                raise PluginError("Authentication error: {0}".format(err.msg))
        if not api.auth:
            log.warning(
                "No authentication provided, you won't be able to access "
                "premium restricted content"
            )

    return api