]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/instagram.py
Prepare for release.
[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 _TEST = {
50 'url': 'http://instagram.com/porsche',
51 'info_dict': {
52 'id': 'porsche',
53 'title': 'porsche',
54 },
55 'playlist_mincount': 2,
56 'playlist': [{
57 'info_dict': {
58 'id': '614605558512799803_462752227',
59 'ext': 'mp4',
60 'title': '#Porsche Intelligent Performance.',
61 'thumbnail': 're:^https?://.*\.jpg',
62 'uploader': 'Porsche',
63 'uploader_id': 'porsche',
64 'timestamp': 1387486713,
65 'upload_date': '20131219',
66 },
67 }],
68 'params': {
69 'extract_flat': True,
70 'skip_download': True,
71 }
72 }
73
74 def _real_extract(self, url):
75 mobj = re.match(self._VALID_URL, url)
76 uploader_id = mobj.group('username')
77
78 entries = []
79 page_count = 0
80 media_url = 'http://instagram.com/%s/media' % uploader_id
81 while True:
82 page = self._download_json(
83 media_url, uploader_id,
84 note='Downloading page %d ' % (page_count + 1),
85 )
86 page_count += 1
87
88 for it in page['items']:
89 if it.get('type') != 'video':
90 continue
91 like_count = int_or_none(it.get('likes', {}).get('count'))
92 user = it.get('user', {})
93
94 formats = [{
95 'format_id': k,
96 'height': v.get('height'),
97 'width': v.get('width'),
98 'url': v['url'],
99 } for k, v in it['videos'].items()]
100 self._sort_formats(formats)
101
102 thumbnails_el = it.get('images', {})
103 thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
104
105 title = it.get('caption', {}).get('text', it['id'])
106
107 entries.append({
108 'id': it['id'],
109 'title': title,
110 'formats': formats,
111 'thumbnail': thumbnail,
112 'webpage_url': it.get('link'),
113 'uploader': user.get('full_name'),
114 'uploader_id': user.get('username'),
115 'like_count': like_count,
116 'timestamp': int_or_none(it.get('created_time')),
117 })
118
119 if not page['items']:
120 break
121 max_id = page['items'][-1]['id']
122 media_url = (
123 'http://instagram.com/%s/media?max_id=%s' % (
124 uploader_id, max_id))
125
126 return {
127 '_type': 'playlist',
128 'entries': entries,
129 'id': uploader_id,
130 'title': uploader_id,
131 }