[periscope] Fix untitled broadcasts (#25482)
[youtube-dl] / youtube_dl / extractor / periscope.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     int_or_none,
9     parse_iso8601,
10     unescapeHTML,
11 )
12
13
14 class 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
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 = '%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
62
63 class PeriscopeIE(PeriscopeBaseIE):
64     IE_DESC = 'Periscope'
65     IE_NAME = 'periscope'
66     _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)'
67     # Alive example URLs can be found here https://www.periscope.tv/
68     _TESTS = [{
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',
81     }, {
82         'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
83         'only_matching': True,
84     }, {
85         'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
86         'only_matching': True,
87     }, {
88         'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
89         'only_matching': True,
90     }]
91
92     @staticmethod
93     def _extract_url(webpage):
94         mobj = re.search(
95             r'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage)
96         if mobj:
97             return mobj.group('url')
98
99     def _real_extract(self, url):
100         token = self._match_id(url)
101
102         stream = self._call_api(
103             'accessVideoPublic', {'broadcast_id': token}, token)
104
105         broadcast = stream['broadcast']
106         info = self._parse_broadcast_data(broadcast, token)
107
108         state = broadcast.get('state').lower()
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
117         video_urls = set()
118         formats = []
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:
122                 continue
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)
128                 continue
129             rtmp_format = {
130                 'url': video_url,
131                 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
132             }
133             self._add_width_and_height(rtmp_format)
134             formats.append(rtmp_format)
135         self._sort_formats(formats)
136
137         info['formats'] = formats
138         return info
139
140
141 class PeriscopeUserIE(PeriscopeBaseIE):
142     _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$'
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',
151             'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
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):
159         user_name = self._match_id(url)
160
161         webpage = self._download_webpage(url, user_name)
162
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')),
167             user_name)
168
169         user = list(data_store['UserCache']['users'].values())[0]['user']
170         user_id = user['id']
171         session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id']
172
173         broadcasts = self._call_api(
174             'getUserBroadcastsPublic',
175             {'user_id': user_id, 'session_id': session_id},
176             user_name)['broadcasts']
177
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')
183
184         entries = [
185             self.url_result(
186                 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
187             for broadcast_id in broadcast_ids]
188
189         return self.playlist_result(entries, user_id, title, description)