[periscope] Add support for videos with broadcast_id (Closes #7359)
[youtube-dl] / youtube_dl / extractor / periscope.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_urllib_parse,
7     compat_urllib_request,
8 )
9 from ..utils import parse_iso8601
10
11
12 class PeriscopeIE(InfoExtractor):
13     IE_DESC = 'Periscope'
14     _VALID_URL = r'https?://(?:www\.)?periscope\.tv/w/(?P<id>[^/?#]+)'
15     _TEST = {
16         'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
17         'md5': '65b57957972e503fcbbaeed8f4fa04ca',
18         'info_dict': {
19             'id': '56102209',
20             'ext': 'mp4',
21             'title': 'Bec Boop - ๐Ÿš โœˆ๏ธ๐Ÿ‡ฌ๐Ÿ‡ง Fly above #London in Emirates Air Line cable car at night ๐Ÿ‡ฌ๐Ÿ‡งโœˆ๏ธ๐Ÿš  #BoopScope ๐ŸŽ€๐Ÿ’—',
22             'timestamp': 1438978559,
23             'upload_date': '20150807',
24             'uploader': 'Bec Boop',
25             'uploader_id': '1465763',
26         },
27         'skip': 'Expires in 24 hours',
28     }
29
30     def _call_api(self, method, value):
31         attribute = 'token' if len(value) > 13 else 'broadcast_id'
32         return self._download_json(
33             'https://api.periscope.tv/api/v2/%s?%s=%s' % (method, attribute, value), value)
34
35     def _real_extract(self, url):
36         token = self._match_id(url)
37
38         broadcast_data = self._call_api('getBroadcastPublic', token)
39         broadcast = broadcast_data['broadcast']
40         status = broadcast['status']
41
42         uploader = broadcast.get('user_display_name') or broadcast_data.get('user', {}).get('display_name')
43         uploader_id = broadcast.get('user_id') or broadcast_data.get('user', {}).get('id')
44
45         title = '%s - %s' % (uploader, status) if uploader else status
46         state = broadcast.get('state').lower()
47         if state == 'running':
48             title = self._live_title(title)
49         timestamp = parse_iso8601(broadcast.get('created_at'))
50
51         thumbnails = [{
52             'url': broadcast[image],
53         } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
54
55         stream = self._call_api('getAccessPublic', token)
56
57         formats = []
58         for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
59             video_url = stream.get(format_id + '_url')
60             if not video_url:
61                 continue
62             f = {
63                 'url': video_url,
64                 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
65             }
66             if format_id != 'rtmp':
67                 f['protocol'] = 'm3u8_native' if state == 'ended' else 'm3u8'
68             formats.append(f)
69         self._sort_formats(formats)
70
71         return {
72             'id': broadcast.get('id') or token,
73             'title': title,
74             'timestamp': timestamp,
75             'uploader': uploader,
76             'uploader_id': uploader_id,
77             'thumbnails': thumbnails,
78             'formats': formats,
79         }
80
81
82 class QuickscopeIE(InfoExtractor):
83     IE_DESC = 'Quick Scope'
84     _VALID_URL = r'https?://watchonperiscope\.com/broadcast/(?P<id>\d+)'
85     _TEST = {
86         'url': 'https://watchonperiscope.com/broadcast/56180087',
87         'only_matching': True,
88     }
89
90     def _real_extract(self, url):
91         broadcast_id = self._match_id(url)
92         request = compat_urllib_request.Request(
93             'https://watchonperiscope.com/api/accessChannel', compat_urllib_parse.urlencode({
94                 'broadcast_id': broadcast_id,
95                 'entry_ticket': '',
96                 'from_push': 'false',
97                 'uses_sessions': 'true',
98             }).encode('utf-8'))
99         return self.url_result(
100             self._download_json(request, broadcast_id)['share_url'], 'Periscope')