[instagram] Improve extraction (closes #22880)

pull/27132/merge
Sergey M․ 4 years ago
parent 58e55198c1
commit f22b5a6b96
No known key found for this signature in database
GPG Key ID: 2C393E0F18A9236D

@ -122,9 +122,9 @@ class InstagramIE(InfoExtractor):
webpage = self._download_webpage(url, video_id) webpage = self._download_webpage(url, video_id)
(video_url, description, thumbnail, timestamp, uploader, (media, video_url, description, thumbnail, timestamp, uploader,
uploader_id, like_count, comment_count, comments, height, uploader_id, like_count, comment_count, comments, height,
width) = [None] * 11 width) = [None] * 12
shared_data = self._parse_json( shared_data = self._parse_json(
self._search_regex( self._search_regex(
@ -137,69 +137,71 @@ class InstagramIE(InfoExtractor):
(lambda x: x['entry_data']['PostPage'][0]['graphql']['shortcode_media'], (lambda x: x['entry_data']['PostPage'][0]['graphql']['shortcode_media'],
lambda x: x['entry_data']['PostPage'][0]['media']), lambda x: x['entry_data']['PostPage'][0]['media']),
dict) dict)
if not media: # _sharedData.entry_data.PostPage is empty when authenticated (see
additional_data = self._parse_json( # https://github.com/ytdl-org/youtube-dl/pull/22880)
self._search_regex(r'window\.__additionalDataLoaded\(\'[^\']+\',\s*({.+?})\);', if not media:
webpage, 'additional data', default='{}'), additional_data = self._parse_json(
video_id, fatal=False) self._search_regex(
if additional_data: r'window\.__additionalDataLoaded\s*\(\s*[^,]+,\s*({.+?})\s*\)\s*;',
media = try_get( webpage, 'additional data', default='{}'),
additional_data, video_id, fatal=False)
lambda x: x['graphql']['shortcode_media'], if additional_data:
dict) media = try_get(
if media: additional_data, lambda x: x['graphql']['shortcode_media'],
video_url = media.get('video_url') dict)
height = int_or_none(media.get('dimensions', {}).get('height')) if media:
width = int_or_none(media.get('dimensions', {}).get('width')) video_url = media.get('video_url')
description = try_get( height = int_or_none(media.get('dimensions', {}).get('height'))
media, lambda x: x['edge_media_to_caption']['edges'][0]['node']['text'], width = int_or_none(media.get('dimensions', {}).get('width'))
compat_str) or media.get('caption') description = try_get(
thumbnail = media.get('display_src') media, lambda x: x['edge_media_to_caption']['edges'][0]['node']['text'],
timestamp = int_or_none(media.get('taken_at_timestamp') or media.get('date')) compat_str) or media.get('caption')
uploader = media.get('owner', {}).get('full_name') thumbnail = media.get('display_src')
uploader_id = media.get('owner', {}).get('username') timestamp = int_or_none(media.get('taken_at_timestamp') or media.get('date'))
uploader = media.get('owner', {}).get('full_name')
def get_count(key, kind): uploader_id = media.get('owner', {}).get('username')
return int_or_none(try_get(
media, (lambda x: x['edge_media_%s' % key]['count'], def get_count(key, kind):
lambda x: x['%ss' % kind]['count']))) return int_or_none(try_get(
like_count = get_count('preview_like', 'like') media, (lambda x: x['edge_media_%s' % key]['count'],
comment_count = get_count('to_comment', 'comment') lambda x: x['%ss' % kind]['count'])))
like_count = get_count('preview_like', 'like')
comments = [{ comment_count = get_count('to_comment', 'comment')
'author': comment.get('user', {}).get('username'),
'author_id': comment.get('user', {}).get('id'), comments = [{
'id': comment.get('id'), 'author': comment.get('user', {}).get('username'),
'text': comment.get('text'), 'author_id': comment.get('user', {}).get('id'),
'timestamp': int_or_none(comment.get('created_at')), 'id': comment.get('id'),
} for comment in media.get( 'text': comment.get('text'),
'comments', {}).get('nodes', []) if comment.get('text')] 'timestamp': int_or_none(comment.get('created_at')),
if not video_url: } for comment in media.get(
edges = try_get( 'comments', {}).get('nodes', []) if comment.get('text')]
media, lambda x: x['edge_sidecar_to_children']['edges'], if not video_url:
list) or [] edges = try_get(
if edges: media, lambda x: x['edge_sidecar_to_children']['edges'],
entries = [] list) or []
for edge_num, edge in enumerate(edges, start=1): if edges:
node = try_get(edge, lambda x: x['node'], dict) entries = []
if not node: for edge_num, edge in enumerate(edges, start=1):
continue node = try_get(edge, lambda x: x['node'], dict)
node_video_url = url_or_none(node.get('video_url')) if not node:
if not node_video_url: continue
continue node_video_url = url_or_none(node.get('video_url'))
entries.append({ if not node_video_url:
'id': node.get('shortcode') or node['id'], continue
'title': 'Video %d' % edge_num, entries.append({
'url': node_video_url, 'id': node.get('shortcode') or node['id'],
'thumbnail': node.get('display_url'), 'title': 'Video %d' % edge_num,
'width': int_or_none(try_get(node, lambda x: x['dimensions']['width'])), 'url': node_video_url,
'height': int_or_none(try_get(node, lambda x: x['dimensions']['height'])), 'thumbnail': node.get('display_url'),
'view_count': int_or_none(node.get('video_view_count')), 'width': int_or_none(try_get(node, lambda x: x['dimensions']['width'])),
}) 'height': int_or_none(try_get(node, lambda x: x['dimensions']['height'])),
return self.playlist_result( 'view_count': int_or_none(node.get('video_view_count')),
entries, video_id, })
'Post by %s' % uploader_id if uploader_id else None, return self.playlist_result(
description) entries, video_id,
'Post by %s' % uploader_id if uploader_id else None,
description)
if not video_url: if not video_url:
video_url = self._og_search_video_url(webpage, secure=False) video_url = self._og_search_video_url(webpage, secure=False)

Loading…
Cancel
Save