]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/periscope.py
Allow extractors to specify section_start/end for clips
[yt-dlp.git] / yt_dlp / extractor / periscope.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 int_or_none,
6 parse_iso8601,
7 unescapeHTML,
8 )
9
10
11 class PeriscopeBaseIE(InfoExtractor):
12 _M3U8_HEADERS = {
13 'Referer': 'https://www.periscope.tv/'
14 }
15
16 def _call_api(self, method, query, item_id):
17 return self._download_json(
18 'https://api.periscope.tv/api/v2/%s' % method,
19 item_id, query=query)
20
21 def _parse_broadcast_data(self, broadcast, video_id):
22 title = broadcast.get('status') or 'Periscope Broadcast'
23 uploader = broadcast.get('user_display_name') or broadcast.get('username')
24 title = '%s - %s' % (uploader, title) if uploader else title
25 is_live = broadcast.get('state').lower() == 'running'
26
27 thumbnails = [{
28 'url': broadcast[image],
29 } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
30
31 return {
32 'id': broadcast.get('id') or video_id,
33 'title': title,
34 'timestamp': parse_iso8601(broadcast.get('created_at')),
35 'uploader': uploader,
36 'uploader_id': broadcast.get('user_id') or broadcast.get('username'),
37 'thumbnails': thumbnails,
38 'view_count': int_or_none(broadcast.get('total_watched')),
39 'tags': broadcast.get('tags'),
40 'is_live': is_live,
41 }
42
43 @staticmethod
44 def _extract_common_format_info(broadcast):
45 return broadcast.get('state').lower(), int_or_none(broadcast.get('width')), int_or_none(broadcast.get('height'))
46
47 @staticmethod
48 def _add_width_and_height(f, width, height):
49 for key, val in (('width', width), ('height', height)):
50 if not f.get(key):
51 f[key] = val
52
53 def _extract_pscp_m3u8_formats(self, m3u8_url, video_id, format_id, state, width, height, fatal=True):
54 m3u8_formats = self._extract_m3u8_formats(
55 m3u8_url, video_id, 'mp4',
56 entry_protocol='m3u8_native'
57 if state in ('ended', 'timed_out') else 'm3u8',
58 m3u8_id=format_id, fatal=fatal, headers=self._M3U8_HEADERS)
59 if len(m3u8_formats) == 1:
60 self._add_width_and_height(m3u8_formats[0], width, height)
61 for f in m3u8_formats:
62 f.setdefault('http_headers', {}).update(self._M3U8_HEADERS)
63 return m3u8_formats
64
65
66 class PeriscopeIE(PeriscopeBaseIE):
67 IE_DESC = 'Periscope'
68 IE_NAME = 'periscope'
69 _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)'
70 # Alive example URLs can be found here https://www.periscope.tv/
71 _TESTS = [{
72 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
73 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
74 'info_dict': {
75 'id': '56102209',
76 'ext': 'mp4',
77 'title': 'Bec Boop - ๐Ÿš โœˆ๏ธ๐Ÿ‡ฌ๐Ÿ‡ง Fly above #London in Emirates Air Line cable car at night ๐Ÿ‡ฌ๐Ÿ‡งโœˆ๏ธ๐Ÿš  #BoopScope ๐ŸŽ€๐Ÿ’—',
78 'timestamp': 1438978559,
79 'upload_date': '20150807',
80 'uploader': 'Bec Boop',
81 'uploader_id': '1465763',
82 },
83 'skip': 'Expires in 24 hours',
84 }, {
85 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
86 'only_matching': True,
87 }, {
88 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
89 'only_matching': True,
90 }, {
91 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
92 'only_matching': True,
93 }]
94
95 @staticmethod
96 def _extract_url(webpage):
97 mobj = re.search(
98 r'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage)
99 if mobj:
100 return mobj.group('url')
101
102 def _real_extract(self, url):
103 token = self._match_id(url)
104
105 stream = self._call_api(
106 'accessVideoPublic', {'broadcast_id': token}, token)
107
108 broadcast = stream['broadcast']
109 info = self._parse_broadcast_data(broadcast, token)
110
111 state = broadcast.get('state').lower()
112 width = int_or_none(broadcast.get('width'))
113 height = int_or_none(broadcast.get('height'))
114
115 def add_width_and_height(f):
116 for key, val in (('width', width), ('height', height)):
117 if not f.get(key):
118 f[key] = val
119
120 video_urls = set()
121 formats = []
122 for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'):
123 video_url = stream.get(format_id + '_url')
124 if not video_url or video_url in video_urls:
125 continue
126 video_urls.add(video_url)
127 if format_id != 'rtmp':
128 m3u8_formats = self._extract_pscp_m3u8_formats(
129 video_url, token, format_id, state, width, height, False)
130 formats.extend(m3u8_formats)
131 continue
132 rtmp_format = {
133 'url': video_url,
134 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
135 }
136 self._add_width_and_height(rtmp_format)
137 formats.append(rtmp_format)
138 self._sort_formats(formats)
139
140 info['formats'] = formats
141 return info
142
143
144 class PeriscopeUserIE(PeriscopeBaseIE):
145 _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$'
146 IE_DESC = 'Periscope user videos'
147 IE_NAME = 'periscope:user'
148
149 _TEST = {
150 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
151 'info_dict': {
152 'id': 'LularoeHusbandMike',
153 'title': 'LULAROE HUSBAND MIKE',
154 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
155 },
156 # Periscope only shows videos in the last 24 hours, so it's possible to
157 # get 0 videos
158 'playlist_mincount': 0,
159 }
160
161 def _real_extract(self, url):
162 user_name = self._match_id(url)
163
164 webpage = self._download_webpage(url, user_name)
165
166 data_store = self._parse_json(
167 unescapeHTML(self._search_regex(
168 r'data-store=(["\'])(?P<data>.+?)\1',
169 webpage, 'data store', default='{}', group='data')),
170 user_name)
171
172 user = list(data_store['UserCache']['users'].values())[0]['user']
173 user_id = user['id']
174 session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id']
175
176 broadcasts = self._call_api(
177 'getUserBroadcastsPublic',
178 {'user_id': user_id, 'session_id': session_id},
179 user_name)['broadcasts']
180
181 broadcast_ids = [
182 broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
183
184 title = user.get('display_name') or user.get('username') or user_name
185 description = user.get('description')
186
187 entries = [
188 self.url_result(
189 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
190 for broadcast_id in broadcast_ids]
191
192 return self.playlist_result(entries, user_id, title, description)