]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/periscope.py
[quickscope] Add extractor
[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, token):
31         return self._download_json(
32             'https://api.periscope.tv/api/v2/%s?token=%s' % (method, token), token)
33
34     def _real_extract(self, url):
35         token = self._match_id(url)
36
37         broadcast_data = self._call_api('getBroadcastPublic', token)
38         broadcast = broadcast_data['broadcast']
39         status = broadcast['status']
40
41         uploader = broadcast.get('user_display_name') or broadcast_data.get('user', {}).get('display_name')
42         uploader_id = broadcast.get('user_id') or broadcast_data.get('user', {}).get('id')
43
44         title = '%s - %s' % (uploader, status) if uploader else status
45         state = broadcast.get('state').lower()
46         if state == 'running':
47             title = self._live_title(title)
48         timestamp = parse_iso8601(broadcast.get('created_at'))
49
50         thumbnails = [{
51             'url': broadcast[image],
52         } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
53
54         stream = self._call_api('getAccessPublic', token)
55
56         formats = []
57         for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
58             video_url = stream.get(format_id + '_url')
59             if not video_url:
60                 continue
61             f = {
62                 'url': video_url,
63                 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
64             }
65             if format_id != 'rtmp':
66                 f['protocol'] = 'm3u8_native' if state == 'ended' else 'm3u8'
67             formats.append(f)
68         self._sort_formats(formats)
69
70         return {
71             'id': broadcast.get('id') or token,
72             'title': title,
73             'timestamp': timestamp,
74             'uploader': uploader,
75             'uploader_id': uploader_id,
76             'thumbnails': thumbnails,
77             'formats': formats,
78         }
79
80
81 class QuickscopeIE(InfoExtractor):
82     IE_DESC = 'Quisck Scope'
83     _VALID_URL = r'https?://watchonperiscope\.com/broadcast/(?P<id>\d+)'
84     _TEST = {
85         'url': 'https://watchonperiscope.com/broadcast/56180087',
86         'only_matching': True,
87     }
88
89     def _real_extract(self, url):
90         broadcast_id = self._match_id(url)
91         request = compat_urllib_request.Request(
92             'https://watchonperiscope.com/api/accessChannel', compat_urllib_parse.urlencode({
93                 'broadcast_id': broadcast_id,
94                 'entry_ticket': '',
95                 'from_push': 'false',
96                 'uses_sessions': 'true',
97             }).encode('utf-8'))
98         return self.url_result(
99             self._download_json(request, broadcast_id)['share_url'], 'Periscope')