]> RaphaΓ«l G. Git Repositories - youtubedl/blob - youtube_dl/extractor/periscope.py
Merge changes from buster branch for the NMU while I was away.
[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 stream = self._call_api(
60 'accessVideoPublic', {'broadcast_id': token}, token)
61
62 broadcast = stream['broadcast']
63 title = broadcast['status']
64
65 uploader = broadcast.get('user_display_name') or broadcast.get('username')
66 uploader_id = (broadcast.get('user_id') or broadcast.get('username'))
67
68 title = '%s - %s' % (uploader, title) if uploader else title
69 state = broadcast.get('state').lower()
70 if state == 'running':
71 title = self._live_title(title)
72 timestamp = parse_iso8601(broadcast.get('created_at'))
73
74 thumbnails = [{
75 'url': broadcast[image],
76 } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
77
78 video_urls = set()
79 formats = []
80 for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'):
81 video_url = stream.get(format_id + '_url')
82 if not video_url or video_url in video_urls:
83 continue
84 video_urls.add(video_url)
85 if format_id != 'rtmp':
86 formats.extend(self._extract_m3u8_formats(
87 video_url, token, 'mp4',
88 entry_protocol='m3u8_native'
89 if state in ('ended', 'timed_out') else 'm3u8',
90 m3u8_id=format_id, fatal=False))
91 continue
92 formats.append({
93 'url': video_url,
94 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
95 })
96 self._sort_formats(formats)
97
98 return {
99 'id': broadcast.get('id') or token,
100 'title': title,
101 'timestamp': timestamp,
102 'uploader': uploader,
103 'uploader_id': uploader_id,
104 'thumbnails': thumbnails,
105 'formats': formats,
106 }
107
108
109 class PeriscopeUserIE(PeriscopeBaseIE):
110 _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$'
111 IE_DESC = 'Periscope user videos'
112 IE_NAME = 'periscope:user'
113
114 _TEST = {
115 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
116 'info_dict': {
117 'id': 'LularoeHusbandMike',
118 'title': 'LULAROE HUSBAND MIKE',
119 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
120 },
121 # Periscope only shows videos in the last 24 hours, so it's possible to
122 # get 0 videos
123 'playlist_mincount': 0,
124 }
125
126 def _real_extract(self, url):
127 user_name = self._match_id(url)
128
129 webpage = self._download_webpage(url, user_name)
130
131 data_store = self._parse_json(
132 unescapeHTML(self._search_regex(
133 r'data-store=(["\'])(?P<data>.+?)\1',
134 webpage, 'data store', default='{}', group='data')),
135 user_name)
136
137 user = list(data_store['UserCache']['users'].values())[0]['user']
138 user_id = user['id']
139 session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id']
140
141 broadcasts = self._call_api(
142 'getUserBroadcastsPublic',
143 {'user_id': user_id, 'session_id': session_id},
144 user_name)['broadcasts']
145
146 broadcast_ids = [
147 broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
148
149 title = user.get('display_name') or user.get('username') or user_name
150 description = user.get('description')
151
152 entries = [
153 self.url_result(
154 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
155 for broadcast_id in broadcast_ids]
156
157 return self.playlist_result(entries, user_id, title, description)