1
0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-11-15 03:27:46 +01:00

[instagram] Improve extraction (closes #22880)

This commit is contained in:
Sergey M․ 2020-12-26 22:37:41 +07:00
parent 58e55198c1
commit f22b5a6b96
No known key found for this signature in database
GPG Key ID: 2C393E0F18A9236D

View File

@ -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')
uploader_id = media.get('owner', {}).get('username')
def get_count(key, kind): def get_count(key, kind):
return int_or_none(try_get( return int_or_none(try_get(
media, (lambda x: x['edge_media_%s' % key]['count'], media, (lambda x: x['edge_media_%s' % key]['count'],
lambda x: x['%ss' % kind]['count']))) lambda x: x['%ss' % kind]['count'])))
like_count = get_count('preview_like', 'like') like_count = get_count('preview_like', 'like')
comment_count = get_count('to_comment', 'comment') comment_count = get_count('to_comment', 'comment')
comments = [{ comments = [{
'author': comment.get('user', {}).get('username'), 'author': comment.get('user', {}).get('username'),
'author_id': comment.get('user', {}).get('id'), 'author_id': comment.get('user', {}).get('id'),
'id': comment.get('id'), 'id': comment.get('id'),
'text': comment.get('text'), 'text': comment.get('text'),
'timestamp': int_or_none(comment.get('created_at')), 'timestamp': int_or_none(comment.get('created_at')),
} for comment in media.get( } for comment in media.get(
'comments', {}).get('nodes', []) if comment.get('text')] 'comments', {}).get('nodes', []) if comment.get('text')]
if not video_url: if not video_url:
edges = try_get( edges = try_get(
media, lambda x: x['edge_sidecar_to_children']['edges'], media, lambda x: x['edge_sidecar_to_children']['edges'],
list) or [] list) or []
if edges: if edges:
entries = [] entries = []
for edge_num, edge in enumerate(edges, start=1): for edge_num, edge in enumerate(edges, start=1):
node = try_get(edge, lambda x: x['node'], dict) node = try_get(edge, lambda x: x['node'], dict)
if not node: if not node:
continue continue
node_video_url = url_or_none(node.get('video_url')) node_video_url = url_or_none(node.get('video_url'))
if not node_video_url: if not node_video_url:
continue continue
entries.append({ entries.append({
'id': node.get('shortcode') or node['id'], 'id': node.get('shortcode') or node['id'],
'title': 'Video %d' % edge_num, 'title': 'Video %d' % edge_num,
'url': node_video_url, 'url': node_video_url,
'thumbnail': node.get('display_url'), 'thumbnail': node.get('display_url'),
'width': int_or_none(try_get(node, lambda x: x['dimensions']['width'])), 'width': int_or_none(try_get(node, lambda x: x['dimensions']['width'])),
'height': int_or_none(try_get(node, lambda x: x['dimensions']['height'])), 'height': int_or_none(try_get(node, lambda x: x['dimensions']['height'])),
'view_count': int_or_none(node.get('video_view_count')), 'view_count': int_or_none(node.get('video_view_count')),
}) })
return self.playlist_result( return self.playlist_result(
entries, video_id, entries, video_id,
'Post by %s' % uploader_id if uploader_id else None, 'Post by %s' % uploader_id if uploader_id else None,
description) 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)