]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/instagram.py
debian/control: Prefer mplayer to mplayer2 in Recommends.
[youtubedl] / 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://instagram\.com/p/(?P<id>[\da-zA-Z]+)'
14 _TEST = {
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
26 def _real_extract(self, url):
27 video_id = self._match_id(url)
28
29 webpage = self._download_webpage(url, video_id)
30 uploader_id = self._search_regex(r'"owner":{"username":"(.+?)"',
31 webpage, 'uploader id', fatal=False)
32 desc = self._search_regex(r'"caption":"(.*?)"', webpage, 'description',
33 fatal=False)
34
35 return {
36 'id': video_id,
37 'url': self._og_search_video_url(webpage, secure=False),
38 'ext': 'mp4',
39 'title': 'Video by %s' % uploader_id,
40 'thumbnail': self._og_search_thumbnail(webpage),
41 'uploader_id': uploader_id,
42 'description': desc,
43 }
44
45
46 class InstagramUserIE(InfoExtractor):
47 _VALID_URL = r'https://instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
48 IE_DESC = 'Instagram user profile'
49 IE_NAME = 'instagram:user'
50 _TEST = {
51 'url': 'https://instagram.com/porsche',
52 'info_dict': {
53 'id': 'porsche',
54 'title': 'porsche',
55 },
56 'playlist_mincount': 2,
57 'playlist': [{
58 'info_dict': {
59 'id': '614605558512799803_462752227',
60 'ext': 'mp4',
61 'title': '#Porsche Intelligent Performance.',
62 'thumbnail': 're:^https?://.*\.jpg',
63 'uploader': 'Porsche',
64 'uploader_id': 'porsche',
65 'timestamp': 1387486713,
66 'upload_date': '20131219',
67 },
68 }],
69 'params': {
70 'extract_flat': True,
71 'skip_download': True,
72 }
73 }
74
75 def _real_extract(self, url):
76 mobj = re.match(self._VALID_URL, url)
77 uploader_id = mobj.group('username')
78
79 entries = []
80 page_count = 0
81 media_url = 'http://instagram.com/%s/media' % uploader_id
82 while True:
83 page = self._download_json(
84 media_url, uploader_id,
85 note='Downloading page %d ' % (page_count + 1),
86 )
87 page_count += 1
88
89 for it in page['items']:
90 if it.get('type') != 'video':
91 continue
92 like_count = int_or_none(it.get('likes', {}).get('count'))
93 user = it.get('user', {})
94
95 formats = [{
96 'format_id': k,
97 'height': v.get('height'),
98 'width': v.get('width'),
99 'url': v['url'],
100 } for k, v in it['videos'].items()]
101 self._sort_formats(formats)
102
103 thumbnails_el = it.get('images', {})
104 thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
105
106 # In some cases caption is null, which corresponds to None
107 # in python. As a result, it.get('caption', {}) gives None
108 title = (it.get('caption') or {}).get('text', it['id'])
109
110 entries.append({
111 'id': it['id'],
112 'title': limit_length(title, 80),
113 'formats': formats,
114 'thumbnail': thumbnail,
115 'webpage_url': it.get('link'),
116 'uploader': user.get('full_name'),
117 'uploader_id': user.get('username'),
118 'like_count': like_count,
119 'timestamp': int_or_none(it.get('created_time')),
120 })
121
122 if not page['items']:
123 break
124 max_id = page['items'][-1]['id']
125 media_url = (
126 'http://instagram.com/%s/media?max_id=%s' % (
127 uploader_id, max_id))
128
129 return {
130 '_type': 'playlist',
131 'entries': entries,
132 'id': uploader_id,
133 'title': uploader_id,
134 }