diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1f0036a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.devcontainer/ +.github/ +.vscode/ +.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 02308af..7fbe4bc 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,14 @@ -[](https://github.com/hacs/integration) # youtube _A platform which give you info about the newest video on a channel._ -**This uses web scraping, a better implementation will be to use the API.** +This is a forked version of https://github.com/custom-components/youtube as that seems to have become abandoned. + +It fixes a few issues with the old integration: + - Adds support for @channel style channel names so you don't have to lookup the Yutube ID. + - Fixxes issues with the Youtube consent cookie + - Fixes communication timeout issues + - Fixes the broken channel image issue ![example][exampleimg] @@ -27,12 +32,22 @@ To get started put all the files from`/custom_components/youtube/` here: ## Example configuration.yaml +Using the old style channel id. + ```yaml sensor: platform: youtube channel_id: UCZ2Ku6wrhdYDHCaBzLaA3bw ``` +Or using new style channel name (_Note: You need to enclose the channel name in quotes!_) + +```yaml +sensor: + platform: youtube + channel_id: '@frenck' +``` + ## Configuration variables key | type | description @@ -62,3 +77,4 @@ key | type | description *** [exampleimg]: example.png + diff --git a/custom_components/youtube/manifest.json b/custom_components/youtube/manifest.json index b3470cf..e16427d 100644 --- a/custom_components/youtube/manifest.json +++ b/custom_components/youtube/manifest.json @@ -1,10 +1,12 @@ { "domain": "youtube", - "name": "Youtube Sensor", - "version": "0.0.0", - "documentation": "https://github.com/custom-components/youtube", + "name": "YouTube Sensor", + "version": "1.0.3", + "documentation": "https://github.com/jonnybergdahl/youtube", + "issue_tracker": "https://github.com/jonnybergdahl/youtube/issues", "dependencies": [], - "codeowners": ["@pinkywafer"], + "codeowners": ["@jonnybergdahl"], "requirements": [], "iot_class": "cloud_polling" } + diff --git a/custom_components/youtube/sensor.py b/custom_components/youtube/sensor.py index 74af357..cd36d83 100644 --- a/custom_components/youtube/sensor.py +++ b/custom_components/youtube/sensor.py @@ -6,7 +6,6 @@ """ import logging -import async_timeout import voluptuous as vol import homeassistant.helpers.config_validation as cv from homeassistant.helpers.aiohttp_client import async_create_clientsession @@ -17,11 +16,11 @@ import html CONF_CHANNEL_ID = 'channel_id' - ICON = 'mdi:youtube' - -BASE_URL = 'https://www.youtube.com/feeds/videos.xml?channel_id={}' +CHANNEL_URL = "https://www.youtube.com/{}" +RSS_URL = 'https://www.youtube.com/feeds/videos.xml?channel_id={}' CHANNEL_LIVE_URL = 'https://www.youtube.com/channel/{}' +COOKIES = {"SOCS": "CAESEwgDEgk0ODE3Nzk3MjQaAmVuIAEaBgiA_LyaBg"} PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_CHANNEL_ID): cv.string, @@ -34,22 +33,50 @@ async def async_setup_platform( hass, config, async_add_entities, discovery_info=None): # pylint: disable=unused-argument """Setup sensor platform.""" channel_id = config['channel_id'] + _LOGGER.debug(f'Setting up {channel_id}') session = async_create_clientsession(hass) + if channel_id.startswith('@'): + channel_id = await get_channel_id(session, channel_id) try: - url = BASE_URL.format(channel_id) - async with async_timeout.timeout(10): - response = await session.get(url) - info = await response.text() + url = RSS_URL.format(channel_id) + response = await session.get(url) + info = await response.text() name = info.split('