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