Thanks to visit codestin.com
Credit goes to github.com

Skip to content

plugin.youtube: find video id in page, fall back to API#1746

Merged
gravyboat merged 4 commits into
streamlink:masterfrom
beardypig:youtube-api-fallback
Jun 8, 2018
Merged

plugin.youtube: find video id in page, fall back to API#1746
gravyboat merged 4 commits into
streamlink:masterfrom
beardypig:youtube-api-fallback

Conversation

@beardypig

Copy link
Copy Markdown
Member

As discussed in #1728 with @back-to, this PR modifies the YouTube plugin to search for the channel's video ID in the page first before trying the API. This should alleviate the pressure on the YouTube API key.

Also added some better error logging if there is an error when calling the API.

Requires testing on more channels

Fixes #1728

@beardypig beardypig added the WIP Work in process label Jun 4, 2018
@beardypig beardypig force-pushed the youtube-api-fallback branch from 6b81321 to 7ab5661 Compare June 4, 2018 14:04
@codecov

codecov Bot commented Jun 4, 2018

Copy link
Copy Markdown

Codecov Report

Merging #1746 into master will increase coverage by 0.13%.
The diff coverage is 34%.

@@            Coverage Diff            @@
##           master   #1746      +/-   ##
=========================================
+ Coverage   49.86%     50%   +0.13%     
=========================================
  Files         238     238              
  Lines       13976   14025      +49     
=========================================
+ Hits         6969    7013      +44     
- Misses       7007    7012       +5

@Twilight0

Twilight0 commented Jun 4, 2018

Copy link
Copy Markdown
Contributor

While processing this, wouldn't be nice to be able to set custom API in plugin options? On the other hand protected videos might play with fallback method.

@beardypig

Copy link
Copy Markdown
Member Author

@Twilight0 you mean to be able to set a custom API key?

@Twilight0

Copy link
Copy Markdown
Contributor

Yes I meant custom API key.

@beardypig

Copy link
Copy Markdown
Member Author

It could be added (I wrote the code already), but it might not be needed now.

Comment thread src/streamlink/plugins/youtube.py Outdated
# find the videoRenderer object, where there is a LVE NOW badge
for x in search_dict(data, 'videoRenderer'):
for blabel in search_dict(x.get("badges", {}), "label"):
if blabel == "LIVE NOW":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if blabel == "LIVE NOW":

won't work as this string changes with every different youtube language version

maybe you can use the

'style': 'BADGE_STYLE_TYPE_LIVE_NOW'

maybe this work,
tested on


some urls from the header will work too

Comment thread src/streamlink/plugins/youtube.py Outdated
match = _channelid_re.search(res.text)
if not match:
return
res = http.get(self.url, headers={"User-Agent": useragents.CHROME})

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you also force the chrome header on the whole plugin and remove all the other headers

if the youtube stream is embedded,
the User-Agent can change to something else from the previous plugin and might break some regex

     def _get_streams(self):
+        http.headers.update({'User-Agent': useragents.CHROME})

@beardypig

Copy link
Copy Markdown
Member Author

Good points, thanks @back-to

@Twilight0

Copy link
Copy Markdown
Contributor

Thank you for adding the custom option for user-set API Key.

@gravyboat

Copy link
Copy Markdown
Member

This looks good. Do you still want to perform more testing on live channels or are you okay with getting this in now @beardypig.

@beardypig

Copy link
Copy Markdown
Member Author

I've tested it on quite a few live videos and it seems to work well for me - there is always the fallback to the API if it does break or doesn't work for some channels :)

@gravyboat

Copy link
Copy Markdown
Member

Okay merging then, thanks @beardypig.

@gravyboat gravyboat merged commit 0dd6add into streamlink:master Jun 8, 2018
@gravyboat gravyboat added plugin issue A Plugin does not work correctly enhancement and removed WIP Work in process labels Jun 8, 2018
@iball

iball commented Jun 9, 2018

Copy link
Copy Markdown

Just tested this on a few live streams myself using 13.0 and these updated YouTube plugin files and it's working again.
Thanks!

mkbloke pushed a commit to mkbloke/streamlink that referenced this pull request Aug 18, 2020
)

* plugin.youtube: find video id in page, fall back to API

* fix test in python 2.7

* plugins.youtube: option for custom api key

* plugins.youtube: update user-agent and live detection key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement plugin issue A Plugin does not work correctly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants