예제 #1
0
async def ais_tunein_library(hass, media_content_id) -> BrowseMedia:
    import xml.etree.ElementTree as ET

    web_session = aiohttp_client.async_get_clientsession(hass)
    if media_content_id == "ais_tunein":
        try:
            #  7 sec should be enough
            with async_timeout.timeout(7):
                # we need this only for demo
                if ais_global.get_sercure_android_id_dom() == "dom-demo":
                    headers = {"accept-language": "pl"}
                    ws_resp = await web_session.get(
                        "http://opml.radiotime.com/", headers=headers)
                else:
                    ws_resp = await web_session.get(
                        "http://opml.radiotime.com/")
                response_text = await ws_resp.text()
                root = ET.fromstring(response_text)  # nosec
                tunein_types = []
                for item in root.findall("body/outline"):
                    tunein_types.append(
                        BrowseMedia(
                            title=item.get("text"),
                            media_class=MEDIA_CLASS_DIRECTORY,
                            media_content_id=media_content_id + "/2/" +
                            item.get("text") + "/" + item.get("URL"),
                            media_content_type=MEDIA_TYPE_APP,
                            can_play=False,
                            can_expand=True,
                            thumbnail="",
                        ))

                root = BrowseMedia(
                    title="TuneIn",
                    media_class=MEDIA_CLASS_DIRECTORY,
                    media_content_id=media_content_id,
                    media_content_type=MEDIA_TYPE_APP,
                    can_expand=True,
                    can_play=False,
                    children=tunein_types,
                )
                return root

        except Exception as e:
            _LOGGER.error("Can't connect tune in api: " + str(e))
            raise BrowseError("Can't connect tune in api: " + str(e))
    elif media_content_id.startswith("ais_tunein/2/"):
        try:
            #  7 sec should be enough
            with async_timeout.timeout(7):
                url_to_call = media_content_id.split("/", 3)[3]
                # we need to set language only for demo
                if ais_global.get_sercure_android_id_dom() == "dom-demo":
                    headers = {"accept-language": "pl"}
                    ws_resp = await web_session.get(url_to_call,
                                                    headers=headers)
                else:
                    ws_resp = await web_session.get(url_to_call)
                response_text = await ws_resp.text()
                root = ET.fromstring(response_text)  # nosec
                tunein_items = []
                for item in root.findall("body/outline"):
                    if item.get("type") == "audio":
                        tunein_items.append(
                            BrowseMedia(
                                title=item.get("text"),
                                media_class=MEDIA_CLASS_DIRECTORY,
                                media_content_id="ais_tunein/2/" +
                                item.get("text") + "/" + item.get("URL"),
                                media_content_type=MEDIA_TYPE_APP,
                                can_play=True,
                                can_expand=False,
                                thumbnail=item.get("image"),
                            ))
                    elif item.get("type") == "link":
                        tunein_items.append(
                            BrowseMedia(
                                title=item.get("text"),
                                media_class=MEDIA_CLASS_DIRECTORY,
                                media_content_id="ais_tunein/2/" +
                                item.get("text") + "/" + item.get("URL"),
                                media_content_type=MEDIA_TYPE_APP,
                                can_play=False,
                                can_expand=True,
                            ))
                for item in root.findall("body/outline/outline"):
                    if item.get("type") == "audio":
                        tunein_items.append(
                            BrowseMedia(
                                title=item.get("text"),
                                media_class=MEDIA_CLASS_DIRECTORY,
                                media_content_id="ais_tunein/2/" +
                                item.get("text") + "/" + item.get("URL"),
                                media_content_type=MEDIA_TYPE_APP,
                                can_play=True,
                                can_expand=False,
                                thumbnail=item.get("image"),
                            ))
                    elif item.get("type") == "link":
                        tunein_items.append(
                            BrowseMedia(
                                title=item.get("text"),
                                media_class=MEDIA_CLASS_DIRECTORY,
                                media_content_id="ais_tunein/2/" +
                                item.get("text") + "/" + item.get("URL"),
                                media_content_type=MEDIA_TYPE_APP,
                                can_play=False,
                                can_expand=True,
                            ))

                root = BrowseMedia(
                    title=media_content_id.split("/", 3)[2],
                    media_class=MEDIA_CLASS_DIRECTORY,
                    media_content_id=media_content_id,
                    media_content_type=MEDIA_TYPE_APP,
                    can_expand=True,
                    can_play=False,
                    children=tunein_items,
                )
                return root

        except Exception as e:
            _LOGGER.error("Can't connect tune in api: " + str(e))
            raise BrowseError("Can't connect tune in api: " + str(e))