]>
Commit | Line | Data |
---|---|---|
0de668af JMF |
1 | from __future__ import unicode_literals |
2 | ||
59fc531f JMF |
3 | import re |
4 | ||
5 | from .common import InfoExtractor | |
e1ec9330 YCH |
6 | from ..utils import ( |
7 | int_or_none, | |
8 | limit_length, | |
9 | ) | |
59fc531f | 10 | |
0de668af | 11 | |
59fc531f | 12 | class InstagramIE(InfoExtractor): |
fc6e75dd | 13 | _VALID_URL = r'https://instagram\.com/p/(?P<id>[\da-zA-Z]+)' |
59fc531f | 14 | _TEST = { |
fc6e75dd | 15 | 'url': 'https://instagram.com/p/aye83DjauH/?foo=bar#abc', |
0de668af JMF |
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', | |
59fc531f JMF |
23 | } |
24 | } | |
25 | ||
26 | def _real_extract(self, url): | |
d2d8248f S |
27 | video_id = self._match_id(url) |
28 | ||
59fc531f | 29 | webpage = self._download_webpage(url, video_id) |
3f402177 | 30 | uploader_id = self._search_regex(r'"owner":{"username":"(.+?)"', |
9e1a5b84 | 31 | webpage, 'uploader id', fatal=False) |
0de668af | 32 | desc = self._search_regex(r'"caption":"(.*?)"', webpage, 'description', |
9e1a5b84 | 33 | fatal=False) |
59fc531f | 34 | |
0de668af JMF |
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, | |
46720279 | 40 | 'thumbnail': self._og_search_thumbnail(webpage), |
0de668af | 41 | 'uploader_id': uploader_id, |
3f402177 | 42 | 'description': desc, |
0de668af | 43 | } |
ea38e55f PH |
44 | |
45 | ||
46 | class InstagramUserIE(InfoExtractor): | |
fc6e75dd | 47 | _VALID_URL = r'https://instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])' |
ea38e55f PH |
48 | IE_DESC = 'Instagram user profile' |
49 | IE_NAME = 'instagram:user' | |
22a6f150 | 50 | _TEST = { |
fc6e75dd | 51 | 'url': 'https://instagram.com/porsche', |
22a6f150 PH |
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 | } | |
ea38e55f PH |
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 | ||
edb99d4c YCH |
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']) | |
ea38e55f PH |
109 | |
110 | entries.append({ | |
111 | 'id': it['id'], | |
e1ec9330 | 112 | 'title': limit_length(title, 80), |
ea38e55f PH |
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, | |
912b38b4 | 119 | 'timestamp': int_or_none(it.get('created_time')), |
ea38e55f PH |
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 | } |