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