Merge remote-tracking branch 'upstream/master' into bliptv
[youtube-dl] / youtube_dl / extractor / instagram.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     int_or_none,
8     limit_length,
9 )
10
11
12 class InstagramIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?instagram\.com/p/(?P<id>[^/?#&]+)'
14     _TESTS = [{
15         'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc',
16         'md5': '0d2da106a9d2631273e192b372806516',
17         'info_dict': {
18             'id': 'aye83DjauH',
19             'ext': 'mp4',
20             'uploader_id': 'naomipq',
21             'title': 'Video by naomipq',
22             'description': 'md5:1f17f0ab29bd6fe2bfad705f58de3cb8',
23         }
24     }, {
25         'url': 'https://instagram.com/p/-Cmh1cukG2/',
26         'only_matching': True,
27     }]
28
29     def _real_extract(self, url):
30         video_id = self._match_id(url)
31
32         webpage = self._download_webpage(url, video_id)
33         uploader_id = self._search_regex(r'"owner":{"username":"(.+?)"',
34                                          webpage, 'uploader id', fatal=False)
35         desc = self._search_regex(r'"caption":"(.*?)"', webpage, 'description',
36                                   fatal=False)
37
38         return {
39             'id': video_id,
40             'url': self._og_search_video_url(webpage, secure=False),
41             'ext': 'mp4',
42             'title': 'Video by %s' % uploader_id,
43             'thumbnail': self._og_search_thumbnail(webpage),
44             'uploader_id': uploader_id,
45             'description': desc,
46         }
47
48
49 class InstagramUserIE(InfoExtractor):
50     _VALID_URL = r'https://instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
51     IE_DESC = 'Instagram user profile'
52     IE_NAME = 'instagram:user'
53     _TEST = {
54         'url': 'https://instagram.com/porsche',
55         'info_dict': {
56             'id': 'porsche',
57             'title': 'porsche',
58         },
59         'playlist_mincount': 2,
60         'playlist': [{
61             'info_dict': {
62                 'id': '614605558512799803_462752227',
63                 'ext': 'mp4',
64                 'title': '#Porsche Intelligent Performance.',
65                 'thumbnail': 're:^https?://.*\.jpg',
66                 'uploader': 'Porsche',
67                 'uploader_id': 'porsche',
68                 'timestamp': 1387486713,
69                 'upload_date': '20131219',
70             },
71         }],
72         'params': {
73             'extract_flat': True,
74             'skip_download': True,
75         }
76     }
77
78     def _real_extract(self, url):
79         mobj = re.match(self._VALID_URL, url)
80         uploader_id = mobj.group('username')
81
82         entries = []
83         page_count = 0
84         media_url = 'http://instagram.com/%s/media' % uploader_id
85         while True:
86             page = self._download_json(
87                 media_url, uploader_id,
88                 note='Downloading page %d ' % (page_count + 1),
89             )
90             page_count += 1
91
92             for it in page['items']:
93                 if it.get('type') != 'video':
94                     continue
95                 like_count = int_or_none(it.get('likes', {}).get('count'))
96                 user = it.get('user', {})
97
98                 formats = [{
99                     'format_id': k,
100                     'height': v.get('height'),
101                     'width': v.get('width'),
102                     'url': v['url'],
103                 } for k, v in it['videos'].items()]
104                 self._sort_formats(formats)
105
106                 thumbnails_el = it.get('images', {})
107                 thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
108
109                 # In some cases caption is null, which corresponds to None
110                 # in python. As a result, it.get('caption', {}) gives None
111                 title = (it.get('caption') or {}).get('text', it['id'])
112
113                 entries.append({
114                     'id': it['id'],
115                     'title': limit_length(title, 80),
116                     'formats': formats,
117                     'thumbnail': thumbnail,
118                     'webpage_url': it.get('link'),
119                     'uploader': user.get('full_name'),
120                     'uploader_id': user.get('username'),
121                     'like_count': like_count,
122                     'timestamp': int_or_none(it.get('created_time')),
123                 })
124
125             if not page['items']:
126                 break
127             max_id = page['items'][-1]['id']
128             media_url = (
129                 'http://instagram.com/%s/media?max_id=%s' % (
130                     uploader_id, max_id))
131
132         return {
133             '_type': 'playlist',
134             'entries': entries,
135             'id': uploader_id,
136             'title': uploader_id,
137         }