Reputation: 272
I use a python library that throws an exception that is known to be wrong. In fact, a recent pull requests suggests to just remove the line that throws the exception.
Is there a way to force the python code to continue ignoring the exception without breaking out of the sub-module code? Embedding the library call into an except block does not help, because subsequent code won't be executed.
I could merge the pull request myself and install a custom version of the library, but this breaks the whole deployment/dependency/update process.
The code I want to ignore consists out of these two lines:
if '<img class="icon meh" src="/yts/img' not in self.watch_html:
raise VideoUnavailable('This video is unavailable.')
from this file that has been patched by this commit in this pr.
Upvotes: 1
Views: 406
Reputation: 2266
I don't think this is possible, and even if it was, I believe it would only be less maintainable.
I think the best solution in this case would be to subclass and override the function in question. The drawbacks are that if you update the library, you have to update the function as well, until the problem is solved completely. It could look like this, having removed the exception-lines, using the code you linked from pytube:
from pytube import Caption
from pytube import CaptionQuery
from pytube import extract
from pytube import mixins
from pytube import request
from pytube import Stream
from pytube import StreamQuery
from pytube.compat import install_proxy
from pytube.compat import parse_qsl
from pytube.exceptions import VideoUnavailable
from pytube.helpers import apply_mixin
from pytube import YouTube
class MyYouTube(YouTube):
# https://github.com/nficano/pytube/blob/master/pytube/__main__.py#L150
def prefetch(self):
"""Eagerly download all necessary data.
Eagerly executes all necessary network requests so all other
operations don't does need to make calls outside of the interpreter
which blocks for long periods of time.
:rtype: None
"""
self.watch_html = request.get(url=self.watch_url)
self.embed_html = request.get(url=self.embed_url)
self.age_restricted = extract.is_age_restricted(self.watch_html)
self.vid_info_url = extract.video_info_url(
video_id=self.video_id,
watch_url=self.watch_url,
watch_html=self.watch_html,
embed_html=self.embed_html,
age_restricted=self.age_restricted,
)
self.vid_info = request.get(self.vid_info_url)
if not self.age_restricted:
self.js_url = extract.js_url(self.watch_html, self.age_restricted)
self.js = request.get(self.js_url)
#YouTube('https://www.youtube.com/watch?v=irauhITDrsE').streams.first().download() # Doesnt work
MyYouTube('https://www.youtube.com/watch?v=irauhITDrsE').streams.first().download() # Works
Depending on the programming language, this may not always be possible due to visibility. Luckily Python doesn't care much.
Upvotes: 1