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',
35 'url': 'https://www.instagram.com/p/BA-pQFBG8HZ/?taken-by=britneyspears',
39 'title': 'Video by britneyspears',
40 'thumbnail': 're:^https?://.*\.jpg',
41 'timestamp': 1453760977,
42 'upload_date': '20160125',
43 'uploader_id': 'britneyspears',
44 'uploader': 'Britney Spears',
49 'skip_download': True,
52 'url': 'https://instagram.com/p/-Cmh1cukG2/',
53 'only_matching': True,
55 'url': 'http://instagram.com/p/9o6LshA7zy/embed/',
56 'only_matching': True,
60 def _extract_embed_url(webpage):
62 r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?instagram\.com/p/[^/]+/embed.*?)\1',
65 return mobj.group('url')
67 blockquote_el = get_element_by_attribute(
68 'class', 'instagram-media', webpage)
69 if blockquote_el is None:
73 r'<a[^>]+href=([\'"])(?P<link>[^\'"]+)\1', blockquote_el)
75 return mobj.group('link')
77 def _real_extract(self, url):
78 mobj = re.match(self._VALID_URL, url)
79 video_id = mobj.group('id')
80 url = mobj.group('url')
82 webpage = self._download_webpage(url, video_id)
84 (video_url, description, thumbnail, timestamp, uploader,
85 uploader_id, like_count, comment_count) = [None] * 8
87 shared_data = self._parse_json(
89 r'window\._sharedData\s*=\s*({.+?});',
90 webpage, 'shared data', default='{}'),
91 video_id, fatal=False)
94 shared_data, lambda x: x['entry_data']['PostPage'][0]['media'], dict)
96 video_url = media.get('video_url')
97 description = media.get('caption')
98 thumbnail = media.get('display_src')
99 timestamp = int_or_none(media.get('date'))
100 uploader = media.get('owner', {}).get('full_name')
101 uploader_id = media.get('owner', {}).get('username')
102 like_count = int_or_none(media.get('likes', {}).get('count'))
103 comment_count = int_or_none(media.get('comments', {}).get('count'))
106 video_url = self._og_search_video_url(webpage, secure=False)
109 uploader_id = self._search_regex(
110 r'"owner"\s*:\s*{\s*"username"\s*:\s*"(.+?)"',
111 webpage, 'uploader id', fatal=False)
114 description = self._search_regex(
115 r'"caption"\s*:\s*"(.+?)"', webpage, 'description', default=None)
116 if description is not None:
117 description = lowercase_escape(description)
120 thumbnail = self._og_search_thumbnail(webpage)
126 'title': 'Video by %s' % uploader_id,
127 'description': description,
128 'thumbnail': thumbnail,
129 'timestamp': timestamp,
130 'uploader_id': uploader_id,
131 'uploader': uploader,
132 'like_count': like_count,
133 'comment_count': comment_count,
137 class InstagramUserIE(InfoExtractor):
138 _VALID_URL = r'https?://(?:www\.)?instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
139 IE_DESC = 'Instagram user profile'
140 IE_NAME = 'instagram:user'
142 'url': 'https://instagram.com/porsche',
147 'playlist_mincount': 2,
150 'id': '614605558512799803_462752227',
152 'title': '#Porsche Intelligent Performance.',
153 'thumbnail': 're:^https?://.*\.jpg',
154 'uploader': 'Porsche',
155 'uploader_id': 'porsche',
156 'timestamp': 1387486713,
157 'upload_date': '20131219',
161 'extract_flat': True,
162 'skip_download': True,
166 def _real_extract(self, url):
167 mobj = re.match(self._VALID_URL, url)
168 uploader_id = mobj.group('username')
172 media_url = 'http://instagram.com/%s/media' % uploader_id
174 page = self._download_json(
175 media_url, uploader_id,
176 note='Downloading page %d ' % (page_count + 1),
180 for it in page['items']:
181 if it.get('type') != 'video':
183 like_count = int_or_none(it.get('likes', {}).get('count'))
184 user = it.get('user', {})
188 'height': v.get('height'),
189 'width': v.get('width'),
191 } for k, v in it['videos'].items()]
192 self._sort_formats(formats)
194 thumbnails_el = it.get('images', {})
195 thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
197 # In some cases caption is null, which corresponds to None
198 # in python. As a result, it.get('caption', {}) gives None
199 title = (it.get('caption') or {}).get('text', it['id'])
203 'title': limit_length(title, 80),
205 'thumbnail': thumbnail,
206 'webpage_url': it.get('link'),
207 'uploader': user.get('full_name'),
208 'uploader_id': user.get('username'),
209 'like_count': like_count,
210 'timestamp': int_or_none(it.get('created_time')),
213 if not page['items']:
215 max_id = page['items'][-1]['id'].split('_')[0]
217 'http://instagram.com/%s/media?max_id=%s' % (
218 uploader_id, max_id))
224 'title': uploader_id,