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