]> RaphaΓ«l G. Git Repositories - youtubedl/blob - youtube_dl/extractor/periscope.py
Register new version in the changelog.
[youtubedl] / 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 # Alive example URLs can be found here http://onperiscope.com/
16 _TESTS = [{
17 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
18 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
19 'info_dict': {
20 'id': '56102209',
21 'ext': 'mp4',
22 'title': 'Bec Boop - πŸš βœˆοΈπŸ‡¬πŸ‡§ Fly above #London in Emirates Air Line cable car at night πŸ‡¬πŸ‡§βœˆοΈπŸš  #BoopScope πŸŽ€πŸ’—',
23 'timestamp': 1438978559,
24 'upload_date': '20150807',
25 'uploader': 'Bec Boop',
26 'uploader_id': '1465763',
27 },
28 'skip': 'Expires in 24 hours',
29 }, {
30 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
31 'only_matching': True,
32 }]
33
34 def _call_api(self, method, value):
35 attribute = 'token' if len(value) > 13 else 'broadcast_id'
36 return self._download_json(
37 'https://api.periscope.tv/api/v2/%s?%s=%s' % (method, attribute, value), value)
38
39 def _real_extract(self, url):
40 token = self._match_id(url)
41
42 broadcast_data = self._call_api('getBroadcastPublic', token)
43 broadcast = broadcast_data['broadcast']
44 status = broadcast['status']
45
46 uploader = broadcast.get('user_display_name') or broadcast_data.get('user', {}).get('display_name')
47 uploader_id = broadcast.get('user_id') or broadcast_data.get('user', {}).get('id')
48
49 title = '%s - %s' % (uploader, status) if uploader else status
50 state = broadcast.get('state').lower()
51 if state == 'running':
52 title = self._live_title(title)
53 timestamp = parse_iso8601(broadcast.get('created_at'))
54
55 thumbnails = [{
56 'url': broadcast[image],
57 } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
58
59 stream = self._call_api('getAccessPublic', token)
60
61 formats = []
62 for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
63 video_url = stream.get(format_id + '_url')
64 if not video_url:
65 continue
66 f = {
67 'url': video_url,
68 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
69 }
70 if format_id != 'rtmp':
71 f['protocol'] = 'm3u8_native' if state == 'ended' else 'm3u8'
72 formats.append(f)
73 self._sort_formats(formats)
74
75 return {
76 'id': broadcast.get('id') or token,
77 'title': title,
78 'timestamp': timestamp,
79 'uploader': uploader,
80 'uploader_id': uploader_id,
81 'thumbnails': thumbnails,
82 'formats': formats,
83 }
84
85
86 class QuickscopeIE(InfoExtractor):
87 IE_DESC = 'Quick Scope'
88 _VALID_URL = r'https?://watchonperiscope\.com/broadcast/(?P<id>\d+)'
89 _TEST = {
90 'url': 'https://watchonperiscope.com/broadcast/56180087',
91 'only_matching': True,
92 }
93
94 def _real_extract(self, url):
95 broadcast_id = self._match_id(url)
96 request = compat_urllib_request.Request(
97 'https://watchonperiscope.com/api/accessChannel', compat_urllib_parse.urlencode({
98 'broadcast_id': broadcast_id,
99 'entry_ticket': '',
100 'from_push': 'false',
101 'uses_sessions': 'true',
102 }).encode('utf-8'))
103 return self.url_result(
104 self._download_json(request, broadcast_id)['share_url'], 'Periscope')