# Buggy code
def get(cls, language):
    try:
        if PYCOUNTRY:
            # lookup workaround for alpha_2 language codes
            lang = (
                languages.get(alpha_2=language)
                if re.match(r"^[a-z]{2}$", language)
                else languages.lookup(language)
            )
            return Language(
                lang.alpha_2,
                lang.alpha_3,
                lang.name,
                getattr(lang, "bibliographic", None),
            )
        else:
            lang = None
            if len(language) == 2:
                lang = languages.get(alpha2=language)
            elif len(language) == 3:
                for code_type in ["part2b", "part2t", "part3"]:
                    try:
                        lang = languages.get(**{code_type: language})
                        break
                    except KeyError:
                        pass
                if not lang:
                    raise KeyError(language)
            else:
                raise KeyError(language)
            return Language(
                lang.alpha2, lang.part3, lang.name, lang.part2b or lang.part2t
            )
    except (LookupError, KeyError):
        raise LookupError("Invalid language code: {0}".format(language))


# Traceback (AttributeError)
$ streamlink --loglevel debug https://www.raiplay.it/dirette/rai1
[cli][debug] OS:         Linux-5.4.80-gentoo-r1-2-x86_64-Intel-R-_Core-TM-2_Duo_CPU_E8400_@_3.00GHz-with-glibc2.2.5
[cli][debug] Python:     3.8.7
[cli][debug] Streamlink: 2.0.0+25.g7a74e18
[cli][debug] Requests(2.25.1), Socks(1.7.1), Websocket(0.57.0)
[cli][info] Found matching plugin raiplay for URL https://www.raiplay.it/dirette/rai1
[plugins.raiplay][debug] Found JSON URL: https://www.raiplay.it/dirette/rai1.json
[plugins.raiplay][debug] Found stream URL: https://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=2606803
[utils.l10n][debug] Language code: en_US
Traceback (most recent call last):
File "/usr/bin/streamlink", line 33, in <module>
sys.exit(load_entry_point('streamlink==2.0.0+25.g7a74e18', 'console_scripts', 'streamlink')())
File "/usr/lib/python3.8/site-packages/streamlink_cli/main.py", line 1052, in main
handle_url()
File "/usr/lib/python3.8/site-packages/streamlink_cli/main.py", line 580, in handle_url
streams = fetch_streams(plugin)
File "/usr/lib/python3.8/site-packages/streamlink_cli/main.py", line 459, in fetch_streams
return plugin.streams(stream_types=args.stream_types,
File "/usr/lib/python3.8/site-packages/streamlink/plugin/plugin.py", line 323, in streams
ostreams = list(ostreams)
File "/usr/lib/python3.8/site-packages/streamlink/plugins/raiplay.py", line 49, in _get_streams
yield from HLSStream.parse_variant_playlist(self.session, stream_url).items()
File "/usr/lib/python3.8/site-packages/streamlink/stream/hls.py", line 494, in parse_variant_playlist
if not default_audio and (media.autoselect and locale.equivalent(language=media.language)):
File "/usr/lib/python3.8/site-packages/streamlink/utils/l10n.py", line 155, in equivalent
equivalent = equivalent and (not language or self.language == self.get_language(language))
File "/usr/lib/python3.8/site-packages/streamlink/utils/l10n.py", line 169, in get_language
return Language.get(language)
File "/usr/lib/python3.8/site-packages/streamlink/utils/l10n.py", line 73, in get
return Language(lang.alpha_2, lang.alpha_3, lang.name, getattr(lang, "bibliographic", None))
File "/usr/lib/python3.8/site-packages/pycountry/db.py", line 19, in __getattr__
raise AttributeError
AttributeError

# Fixed code
def get(cls, language):
    try:
        if PYCOUNTRY:
            lang = (
                languages.get(alpha_2=language)
                or languages.get(alpha_3=language)
                or languages.get(bibliographic=language)
                or languages.get(name=language)
            )
            if not lang:
                raise KeyError(language)
            return Language(
                # some languages don't have an alpha_2 code
                getattr(lang, "alpha_2", ""),
                lang.alpha_3,
                lang.name,
                getattr(lang, "bibliographic", ""),
            )
        else:
            lang = None
            if len(language) == 2:
                lang = languages.get(alpha2=language)
            elif len(language) == 3:
                for code_type in ["part2b", "part2t", "part3"]:
                    try:
                        lang = languages.get(**{code_type: language})
                        break
                    except KeyError:
                        pass
                if not lang:
                    raise KeyError(language)
            else:
                raise KeyError(language)
            return Language(
                lang.alpha2, lang.part3, lang.name, lang.part2b or lang.part2t
            )
    except (LookupError, KeyError):
        raise LookupError("Invalid language code: {0}".format(language))