Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live attribute not working again! #11

Closed
pinkywafer opened this issue Oct 15, 2020 · 9 comments · Fixed by #12 or #13
Closed

Live attribute not working again! #11

pinkywafer opened this issue Oct 15, 2020 · 9 comments · Fixed by #12 or #13

Comments

@pinkywafer
Copy link
Collaborator

Version of the custom_component

0.3.1

Describe the bug
Live attribute is not working again (ref #8). Youtube have changed the page again, BADGE_STYLE_TYPE_LIVE_NOW (ref #9) is now also not used

log

None

I'm looking into alternatives

@pinkywafer
Copy link
Collaborator Author

ok, I think I have a working solution, however I'm not sure that it has the desired effect. For example, if a new video is uploaded the channel may still show live if another stream is going on. Additionally, if long-term streams are also ongoing, and videos are uploaded, it is possible that the live attribute does not show. I think I have a solution which checks the video itself to find out if that video is being streamed live, which I think would be the preferred option.
Either way, let me know and I'll sort out a pull request for whichever you prefer:

  1. current behaviour - just check the channel for live
  2. Check the video being reported as the latest for live

@physk
Copy link

physk commented Oct 19, 2020

I tried, i couldn't get it working. but i think your best option would be to do something along the lines of

async def is_live(video_url, name, hass, session):
    """Return bool if channel is live"""
    returnvalue = False
    try:
        async with async_timeout.timeout(10, loop=hass.loop):
            response = await session.get(video_url)
            info = await response.text()
        if 'Started streaming' in info and 'watching now' in info:
            returnvalue = True
            _LOGGER.debug('%s - Channel is live', name)
    except Exception as error:  # pylint: disable=broad-except
        _LOGGER.debug('%s - Could not update - %s', name, error)
    return returnvalue

but i am no Python Programmer!

This way it would get it directly from the video page and then if say a channel was streaming more than one thing it would not mess things up! :)

@pinkywafer
Copy link
Collaborator Author

That's one way i was looking at, checking the latest video. Your code's not quite right, you need to pass the video URL when calling rather than in the definition. I was looking for {\"key\":\"is_viewed_live\",\"value\":\"True\"} instead, as it should show only if the video is currently live.

I tried looking for the live icon on the Channel page, but it is not reliable 100%. If everyone's happy with checking the latest video (rather than the channel) I'll open a PR later.

@physk
Copy link

physk commented Oct 19, 2020

I don't think this is correct, I just opened up a live stream from last night (no longer live) and it still has

{\"key\":\"is_viewed_live\",\"value\":\"True\"}

but an uploaded video does not have it. So it seems like that flag is to show weather it was a live stream or an upload.

@pinkywafer
Copy link
Collaborator Author

Can you please give details of the video you are checking.
I've I've just tested again using the drzzs channel, so configured:

sensor:
  - platform: youtube
    channel_id: UC7G4tLa4Kt6A9e3hJ-HO8ng

There was a live stream on his channel yesterday evening (no longer live).
The sensor returns live: false
Note that the YouTube URL changes for a video. The URL while live is different to the URL returned after the stream has finished, so if you're checking the URL from the live stream, that could be the cause.

@ludeeus ludeeus reopened this Oct 19, 2020
@physk
Copy link

physk commented Oct 19, 2020

Sure, this is the video that comes up with

{\"key\":\"is_viewed_live\",\"value\":\"True\"}

even though it is no longer live (if you do a search in the source for the video) it was also coming up live with the new code you had committed.

https://www.youtube.com/watch?v=HBFp2z6tvUg

i have made a work around which seems to be working and detecting lives correctly

async def is_live(url, name, hass, session):
    """Return bool if channel is live"""
    returnvalue = False
    try:
        async with async_timeout.timeout(10, loop=hass.loop):
            response = await session.get(url)
            info = await response.text()
        if 'Started streaming' in info:
            returnvalue = True
            _LOGGER.debug('%s - Latest Video is live', name)
    except Exception as error:  # pylint: disable=broad-except
        _LOGGER.debug('%s - Could not update - %s', name, error)
    return returnvalue

@pinkywafer
Copy link
Collaborator Author

trying with if 'Started streaming' in info and 'watching now' in info: now

@pinkywafer
Copy link
Collaborator Author

thanks @physk confirmed started streaming working here also

@physk
Copy link

physk commented Oct 19, 2020

No worries, Glad I could help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants