Skip to content

Commit

Permalink
Fix is_channel_live & add channel_image (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
IonutNeagu committed Jan 20, 2021
1 parent a979478 commit 989c77b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions custom_components/youtube/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.helpers.entity import Entity
import re

CONF_CHANNEL_ID = 'channel_id'

ICON = 'mdi:youtube'

BASE_URL = 'https://www.youtube.com/feeds/videos.xml?channel_id={}'
CHANNEL_LIVE_URL = 'https://www.youtube.com/channel/{}/live'
CHANNEL_LIVE_URL = 'https://www.youtube.com/channel/{}'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_CHANNEL_ID): cv.string,
Expand Down Expand Up @@ -60,6 +61,7 @@ def __init__(self, channel_id, name, session):
self.url = None
self.published = None
self.channel_live = False
self.channel_image = None

async def async_update(self):
"""Update sensor."""
Expand All @@ -83,7 +85,7 @@ async def async_update(self):
self._state = title
self._image = thumbnail_url
url = CHANNEL_LIVE_URL.format(self.channel_id)
self.channel_live = await is_channel_live(url, self.name, self.hass, self.session)
self.channel_live, self.channel_image = await is_channel_live(url, self.name, self.hass, self.session)
except Exception as error: # pylint: disable=broad-except
_LOGGER.debug('%s - Could not update - %s', self._name, error)

Expand Down Expand Up @@ -114,7 +116,8 @@ def device_state_attributes(self):
'published': self.published,
'stream': self.stream,
'live': self.live,
'channel_is_live': self.channel_live}
'channel_is_live': self.channel_live,
'channel_image': self.channel_image}

async def is_live(url, name, hass, session):
"""Return bool if video is stream and bool if video is live"""
Expand All @@ -140,9 +143,11 @@ async def is_channel_live(url, name, hass, session):
async with async_timeout.timeout(10, loop=hass.loop):
response = await session.get(url)
info = await response.text()
if '"isLive":true' in info:
if '{"iconType":"LIVE"}' in info:
live = True
_LOGGER.debug('%s - Channel is live', name)
regex = r"\"width\":48,\"height\":48},{\"url\":\"(.*?)\",\"width\":88,\"height\":88},{\"url\":"
channel_image = re.findall(regex, info, re.MULTILINE)[0].replace("=s88-c-k-c0x00ffffff-no-rj", "")
except Exception as error: # pylint: disable=broad-except
_LOGGER.debug('%s - Could not update - %s', name, error)
return live
return live, channel_image

0 comments on commit 989c77b

Please sign in to comment.