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