]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/periscope.py
3 from .common
import InfoExtractor
11 class PeriscopeBaseIE(InfoExtractor
):
13 'Referer': 'https://www.periscope.tv/'
16 def _call_api(self
, method
, query
, item_id
):
17 return self
._download
_json
(
18 'https://api.periscope.tv/api/v2/%s' % method
,
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'
28 'url': broadcast
[image
],
29 } for image
in ('image_url', 'image_url_small') if broadcast
.get(image
)]
32 'id': broadcast
.get('id') or video_id
,
34 'timestamp': parse_iso8601(broadcast
.get('created_at')),
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'),
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'))
48 def _add_width_and_height(f
, width
, height
):
49 for key
, val
in (('width', width
), ('height', height
)):
53 def _extract_pscp_m3u8_formats(self
, m3u8_url
, video_id
, format_id
, state
, width
, height
, fatal
=True):
54 m3u8_formats
= self
._extract
_m
3u8_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
._M
3U8_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
._M
3U8_HEADERS
)
66 class PeriscopeIE(PeriscopeBaseIE
):
69 _VALID_URL
= r
'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)'
70 # Alive example URLs can be found here https://www.periscope.tv/
72 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
73 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
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',
83 'skip': 'Expires in 24 hours',
85 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
86 'only_matching': True,
88 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
89 'only_matching': True,
91 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
92 'only_matching': True,
96 def _extract_url(webpage
):
98 r
'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage)
100 return mobj.group('url')
102 def _real_extract(self, url):
103 token = self._match_id(url)
105 stream = self._call_api(
106 'accessVideoPublic', {'broadcast_id': token}, token)
108 broadcast = stream['broadcast']
109 info = self._parse_broadcast_data(broadcast, token)
111 state = broadcast.get('state').lower()
112 width = int_or_none(broadcast.get('width'))
113 height = int_or_none(broadcast.get('height'))
115 def add_width_and_height(f):
116 for key, val in (('width', width), ('height', height)):
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:
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)
134 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
136 self._add_width_and_height(rtmp_format)
137 formats.append(rtmp_format)
138 self._sort_formats(formats)
140 info['formats'] = formats
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'
150 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
152 'id': 'LularoeHusbandMike',
153 'title': 'LULAROE HUSBAND MIKE',
154 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
156 # Periscope only shows videos in the last 24 hours, so it's possible to
158 'playlist_mincount': 0,
161 def _real_extract(self, url):
162 user_name = self._match_id(url)
164 webpage = self._download_webpage(url, user_name)
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
')),
172 user = list(data_store['UserCache
']['users
'].values())[0]['user
']
174 session_id = data_store['SessionToken
']['public
']['broadcastHistory
']['token
']['session_id
']
176 broadcasts = self._call_api(
177 'getUserBroadcastsPublic
',
178 {'user_id': user_id, 'session_id': session_id},
179 user_name)['broadcasts
']
182 broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
184 title = user.get('display_name
') or user.get('username
') or user_name
185 description = user.get('description
')
189 'https
://www
.periscope
.tv
/%s/%s' % (user_name, broadcast_id))
190 for broadcast_id in broadcast_ids]
192 return self.playlist_result(entries, user_id, title, description)