]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/instagram.py
Merge pull request #8061 from dstftw/introduce-chapter-and-series-fields
[yt-dlp.git] / youtube_dl / extractor / instagram.py
CommitLineData
0de668af
JMF
1from __future__ import unicode_literals
2
59fc531f
JMF
3import re
4
5from .common import InfoExtractor
e1ec9330
YCH
6from ..utils import (
7 int_or_none,
8 limit_length,
9)
59fc531f 10
0de668af 11
59fc531f 12class InstagramIE(InfoExtractor):
67446fd4 13 _VALID_URL = r'https?://(?:www\.)?instagram\.com/p/(?P<id>[^/?#&]+)'
4479600d 14 _TESTS = [{
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 23 }
4479600d
S
24 }, {
25 'url': 'https://instagram.com/p/-Cmh1cukG2/',
26 'only_matching': True,
27 }]
59fc531f
JMF
28
29 def _real_extract(self, url):
d2d8248f
S
30 video_id = self._match_id(url)
31
59fc531f 32 webpage = self._download_webpage(url, video_id)
3f402177 33 uploader_id = self._search_regex(r'"owner":{"username":"(.+?)"',
9e1a5b84 34 webpage, 'uploader id', fatal=False)
0de668af 35 desc = self._search_regex(r'"caption":"(.*?)"', webpage, 'description',
9e1a5b84 36 fatal=False)
59fc531f 37
0de668af
JMF
38 return {
39 'id': video_id,
40 'url': self._og_search_video_url(webpage, secure=False),
41 'ext': 'mp4',
42 'title': 'Video by %s' % uploader_id,
46720279 43 'thumbnail': self._og_search_thumbnail(webpage),
0de668af 44 'uploader_id': uploader_id,
3f402177 45 'description': desc,
0de668af 46 }
ea38e55f
PH
47
48
49class InstagramUserIE(InfoExtractor):
dcdc3523 50 _VALID_URL = r'https?://(?:www\.)?instagram\.com/(?P<username>[^/]{2,})/?(?:$|[?#])'
ea38e55f
PH
51 IE_DESC = 'Instagram user profile'
52 IE_NAME = 'instagram:user'
22a6f150 53 _TEST = {
fc6e75dd 54 'url': 'https://instagram.com/porsche',
22a6f150
PH
55 'info_dict': {
56 'id': 'porsche',
57 'title': 'porsche',
58 },
59 'playlist_mincount': 2,
60 'playlist': [{
61 'info_dict': {
62 'id': '614605558512799803_462752227',
63 'ext': 'mp4',
64 'title': '#Porsche Intelligent Performance.',
65 'thumbnail': 're:^https?://.*\.jpg',
66 'uploader': 'Porsche',
67 'uploader_id': 'porsche',
68 'timestamp': 1387486713,
69 'upload_date': '20131219',
70 },
71 }],
72 'params': {
73 'extract_flat': True,
74 'skip_download': True,
75 }
76 }
ea38e55f
PH
77
78 def _real_extract(self, url):
79 mobj = re.match(self._VALID_URL, url)
80 uploader_id = mobj.group('username')
81
82 entries = []
83 page_count = 0
84 media_url = 'http://instagram.com/%s/media' % uploader_id
85 while True:
86 page = self._download_json(
87 media_url, uploader_id,
88 note='Downloading page %d ' % (page_count + 1),
89 )
90 page_count += 1
91
92 for it in page['items']:
93 if it.get('type') != 'video':
94 continue
95 like_count = int_or_none(it.get('likes', {}).get('count'))
96 user = it.get('user', {})
97
98 formats = [{
99 'format_id': k,
100 'height': v.get('height'),
101 'width': v.get('width'),
102 'url': v['url'],
103 } for k, v in it['videos'].items()]
104 self._sort_formats(formats)
105
106 thumbnails_el = it.get('images', {})
107 thumbnail = thumbnails_el.get('thumbnail', {}).get('url')
108
edb99d4c
YCH
109 # In some cases caption is null, which corresponds to None
110 # in python. As a result, it.get('caption', {}) gives None
111 title = (it.get('caption') or {}).get('text', it['id'])
ea38e55f
PH
112
113 entries.append({
114 'id': it['id'],
e1ec9330 115 'title': limit_length(title, 80),
ea38e55f
PH
116 'formats': formats,
117 'thumbnail': thumbnail,
118 'webpage_url': it.get('link'),
119 'uploader': user.get('full_name'),
120 'uploader_id': user.get('username'),
121 'like_count': like_count,
912b38b4 122 'timestamp': int_or_none(it.get('created_time')),
ea38e55f
PH
123 })
124
125 if not page['items']:
126 break
127 max_id = page['items'][-1]['id']
128 media_url = (
129 'http://instagram.com/%s/media?max_id=%s' % (
130 uploader_id, max_id))
131
132 return {
133 '_type': 'playlist',
134 'entries': entries,
135 'id': uploader_id,
136 'title': uploader_id,
137 }