1 from .common
import InfoExtractor
7 from ..utils
.traversal
import traverse_obj
10 class PeriscopeBaseIE(InfoExtractor
):
12 'Referer': 'https://www.periscope.tv/',
15 def _call_api(self
, method
, query
, item_id
):
16 return self
._download
_json
(
17 f
'https://api.periscope.tv/api/v2/{method}',
20 def _parse_broadcast_data(self
, broadcast
, video_id
):
21 title
= broadcast
.get('status') or 'Periscope Broadcast'
22 uploader
= broadcast
.get('user_display_name') or broadcast
.get('username')
23 title
= f
'{uploader} - {title}' if uploader
else title
25 'url': broadcast
[image
],
26 } for image
in ('image_url', 'image_url_medium', 'image_url_small') if broadcast
.get(image
)]
29 'id': broadcast
.get('id') or video_id
,
31 'timestamp': parse_iso8601(broadcast
.get('created_at')) or int_or_none(
32 broadcast
.get('created_at_ms'), scale
=1000),
33 'release_timestamp': int_or_none(broadcast
.get('scheduled_start_ms'), scale
=1000),
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 'concurrent_view_count': int_or_none(broadcast
.get('total_watching')),
39 'tags': broadcast
.get('tags'),
42 'not_started': 'is_upcoming',
43 }.get(traverse_obj(broadcast
, ('state', {str.lower
}))) or 'was_live',
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 _EMBED_REGEX
= [r
'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1']
74 # Alive example URLs can be found here https://www.periscope.tv/
76 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
77 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
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',
87 'skip': 'Expires in 24 hours',
89 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
90 'only_matching': True,
92 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
93 'only_matching': True,
95 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
96 'only_matching': True,
99 def _real_extract(self, url):
100 token = self._match_id(url)
102 stream = self._call_api(
103 'accessVideoPublic', {'broadcast_id': token}, token)
105 broadcast = stream['broadcast']
106 info = self._parse_broadcast_data(broadcast, token)
108 state = broadcast.get('state').lower()
109 width = int_or_none(broadcast.get('width'))
110 height = int_or_none(broadcast.get('height'))
112 def add_width_and_height(f):
113 for key, val in (('width', width), ('height', height)):
119 for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'):
120 video_url = stream.get(format_id + '_url')
121 if not video_url or video_url in video_urls:
123 video_urls.add(video_url)
124 if format_id != 'rtmp':
125 m3u8_formats = self._extract_pscp_m3u8_formats(
126 video_url, token, format_id, state, width, height, False)
127 formats.extend(m3u8_formats)
131 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
133 self._add_width_and_height(rtmp_format)
134 formats.append(rtmp_format)
136 info['formats'] = formats
140 class PeriscopeUserIE(PeriscopeBaseIE):
141 _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$'
142 IE_DESC = 'Periscope user videos'
143 IE_NAME = 'periscope:user'
146 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
148 'id': 'LularoeHusbandMike',
149 'title': 'LULAROE HUSBAND MIKE',
150 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
152 # Periscope only shows videos in the last 24 hours, so it's possible to
154 'playlist_mincount': 0,
157 def _real_extract(self, url):
158 user_name = self._match_id(url)
160 webpage = self._download_webpage(url, user_name)
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
')),
168 user = next(iter(data_store['UserCache
']['users
'].values()))['user
']
170 session_id = data_store['SessionToken
']['public
']['broadcastHistory
']['token
']['session_id
']
172 broadcasts = self._call_api(
173 'getUserBroadcastsPublic
',
174 {'user_id
': user_id, 'session_id
': session_id},
175 user_name)['broadcasts
']
178 broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
180 title = user.get('display_name
') or user.get('username
') or user_name
181 description = user.get('description
')
185 f'https
://www
.periscope
.tv
/{user_name}
/{broadcast_id}
')
186 for broadcast_id in broadcast_ids]
188 return self.playlist_result(entries, user_id, title, description)