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