1 from __future__ import unicode_literals
5 from .common import InfoExtractor
7 get_element_by_attribute,
14 class InstagramIE(InfoExtractor):
15 _VALID_URL = r'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,
44 def _extract_embed_url(webpage):
45 blockquote_el = get_element_by_attribute(
46 'class', 'instagram-media', webpage)
47 if blockquote_el is None:
51 r'<a[^>]+href=([\'"])(?P<link>[^\'"]+)\1', blockquote_el)
53 return mobj.group('link')
55 def _real_extract(self, url):
56 video_id = self._match_id(url)
58 webpage = self._download_webpage(url, video_id)
59 uploader_id = self._search_regex(r'"owner":{"username":"(.+?)"',
60 webpage, 'uploader id', fatal=False)
61 desc = self._search_regex(
62 r'"caption":"(.+?)"', webpage, 'description', default=None)
64 desc = lowercase_escape(desc)
68 'url': self._og_search_video_url(webpage, secure=False),
70 'title': 'Video by %s' % uploader_id,
71 'thumbnail': self._og_search_thumbnail(webpage),
72 'uploader_id': uploader_id,
77 class InstagramUserIE(InfoExtractor):
78 _VALID_URL = r'https?://(?:www\.)?instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
79 IE_DESC = 'Instagram user profile'
80 IE_NAME = 'instagram:user'
82 'url': 'https://instagram.com/porsche',
87 'playlist_mincount': 2,
90 'id': '614605558512799803_462752227',
92 'title': '#Porsche Intelligent Performance.',
93 'thumbnail': 're:^https?://.*\.jpg',
94 'uploader': 'Porsche',
95 'uploader_id': 'porsche',
96 'timestamp': 1387486713,
97 'upload_date': '20131219',
101 'extract_flat': True,
102 'skip_download': True,
106 def _real_extract(self, url):
107 mobj = re.match(self._VALID_URL, url)
108 uploader_id = mobj.group('username')
112 media_url = 'http://instagram.com/%s/media' % uploader_id
114 page = self._download_json(
115 media_url, uploader_id,
116 note='Downloading page %d ' % (page_count + 1),
120 for it in page['items']:
121 if it.get('type') != 'video':
123 like_count = int_or_none(it.get('likes', {}).get('count'))
124 user = it.get('user', {})
128 'height': v.get('height'),
129 'width': v.get('width'),
131 } for k, v in it['videos'].items()]
132 self._sort_formats(formats)
134 thumbnails_el = it.get('images', {})
135 thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
137 # In some cases caption is null, which corresponds to None
138 # in python. As a result, it.get('caption', {}) gives None
139 title = (it.get('caption') or {}).get('text', it['id'])
143 'title': limit_length(title, 80),
145 'thumbnail': thumbnail,
146 'webpage_url': it.get('link'),
147 'uploader': user.get('full_name'),
148 'uploader_id': user.get('username'),
149 'like_count': like_count,
150 'timestamp': int_or_none(it.get('created_time')),
153 if not page['items']:
155 max_id = page['items'][-1]['id']
157 'http://instagram.com/%s/media?max_id=%s' % (
158 uploader_id, max_id))
164 'title': uploader_id,