# Buggy code
def _get_streams(self):
    match = _url_re.match(self.url)
    if not match:
        return

    channel, media_id = match.group("channel", "media_id")
    self.logger.debug(
        "Matched URL: channel={0}, media_id={1}".format(channel, media_id)
    )
    if not media_id:
        res = http.get(LIVE_API.format(channel))
        livestream = http.json(res, schema=_live_schema)
        if livestream["media_hosted_media"]:
            hosted = _live_schema.validate(livestream["media_hosted_media"])
            self.logger.info(
                "{0} is hosting {1}",
                livestream["media_user_name"],
                hosted["media_user_name"],
            )
            livestream = hosted

        if not livestream["media_is_live"]:
            return

        media_id = livestream["media_id"]
        media_type = "live"
    else:
        media_type = "video"

    res = http.get(PLAYER_API.format(media_type, media_id))
    player = http.json(res, schema=_player_schema)

    if media_type == "live":
        return self._get_live_streams(player)
    else:
        return self._get_video_streams(player)


# Traceback (KeyError)
streamlink smashcast.tv/greatvaluesmash best
[cli][info] Found matching plugin hitbox for URL smashcast.tv/greatvaluesmash
Traceback (most recent call last):
File "C:\Program Files (x86)\Streamlink\bin\streamlink-script.py", line 15, in
<module>
main()
File "C:\Program Files (x86)\Streamlink\pkgs\streamlink_cli\main.py", line 103
8, in main
handle_url()
File "C:\Program Files (x86)\Streamlink\pkgs\streamlink_cli\main.py", line 482
, in handle_url
streams = fetch_streams(plugin)
File "C:\Program Files (x86)\Streamlink\pkgs\streamlink_cli\main.py", line 394
, in fetch_streams
sorting_excludes=args.stream_sorting_excludes)
File "C:\Program Files (x86)\Streamlink\pkgs\streamlink\plugin\plugin.py", lin
e 345, in get_streams
return self.streams(*args, **kwargs)
File "C:\Program Files (x86)\Streamlink\pkgs\streamlink\plugin\plugin.py", lin
e 248, in streams
ostreams = self._get_streams()
File "C:\Program Files (x86)\Streamlink\pkgs\streamlink\plugins\hitbox.py", li
ne 181, in _get_streams
if livestream["media_hosted_media"]:
KeyError: 'media_hosted_media'

# Fixed code
def _get_streams(self):
    match = _url_re.match(self.url)
    if not match:
        return

    channel, media_id = match.group("channel", "media_id")
    self.logger.debug(
        "Matched URL: channel={0}, media_id={1}".format(channel, media_id)
    )
    if not media_id:
        res = http.get(LIVE_API.format(channel))
        livestream = http.json(res, schema=_live_schema)
        if livestream.get("media_hosted_media"):
            hosted = _live_schema.validate(livestream["media_hosted_media"])
            self.logger.info(
                "{0} is hosting {1}",
                livestream["media_user_name"],
                hosted["media_user_name"],
            )
            livestream = hosted

        if not livestream["media_is_live"]:
            return

        media_id = livestream["media_id"]
        media_type = "live"
    else:
        media_type = "video"

    res = http.get(PLAYER_API.format(media_type, media_id))
    player = http.json(res, schema=_player_schema)

    if media_type == "live":
        return self._get_live_streams(player)
    else:
        return self._get_video_streams(player)