]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/periscope.py
2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
14 class PeriscopeBaseIE(InfoExtractor
):
16 'Referer': 'https://www.periscope.tv/'
19 def _call_api(self
, method
, query
, item_id
):
20 return self
._download
_json
(
21 'https://api.periscope.tv/api/v2/%s' % method
,
24 def _parse_broadcast_data(self
, broadcast
, video_id
):
25 title
= broadcast
.get('status') or 'Periscope Broadcast'
26 uploader
= broadcast
.get('user_display_name') or broadcast
.get('username')
27 title
= '%s - %s' % (uploader
, title
) if uploader
else title
28 is_live
= broadcast
.get('state').lower() == 'running'
31 'url': broadcast
[image
],
32 } for image
in ('image_url', 'image_url_small') if broadcast
.get(image
)]
35 'id': broadcast
.get('id') or video_id
,
36 'title': self
._live
_title
(title
) if is_live
else title
,
37 'timestamp': parse_iso8601(broadcast
.get('created_at')),
39 'uploader_id': broadcast
.get('user_id') or broadcast
.get('username'),
40 'thumbnails': thumbnails
,
41 'view_count': int_or_none(broadcast
.get('total_watched')),
42 'tags': broadcast
.get('tags'),
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'))
51 def _add_width_and_height(f
, width
, height
):
52 for key
, val
in (('width', width
), ('height', height
)):
56 def _extract_pscp_m3u8_formats(self
, m3u8_url
, video_id
, format_id
, state
, width
, height
, fatal
=True):
57 m3u8_formats
= self
._extract
_m
3u8_formats
(
58 m3u8_url
, video_id
, 'mp4',
59 entry_protocol
='m3u8_native'
60 if state
in ('ended', 'timed_out') else 'm3u8',
61 m3u8_id
=format_id
, fatal
=fatal
, headers
=self
._M
3U8_HEADERS
)
62 if len(m3u8_formats
) == 1:
63 self
._add
_width
_and
_height
(m3u8_formats
[0], width
, height
)
64 for f
in m3u8_formats
:
65 f
.setdefault('http_headers', {}).update(self
._M
3U8_HEADERS
)
69 class PeriscopeIE(PeriscopeBaseIE
):
72 _VALID_URL
= r
'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)'
73 # Alive example URLs can be found here https://www.periscope.tv/
75 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
76 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
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',
86 'skip': 'Expires in 24 hours',
88 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
89 'only_matching': True,
91 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
92 'only_matching': True,
94 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
95 'only_matching': True,
99 def _extract_url(webpage
):
101 r
'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage)
103 return mobj.group('url')
105 def _real_extract(self, url):
106 token = self._match_id(url)
108 stream = self._call_api(
109 'accessVideoPublic', {'broadcast_id': token}, token)
111 broadcast = stream['broadcast']
112 info = self._parse_broadcast_data(broadcast, token)
114 state = broadcast.get('state').lower()
115 width = int_or_none(broadcast.get('width'))
116 height = int_or_none(broadcast.get('height'))
118 def add_width_and_height(f):
119 for key, val in (('width', width), ('height', height)):
125 for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'):
126 video_url = stream.get(format_id + '_url')
127 if not video_url or video_url in video_urls:
129 video_urls.add(video_url)
130 if format_id != 'rtmp':
131 m3u8_formats = self._extract_pscp_m3u8_formats(
132 video_url, token, format_id, state, width, height, False)
133 formats.extend(m3u8_formats)
137 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
139 self._add_width_and_height(rtmp_format)
140 formats.append(rtmp_format)
141 self._sort_formats(formats)
143 info['formats'] = formats
147 class PeriscopeUserIE(PeriscopeBaseIE):
148 _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$'
149 IE_DESC = 'Periscope user videos'
150 IE_NAME = 'periscope:user'
153 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
155 'id': 'LularoeHusbandMike',
156 'title': 'LULAROE HUSBAND MIKE',
157 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
159 # Periscope only shows videos in the last 24 hours, so it's possible to
161 'playlist_mincount': 0,
164 def _real_extract(self, url):
165 user_name = self._match_id(url)
167 webpage = self._download_webpage(url, user_name)
169 data_store = self._parse_json(
170 unescapeHTML(self._search_regex(
171 r'data-store=(["\'])(?P
<data
>.+?
)\
1',
172 webpage, 'data store
', default='{}', group='data
')),
175 user = list(data_store['UserCache
']['users
'].values())[0]['user
']
177 session_id = data_store['SessionToken
']['public
']['broadcastHistory
']['token
']['session_id
']
179 broadcasts = self._call_api(
180 'getUserBroadcastsPublic
',
181 {'user_id': user_id, 'session_id': session_id},
182 user_name)['broadcasts
']
185 broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
187 title = user.get('display_name
') or user.get('username
') or user_name
188 description = user.get('description
')
192 'https
://www
.periscope
.tv
/%s/%s' % (user_name, broadcast_id))
193 for broadcast_id in broadcast_ids]
195 return self.playlist_result(entries, user_id, title, description)