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) = [None] * 8
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 description = media.get('caption')
100 thumbnail = media.get('display_src')
101 timestamp = int_or_none(media.get('date'))
102 uploader = media.get('owner', {}).get('full_name')
103 uploader_id = media.get('owner', {}).get('username')
104 like_count = int_or_none(media.get('likes', {}).get('count'))
105 comment_count = int_or_none(media.get('comments', {}).get('count'))
107 'author': comment.get('user', {}).get('username'),
108 'author_id': comment.get('user', {}).get('id'),
109 'id': comment.get('id'),
110 'text': comment.get('text'),
111 'timestamp': int_or_none(comment.get('created_at')),
112 } for comment in media.get('comments', {}).get('nodes', [])
113 if comment.get('text')]
116 video_url = self._og_search_video_url(webpage, secure=False)
119 uploader_id = self._search_regex(
120 r'"owner"\s*:\s*{\s*"username"\s*:\s*"(.+?)"',
121 webpage, 'uploader id', fatal=False)
124 description = self._search_regex(
125 r'"caption"\s*:\s*"(.+?)"', webpage, 'description', default=None)
126 if description is not None:
127 description = lowercase_escape(description)
130 thumbnail = self._og_search_thumbnail(webpage)
136 'title': 'Video by %s' % uploader_id,
137 'description': description,
138 'thumbnail': thumbnail,
139 'timestamp': timestamp,
140 'uploader_id': uploader_id,
141 'uploader': uploader,
142 'like_count': like_count,
143 'comment_count': comment_count,
144 'comments': comments,
148 class InstagramUserIE(InfoExtractor):
149 _VALID_URL = r'https?://(?:www\.)?instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
150 IE_DESC = 'Instagram user profile'
151 IE_NAME = 'instagram:user'
153 'url': 'https://instagram.com/porsche',
158 'playlist_mincount': 2,
161 'id': '614605558512799803_462752227',
163 'title': '#Porsche Intelligent Performance.',
164 'thumbnail': 're:^https?://.*\.jpg',
165 'uploader': 'Porsche',
166 'uploader_id': 'porsche',
167 'timestamp': 1387486713,
168 'upload_date': '20131219',
172 'extract_flat': True,
173 'skip_download': True,
177 def _real_extract(self, url):
178 mobj = re.match(self._VALID_URL, url)
179 uploader_id = mobj.group('username')
183 media_url = 'http://instagram.com/%s/media' % uploader_id
185 page = self._download_json(
186 media_url, uploader_id,
187 note='Downloading page %d ' % (page_count + 1),
191 for it in page['items']:
192 if it.get('type') != 'video':
194 like_count = int_or_none(it.get('likes', {}).get('count'))
195 user = it.get('user', {})
199 'height': v.get('height'),
200 'width': v.get('width'),
202 } for k, v in it['videos'].items()]
203 self._sort_formats(formats)
205 thumbnails_el = it.get('images', {})
206 thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
208 # In some cases caption is null, which corresponds to None
209 # in python. As a result, it.get('caption', {}) gives None
210 title = (it.get('caption') or {}).get('text', it['id'])
214 'title': limit_length(title, 80),
216 'thumbnail': thumbnail,
217 'webpage_url': it.get('link'),
218 'uploader': user.get('full_name'),
219 'uploader_id': user.get('username'),
220 'like_count': like_count,
221 'timestamp': int_or_none(it.get('created_time')),
224 if not page['items']:
226 max_id = page['items'][-1]['id'].split('_')[0]
228 'http://instagram.com/%s/media?max_id=%s' % (
229 uploader_id, max_id))
235 'title': uploader_id,