]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/instagram.py
196407b063a9393b94c759be6c8080de9a494277
1 from __future__
import unicode_literals
5 from .common
import InfoExtractor
7 get_element_by_attribute
,
15 class InstagramIE(InfoExtractor
):
16 _VALID_URL
= r
'(?P<url>https?://(?:www\.)?instagram\.com/p/(?P<id>[^/?#&]+))'
18 'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc',
19 'md5': '0d2da106a9d2631273e192b372806516',
23 'title': 'Video by naomipq',
24 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
25 'thumbnail': 're:^https?://.*\.jpg',
26 'timestamp': 1371748545,
27 'upload_date': '20130620',
28 'uploader_id': 'naomipq',
29 'uploader': 'Naomi Leonor Phan-Quang',
36 'url': 'https://www.instagram.com/p/BA-pQFBG8HZ/?taken-by=britneyspears',
40 'title': 'Video by britneyspears',
41 'thumbnail': 're:^https?://.*\.jpg',
42 'timestamp': 1453760977,
43 'upload_date': '20160125',
44 'uploader_id': 'britneyspears',
45 'uploader': 'Britney Spears',
51 'skip_download': True,
54 'url': 'https://instagram.com/p/-Cmh1cukG2/',
55 'only_matching': True,
57 'url': 'http://instagram.com/p/9o6LshA7zy/embed/',
58 'only_matching': True,
62 def _extract_embed_url(webpage
):
64 r
'<iframe[^>]+src=(["\'])(?P
<url
>(?
:https?
:)?
//(?
:www\
.)?instagram\
.com
/p
/[^
/]+/embed
.*?
)\
1',
67 return mobj.group('url
')
69 blockquote_el = get_element_by_attribute(
70 'class', 'instagram
-media
', webpage)
71 if blockquote_el is None:
75 r'<a
[^
>]+href
=([\'"])(?P<link>[^\'"]+)\
1', blockquote_el)
77 return mobj.group('link
')
79 def _real_extract(self, url):
80 mobj = re.match(self._VALID_URL, url)
81 video_id = mobj.group('id')
82 url = mobj.group('url
')
84 webpage = self._download_webpage(url, video_id)
86 (video_url, description, thumbnail, timestamp, uploader,
87 uploader_id, like_count, comment_count, height, width) = [None] * 10
89 shared_data = self._parse_json(
91 r'window\
._sharedData\s
*=\s
*({.+?
});',
92 webpage, 'shared data
', default='{}'),
93 video_id, fatal=False)
96 shared_data, lambda x: x['entry_data
']['PostPage
'][0]['media
'], dict)
98 video_url = media.get('video_url
')
99 height = int_or_none(media.get('dimensions
', {}).get('height
'))
100 width = int_or_none(media.get('dimensions
', {}).get('width
'))
101 description = media.get('caption
')
102 thumbnail = media.get('display_src
')
103 timestamp = int_or_none(media.get('date
'))
104 uploader = media.get('owner
', {}).get('full_name
')
105 uploader_id = media.get('owner
', {}).get('username
')
106 like_count = int_or_none(media.get('likes
', {}).get('count
'))
107 comment_count = int_or_none(media.get('comments
', {}).get('count
'))
109 'author
': comment.get('user
', {}).get('username
'),
110 'author_id
': comment.get('user
', {}).get('id'),
111 'id': comment.get('id'),
112 'text
': comment.get('text
'),
113 'timestamp
': int_or_none(comment.get('created_at
')),
114 } for comment in media.get(
115 'comments
', {}).get('nodes
', []) if comment.get('text
')]
118 video_url = self._og_search_video_url(webpage, secure=False)
127 uploader_id = self._search_regex(
128 r'"owner"\s
*:\s
*{\s
*"username"\s
*:\s
*"(.+?)"',
129 webpage, 'uploader
id', fatal=False)
132 description = self._search_regex(
133 r'"caption"\s
*:\s
*"(.+?)"', webpage, 'description
', default=None)
134 if description is not None:
135 description = lowercase_escape(description)
138 thumbnail = self._og_search_thumbnail(webpage)
144 'title
': 'Video by
%s' % uploader_id,
145 'description
': description,
146 'thumbnail
': thumbnail,
147 'timestamp
': timestamp,
148 'uploader_id
': uploader_id,
149 'uploader
': uploader,
150 'like_count
': like_count,
151 'comment_count
': comment_count,
152 'comments
': comments,
156 class InstagramUserIE(InfoExtractor):
157 _VALID_URL = r'https?
://(?
:www\
.)?instagram\
.com
/(?P
<username
>[^
/]{2,})/?
(?
:$|
[?
#])'
158 IE_DESC
= 'Instagram user profile'
159 IE_NAME
= 'instagram:user'
161 'url': 'https://instagram.com/porsche',
166 'playlist_mincount': 2,
169 'id': '614605558512799803_462752227',
171 'title': '#Porsche Intelligent Performance.',
172 'thumbnail': 're:^https?://.*\.jpg',
173 'uploader': 'Porsche',
174 'uploader_id': 'porsche',
175 'timestamp': 1387486713,
176 'upload_date': '20131219',
180 'extract_flat': True,
181 'skip_download': True,
185 def _real_extract(self
, url
):
186 mobj
= re
.match(self
._VALID
_URL
, url
)
187 uploader_id
= mobj
.group('username')
191 media_url
= 'http://instagram.com/%s/media' % uploader_id
193 page
= self
._download
_json
(
194 media_url
, uploader_id
,
195 note
='Downloading page %d ' % (page_count
+ 1),
199 for it
in page
['items']:
200 if it
.get('type') != 'video':
202 like_count
= int_or_none(it
.get('likes', {}).get('count'))
203 user
= it
.get('user', {})
207 'height': v
.get('height'),
208 'width': v
.get('width'),
210 } for k
, v
in it
['videos'].items()]
211 self
._sort
_formats
(formats
)
213 thumbnails_el
= it
.get('images', {})
214 thumbnail
= thumbnails_el
.get('thumbnail', {}).get('url')
216 # In some cases caption is null, which corresponds to None
217 # in python. As a result, it.get('caption', {}) gives None
218 title
= (it
.get('caption') or {}).get('text', it
['id'])
222 'title': limit_length(title
, 80),
224 'thumbnail': thumbnail
,
225 'webpage_url': it
.get('link'),
226 'uploader': user
.get('full_name'),
227 'uploader_id': user
.get('username'),
228 'like_count': like_count
,
229 'timestamp': int_or_none(it
.get('created_time')),
232 if not page
['items']:
234 max_id
= page
['items'][-1]['id'].split('_')[0]
236 'http://instagram.com/%s/media?max_id=%s' % (
237 uploader_id
, max_id
))
243 'title': uploader_id
,