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