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