[snapchat] Improve robustness

This commit is contained in:
Fynn 2021-09-10 18:45:31 +02:00
parent 082dfa495e
commit 80bc41f018

View File

@ -1,8 +1,7 @@
# coding: utf-8 # coding: utf-8
from __future__ import unicode_literals from __future__ import unicode_literals
import re
from ..utils import unified_timestamp, parse_duration from ..utils import unified_timestamp, parse_duration, try_get, str_or_none, ExtractorError
from .common import InfoExtractor from .common import InfoExtractor
@ -15,11 +14,11 @@ class SnapchatIE(InfoExtractor):
'id': 'W7_EDlXWTBiXAEEniNoMPwAAYuz9_1mcdex8MAXndPyIOAXndPyFyAO1OAA', 'id': 'W7_EDlXWTBiXAEEniNoMPwAAYuz9_1mcdex8MAXndPyIOAXndPyFyAO1OAA',
'ext': 'mp4', 'ext': 'mp4',
'title': 'W7_EDlXWTBiXAEEniNoMPwAAYuz9_1mcdex8MAXndPyIOAXndPyFyAO1OAA', 'title': 'W7_EDlXWTBiXAEEniNoMPwAAYuz9_1mcdex8MAXndPyIOAXndPyFyAO1OAA',
'thumbnail': 'https://s.sc-cdn.net/p9mgs8YyMZzfFRls4HGZxwXWBGVF-cEa6ZYPuCT2xPk=/default/preview.jpg', 'thumbnail': r're:https://s\.sc-cdn\.net/.+\.jpg',
'description': '#spotlight', 'description': '#spotlight',
'timestamp': 1622914559, 'timestamp': 1622914559,
'upload_date': '20210605', 'upload_date': '20210605',
'view_count': 62100, 'view_count': 72100,
'uploader_id': None 'uploader_id': None
} }
} }
@ -28,27 +27,23 @@ class SnapchatIE(InfoExtractor):
video_id = self._match_id(url) video_id = self._match_id(url)
webpage = self._download_webpage(url, video_id) webpage = self._download_webpage(url, video_id)
schema_video_object_raw = self._html_search_regex(r'<script data-react-helmet="true" type="application/ld\+json">(.+)</script>', schema_video_object_raw = self._html_search_regex(r'<script\s[^>]*?data-react-helmet\s*=\s*"true"\s[^>]*?type\s*=\s*"application/ld\+json">(.+?)</script>',
webpage, 'schema_video_object') webpage, 'schema_video_object')
schema_video_object = self._parse_json(schema_video_object_raw, video_id, fatal=True) schema_video_object = self._parse_json(schema_video_object_raw, video_id, fatal=True)
video_url = schema_video_object.get('contentUrl') try:
video_url = str_or_none(schema_video_object['contentUrl'])
if not video_url:
raise ValueError('video_url must be non-empty string')
except (TypeError, ValueError) as e:
raise ExtractorError('Unexpected format for schema_video_object', cause=e, video_id=video_id)
title = schema_video_object.get('name') title = schema_video_object.get('name')
if not title: if not title:
matching = re.match(r'sc-cdn.net/\w+/(\w+)', video_url) title = self._generic_title(url)
if matching:
title = matching.group(1)
if not title:
title = video_id
try: views = try_get(schema_video_object.get('interactionStatistic'), lambda x: x['userInteractionCount'])
views = schema_video_object.get('interactionStatistic').get('userInteractionCount')
except AttributeError:
views = None
try: uploader_id = try_get(schema_video_object.get('creator'), lambda x: x['alternateName'])
uploader_id = schema_video_object.get('creator').get('alternateName')
except AttributeError:
uploader_id = None
return { return {
'id': video_id, 'id': video_id,