From d4f14a72dc1dd79396e0e80980268aee902b61e4 Mon Sep 17 00:00:00 2001 From: bashonly <88596187+bashonly@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:28:18 -0600 Subject: [PATCH] [ie] Do not test truth value of `xml.etree.ElementTree.Element` (#8582) Testing the truthiness of an `xml.etree.ElementTree.Element` instance is deprecated in py3.12 Authored by: bashonly --- yt_dlp/extractor/cbc.py | 7 ++++--- yt_dlp/extractor/common.py | 4 +++- yt_dlp/extractor/mtv.py | 3 ++- yt_dlp/extractor/nbc.py | 7 +++++-- yt_dlp/extractor/slideslive.py | 12 +++++++----- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/yt_dlp/extractor/cbc.py b/yt_dlp/extractor/cbc.py index be2d13e44..29f0e307d 100644 --- a/yt_dlp/extractor/cbc.py +++ b/yt_dlp/extractor/cbc.py @@ -1,8 +1,9 @@ -import re -import json import base64 +import json +import re import time import urllib.parse +import xml.etree.ElementTree from .common import InfoExtractor from ..compat import ( @@ -387,7 +388,7 @@ class CBCGemIE(InfoExtractor): url = re.sub(r'(Manifest\(.*?),format=[\w-]+(.*?\))', r'\1\2', base_url) secret_xml = self._download_xml(url, video_id, note='Downloading secret XML', fatal=False) - if not secret_xml: + if not isinstance(secret_xml, xml.etree.ElementTree.Element): return for child in secret_xml: diff --git a/yt_dlp/extractor/common.py b/yt_dlp/extractor/common.py index c3ceb0039..b3a45b3fb 100644 --- a/yt_dlp/extractor/common.py +++ b/yt_dlp/extractor/common.py @@ -2225,7 +2225,9 @@ class InfoExtractor: mpd_url, video_id, note='Downloading MPD VOD manifest' if note is None else note, errnote='Failed to download VOD manifest' if errnote is None else errnote, - fatal=False, data=data, headers=headers, query=query) or {} + fatal=False, data=data, headers=headers, query=query) + if not isinstance(mpd_doc, xml.etree.ElementTree.Element): + return None return int_or_none(parse_duration(mpd_doc.get('mediaPresentationDuration'))) @staticmethod diff --git a/yt_dlp/extractor/mtv.py b/yt_dlp/extractor/mtv.py index 0d700b9a8..e192453c7 100644 --- a/yt_dlp/extractor/mtv.py +++ b/yt_dlp/extractor/mtv.py @@ -1,4 +1,5 @@ import re +import xml.etree.ElementTree from .common import InfoExtractor from ..compat import compat_str @@ -137,7 +138,7 @@ class MTVServicesInfoExtractor(InfoExtractor): mediagen_doc = self._download_xml( mediagen_url, video_id, 'Downloading video urls', fatal=False) - if mediagen_doc is False: + if not isinstance(mediagen_doc, xml.etree.ElementTree.Element): return None item = mediagen_doc.find('./video/item') diff --git a/yt_dlp/extractor/nbc.py b/yt_dlp/extractor/nbc.py index 666550a49..2d3aa26ec 100644 --- a/yt_dlp/extractor/nbc.py +++ b/yt_dlp/extractor/nbc.py @@ -1,6 +1,7 @@ import base64 import json import re +import xml.etree.ElementTree from .common import InfoExtractor from .theplatform import ThePlatformIE, default_ns @@ -803,8 +804,10 @@ class NBCStationsIE(InfoExtractor): smil = self._download_xml( f'https://link.theplatform.com/s/{pdk_acct}/{player_id}', video_id, note='Downloading SMIL data', query=query, fatal=is_live) - subtitles = self._parse_smil_subtitles(smil, default_ns) if smil else {} - for video in smil.findall(self._xpath_ns('.//video', default_ns)) if smil else []: + if not isinstance(smil, xml.etree.ElementTree.Element): + smil = None + subtitles = self._parse_smil_subtitles(smil, default_ns) if smil is not None else {} + for video in smil.findall(self._xpath_ns('.//video', default_ns)) if smil is not None else []: info['duration'] = float_or_none(remove_end(video.get('dur'), 'ms'), 1000) video_src_url = video.get('src') ext = mimetype2ext(video.get('type'), default=determine_ext(video_src_url)) diff --git a/yt_dlp/extractor/slideslive.py b/yt_dlp/extractor/slideslive.py index 25f867a60..13f3109d7 100644 --- a/yt_dlp/extractor/slideslive.py +++ b/yt_dlp/extractor/slideslive.py @@ -1,5 +1,6 @@ import re import urllib.parse +import xml.etree.ElementTree from .common import InfoExtractor from ..utils import ( @@ -469,11 +470,12 @@ class SlidesLiveIE(InfoExtractor): slides = self._download_xml( player_info['slides_xml_url'], video_id, fatal=False, note='Downloading slides XML', errnote='Failed to download slides info') - slide_url_template = 'https://cdn.slideslive.com/data/presentations/%s/slides/big/%s%s' - for slide_id, slide in enumerate(slides.findall('./slide') if slides else [], 1): - slides_info.append(( - slide_id, xpath_text(slide, './slideName', 'name'), '.jpg', - int_or_none(xpath_text(slide, './timeSec', 'time')))) + if isinstance(slides, xml.etree.ElementTree.Element): + slide_url_template = 'https://cdn.slideslive.com/data/presentations/%s/slides/big/%s%s' + for slide_id, slide in enumerate(slides.findall('./slide')): + slides_info.append(( + slide_id, xpath_text(slide, './slideName', 'name'), '.jpg', + int_or_none(xpath_text(slide, './timeSec', 'time')))) chapters, thumbnails = [], [] if url_or_none(player_info.get('thumbnail')):