]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/instagram.py
1 from __future__
import unicode_literals
5 from .common
import InfoExtractor
6 from ..compat
import compat_str
8 get_element_by_attribute
,
16 class InstagramIE(InfoExtractor
):
17 _VALID_URL
= r
'(?P<url>https?://(?:www\.)?instagram\.com/p/(?P<id>[^/?#&]+))'
19 'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc',
20 'md5': '0d2da106a9d2631273e192b372806516',
24 'title': 'Video by naomipq',
25 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
26 'thumbnail': r
're:^https?://.*\.jpg',
27 'timestamp': 1371748545,
28 'upload_date': '20130620',
29 'uploader_id': 'naomipq',
30 'uploader': 'Naomi Leonor Phan-Quang',
37 'url': 'https://www.instagram.com/p/BA-pQFBG8HZ/?taken-by=britneyspears',
41 'title': 'Video by britneyspears',
42 'thumbnail': r
're:^https?://.*\.jpg',
43 'timestamp': 1453760977,
44 'upload_date': '20160125',
45 'uploader_id': 'britneyspears',
46 'uploader': 'Britney Spears',
52 'skip_download': True,
56 'url': 'https://www.instagram.com/p/BQ0eAlwhDrw/',
78 'title': 'Post by instagram',
79 'description': 'md5:0f9203fc6a2ce4d228da5754bcf54957',
82 'url': 'https://instagram.com/p/-Cmh1cukG2/',
83 'only_matching': True,
85 'url': 'http://instagram.com/p/9o6LshA7zy/embed/',
86 'only_matching': True,
90 def _extract_embed_url(webpage
):
92 r
'<iframe[^>]+src=(["\'])(?P
<url
>(?
:https?
:)?
//(?
:www\
.)?instagram\
.com
/p
/[^
/]+/embed
.*?
)\
1',
95 return mobj.group('url
')
97 blockquote_el = get_element_by_attribute(
98 'class', 'instagram
-media
', webpage)
99 if blockquote_el is None:
103 r'<a
[^
>]+href
=([\'"])(?P<link>[^\'"]+)\
1', blockquote_el)
105 return mobj.group('link
')
107 def _real_extract(self, url):
108 mobj = re.match(self._VALID_URL, url)
109 video_id = mobj.group('id')
110 url = mobj.group('url
')
112 webpage = self._download_webpage(url, video_id)
114 (video_url, description, thumbnail, timestamp, uploader,
115 uploader_id, like_count, comment_count, comments, height,
118 shared_data = self._parse_json(
120 r'window\
._sharedData\s
*=\s
*({.+?
});',
121 webpage, 'shared data
', default='{}'),
122 video_id, fatal=False)
126 (lambda x: x['entry_data
']['PostPage
'][0]['graphql
']['shortcode_media
'],
127 lambda x: x['entry_data
']['PostPage
'][0]['media
']),
130 video_url = media.get('video_url
')
131 height = int_or_none(media.get('dimensions
', {}).get('height
'))
132 width = int_or_none(media.get('dimensions
', {}).get('width
'))
133 description = media.get('caption
')
134 thumbnail = media.get('display_src
')
135 timestamp = int_or_none(media.get('date
'))
136 uploader = media.get('owner
', {}).get('full_name
')
137 uploader_id = media.get('owner
', {}).get('username
')
138 like_count = int_or_none(media.get('likes
', {}).get('count
'))
139 comment_count = int_or_none(media.get('comments
', {}).get('count
'))
141 'author
': comment.get('user
', {}).get('username
'),
142 'author_id
': comment.get('user
', {}).get('id'),
143 'id': comment.get('id'),
144 'text
': comment.get('text
'),
145 'timestamp
': int_or_none(comment.get('created_at
')),
146 } for comment in media.get(
147 'comments
', {}).get('nodes
', []) if comment.get('text
')]
150 media, lambda x: x['edge_sidecar_to_children
']['edges
'],
154 for edge_num, edge in enumerate(edges, start=1):
155 node = try_get(edge, lambda x: x['node
'], dict)
158 node_video_url = try_get(node, lambda x: x['video_url
'], compat_str)
159 if not node_video_url:
162 'id': node.get('shortcode
') or node['id'],
163 'title
': 'Video
%d' % edge_num,
164 'url
': node_video_url,
165 'thumbnail
': node.get('display_url
'),
166 'width
': int_or_none(try_get(node, lambda x: x['dimensions
']['width
'])),
167 'height
': int_or_none(try_get(node, lambda x: x['dimensions
']['height
'])),
168 'view_count
': int_or_none(node.get('video_view_count
')),
170 return self.playlist_result(
172 'Post by
%s' % uploader_id if uploader_id else None,
176 video_url = self._og_search_video_url(webpage, secure=False)
185 uploader_id = self._search_regex(
186 r'"owner"\s
*:\s
*{\s
*"username"\s
*:\s
*"(.+?)"',
187 webpage, 'uploader
id', fatal=False)
190 description = self._search_regex(
191 r'"caption"\s
*:\s
*"(.+?)"', webpage, 'description
', default=None)
192 if description is not None:
193 description = lowercase_escape(description)
196 thumbnail = self._og_search_thumbnail(webpage)
202 'title
': 'Video by
%s' % uploader_id,
203 'description
': description,
204 'thumbnail
': thumbnail,
205 'timestamp
': timestamp,
206 'uploader_id
': uploader_id,
207 'uploader
': uploader,
208 'like_count
': like_count,
209 'comment_count
': comment_count,
210 'comments
': comments,
214 class InstagramUserIE(InfoExtractor):
215 _VALID_URL = r'https?
://(?
:www\
.)?instagram\
.com
/(?P
<username
>[^
/]{2,})/?
(?
:$|
[?
#])'
216 IE_DESC
= 'Instagram user profile'
217 IE_NAME
= 'instagram:user'
219 'url': 'https://instagram.com/porsche',
224 'playlist_mincount': 2,
227 'id': '614605558512799803_462752227',
229 'title': '#Porsche Intelligent Performance.',
230 'thumbnail': r
're:^https?://.*\.jpg',
231 'uploader': 'Porsche',
232 'uploader_id': 'porsche',
233 'timestamp': 1387486713,
234 'upload_date': '20131219',
238 'extract_flat': True,
239 'skip_download': True,
243 def _real_extract(self
, url
):
244 mobj
= re
.match(self
._VALID
_URL
, url
)
245 uploader_id
= mobj
.group('username')
249 media_url
= 'http://instagram.com/%s/media' % uploader_id
251 page
= self
._download
_json
(
252 media_url
, uploader_id
,
253 note
='Downloading page %d ' % (page_count
+ 1),
257 for it
in page
['items']:
258 if it
.get('type') != 'video':
260 like_count
= int_or_none(it
.get('likes', {}).get('count'))
261 user
= it
.get('user', {})
265 'height': v
.get('height'),
266 'width': v
.get('width'),
268 } for k
, v
in it
['videos'].items()]
269 self
._sort
_formats
(formats
)
271 thumbnails_el
= it
.get('images', {})
272 thumbnail
= thumbnails_el
.get('thumbnail', {}).get('url')
274 # In some cases caption is null, which corresponds to None
275 # in python. As a result, it.get('caption', {}) gives None
276 title
= (it
.get('caption') or {}).get('text', it
['id'])
280 'title': limit_length(title
, 80),
282 'thumbnail': thumbnail
,
283 'webpage_url': it
.get('link'),
284 'uploader': user
.get('full_name'),
285 'uploader_id': user
.get('username'),
286 'like_count': like_count
,
287 'timestamp': int_or_none(it
.get('created_time')),
290 if not page
['items']:
292 max_id
= page
['items'][-1]['id'].split('_')[0]
294 'http://instagram.com/%s/media?max_id=%s' % (
295 uploader_id
, max_id
))
301 'title': uploader_id
,