1 from __future__ import unicode_literals
5 from .common import InfoExtractor
7 get_element_by_attribute,
14 class InstagramIE(InfoExtractor):
15 _VALID_URL = r'(?P<url>https?://(?:www\.)?instagram\.com/p/(?P<id>[^/?#&]+))'
17 'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc',
18 'md5': '0d2da106a9d2631273e192b372806516',
22 'uploader_id': 'naomipq',
23 'title': 'Video by naomipq',
24 'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
28 'url': 'https://www.instagram.com/p/BA-pQFBG8HZ/?taken-by=britneyspears',
32 'uploader_id': 'britneyspears',
33 'title': 'Video by britneyspears',
36 'skip_download': True,
39 'url': 'https://instagram.com/p/-Cmh1cukG2/',
40 'only_matching': True,
42 'url': 'http://instagram.com/p/9o6LshA7zy/embed/',
43 'only_matching': True,
47 def _extract_embed_url(webpage):
49 r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?instagram\.com/p/[^/]+/embed.*?)\1',
52 return mobj.group('url')
54 blockquote_el = get_element_by_attribute(
55 'class', 'instagram-media', webpage)
56 if blockquote_el is None:
60 r'<a[^>]+href=([\'"])(?P<link>[^\'"]+)\1', blockquote_el)
62 return mobj.group('link')
64 def _real_extract(self, url):
65 mobj = re.match(self._VALID_URL, url)
66 video_id = mobj.group('id')
67 url = mobj.group('url')
69 webpage = self._download_webpage(url, video_id)
70 uploader_id = self._search_regex(r'"owner":{"username":"(.+?)"',
71 webpage, 'uploader id', fatal=False)
72 desc = self._search_regex(
73 r'"caption":"(.+?)"', webpage, 'description', default=None)
75 desc = lowercase_escape(desc)
79 'url': self._og_search_video_url(webpage, secure=False),
81 'title': 'Video by %s' % uploader_id,
82 'thumbnail': self._og_search_thumbnail(webpage),
83 'uploader_id': uploader_id,
88 class InstagramUserIE(InfoExtractor):
89 _VALID_URL = r'https?://(?:www\.)?instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
90 IE_DESC = 'Instagram user profile'
91 IE_NAME = 'instagram:user'
93 'url': 'https://instagram.com/porsche',
98 'playlist_mincount': 2,
101 'id': '614605558512799803_462752227',
103 'title': '#Porsche Intelligent Performance.',
104 'thumbnail': 're:^https?://.*\.jpg',
105 'uploader': 'Porsche',
106 'uploader_id': 'porsche',
107 'timestamp': 1387486713,
108 'upload_date': '20131219',
112 'extract_flat': True,
113 'skip_download': True,
117 def _real_extract(self, url):
118 mobj = re.match(self._VALID_URL, url)
119 uploader_id = mobj.group('username')
123 media_url = 'http://instagram.com/%s/media' % uploader_id
125 page = self._download_json(
126 media_url, uploader_id,
127 note='Downloading page %d ' % (page_count + 1),
131 for it in page['items']:
132 if it.get('type') != 'video':
134 like_count = int_or_none(it.get('likes', {}).get('count'))
135 user = it.get('user', {})
139 'height': v.get('height'),
140 'width': v.get('width'),
142 } for k, v in it['videos'].items()]
143 self._sort_formats(formats)
145 thumbnails_el = it.get('images', {})
146 thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
148 # In some cases caption is null, which corresponds to None
149 # in python. As a result, it.get('caption', {}) gives None
150 title = (it.get('caption') or {}).get('text', it['id'])
154 'title': limit_length(title, 80),
156 'thumbnail': thumbnail,
157 'webpage_url': it.get('link'),
158 'uploader': user.get('full_name'),
159 'uploader_id': user.get('username'),
160 'like_count': like_count,
161 'timestamp': int_or_none(it.get('created_time')),
164 if not page['items']:
166 max_id = page['items'][-1]['id'].split('_')[0]
168 'http://instagram.com/%s/media?max_id=%s' % (
169 uploader_id, max_id))
175 'title': uploader_id,