]> RaphaΓ«l G. Git Repositories - youtubedl/blob - youtube_dl/extractor/periscope.py
debian/control: Mark compliance with policy 4.0.1.
[youtubedl] / 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 parse_iso8601,
9 unescapeHTML,
10 )
11
12
13 class PeriscopeBaseIE(InfoExtractor):
14 def _call_api(self, method, query, item_id):
15 return self._download_json(
16 'https://api.periscope.tv/api/v2/%s' % method,
17 item_id, query=query)
18
19
20 class PeriscopeIE(PeriscopeBaseIE):
21 IE_DESC = 'Periscope'
22 IE_NAME = 'periscope'
23 _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)'
24 # Alive example URLs can be found here http://onperiscope.com/
25 _TESTS = [{
26 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
27 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
28 'info_dict': {
29 'id': '56102209',
30 'ext': 'mp4',
31 'title': 'Bec Boop - πŸš βœˆοΈπŸ‡¬πŸ‡§ Fly above #London in Emirates Air Line cable car at night πŸ‡¬πŸ‡§βœˆοΈπŸš  #BoopScope πŸŽ€πŸ’—',
32 'timestamp': 1438978559,
33 'upload_date': '20150807',
34 'uploader': 'Bec Boop',
35 'uploader_id': '1465763',
36 },
37 'skip': 'Expires in 24 hours',
38 }, {
39 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
40 'only_matching': True,
41 }, {
42 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
43 'only_matching': True,
44 }, {
45 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
46 'only_matching': True,
47 }]
48
49 @staticmethod
50 def _extract_url(webpage):
51 mobj = re.search(
52 r'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage)
53 if mobj:
54 return mobj.group('url')
55
56 def _real_extract(self, url):
57 token = self._match_id(url)
58
59 broadcast_data = self._call_api(
60 'getBroadcastPublic', {'broadcast_id': token}, token)
61 broadcast = broadcast_data['broadcast']
62 status = broadcast['status']
63
64 user = broadcast_data.get('user', {})
65
66 uploader = broadcast.get('user_display_name') or user.get('display_name')
67 uploader_id = (broadcast.get('username') or user.get('username') or
68 broadcast.get('user_id') or user.get('id'))
69
70 title = '%s - %s' % (uploader, status) if uploader else status
71 state = broadcast.get('state').lower()
72 if state == 'running':
73 title = self._live_title(title)
74 timestamp = parse_iso8601(broadcast.get('created_at'))
75
76 thumbnails = [{
77 'url': broadcast[image],
78 } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
79
80 stream = self._call_api(
81 'getAccessPublic', {'broadcast_id': token}, token)
82
83 video_urls = set()
84 formats = []
85 for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'):
86 video_url = stream.get(format_id + '_url')
87 if not video_url or video_url in video_urls:
88 continue
89 video_urls.add(video_url)
90 if format_id != 'rtmp':
91 formats.extend(self._extract_m3u8_formats(
92 video_url, token, 'mp4',
93 entry_protocol='m3u8_native'
94 if state in ('ended', 'timed_out') else 'm3u8',
95 m3u8_id=format_id, fatal=False))
96 continue
97 formats.append({
98 'url': video_url,
99 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
100 })
101 self._sort_formats(formats)
102
103 return {
104 'id': broadcast.get('id') or token,
105 'title': title,
106 'timestamp': timestamp,
107 'uploader': uploader,
108 'uploader_id': uploader_id,
109 'thumbnails': thumbnails,
110 'formats': formats,
111 }
112
113
114 class PeriscopeUserIE(PeriscopeBaseIE):
115 _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$'
116 IE_DESC = 'Periscope user videos'
117 IE_NAME = 'periscope:user'
118
119 _TEST = {
120 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
121 'info_dict': {
122 'id': 'LularoeHusbandMike',
123 'title': 'LULAROE HUSBAND MIKE',
124 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
125 },
126 # Periscope only shows videos in the last 24 hours, so it's possible to
127 # get 0 videos
128 'playlist_mincount': 0,
129 }
130
131 def _real_extract(self, url):
132 user_name = self._match_id(url)
133
134 webpage = self._download_webpage(url, user_name)
135
136 data_store = self._parse_json(
137 unescapeHTML(self._search_regex(
138 r'data-store=(["\'])(?P<data>.+?)\1',
139 webpage, 'data store', default='{}', group='data')),
140 user_name)
141
142 user = list(data_store['UserCache']['users'].values())[0]['user']
143 user_id = user['id']
144 session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id']
145
146 broadcasts = self._call_api(
147 'getUserBroadcastsPublic',
148 {'user_id': user_id, 'session_id': session_id},
149 user_name)['broadcasts']
150
151 broadcast_ids = [
152 broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
153
154 title = user.get('display_name') or user.get('username') or user_name
155 description = user.get('description')
156
157 entries = [
158 self.url_result(
159 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
160 for broadcast_id in broadcast_ids]
161
162 return self.playlist_result(entries, user_id, title, description)