]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/periscope.py
[extractor/generic] Add support for redtube embds (closes #11099)
[yt-dlp.git] / youtube_dl / extractor / periscope.py
CommitLineData
3550821f
S
1# coding: utf-8
2from __future__ import unicode_literals
3
f0bc5a86
YCH
4import re
5
3550821f 6from .common import InfoExtractor
0db9a05f
S
7from ..utils import (
8 parse_iso8601,
9 unescapeHTML,
10)
3550821f
S
11
12
92c27a0d
S
13class 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
20class PeriscopeIE(PeriscopeBaseIE):
1e83741c 21 IE_DESC = 'Periscope'
6f59aa93 22 IE_NAME = 'periscope'
0c59d02b 23 _VALID_URL = r'https?://(?:www\.)?periscope\.tv/[^/]+/(?P<id>[^/?#]+)'
53472df8 24 # Alive example URLs can be found here http://onperiscope.com/
2549e113 25 _TESTS = [{
3550821f
S
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',
2549e113
S
38 }, {
39 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
40 'only_matching': True,
0c59d02b
S
41 }, {
42 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
43 'only_matching': True,
2549e113 44 }]
3550821f 45
f0bc5a86
YCH
46 @staticmethod
47 def _extract_url(webpage):
48 mobj = re.search(
49 r'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?periscope\.tv/(?:(?!\1).)+)\1', webpage)
50 if mobj:
51 return mobj.group('url')
52
621d6a95
S
53 def _real_extract(self, url):
54 token = self._match_id(url)
3550821f 55
92c27a0d
S
56 broadcast_data = self._call_api(
57 'getBroadcastPublic', {'broadcast_id': token}, token)
3550821f
S
58 broadcast = broadcast_data['broadcast']
59 status = broadcast['status']
60
92d221ad
S
61 user = broadcast_data.get('user', {})
62
63 uploader = broadcast.get('user_display_name') or user.get('display_name')
64 uploader_id = (broadcast.get('username') or user.get('username') or
65 broadcast.get('user_id') or user.get('id'))
3550821f
S
66
67 title = '%s - %s' % (uploader, status) if uploader else status
1e83741c
S
68 state = broadcast.get('state').lower()
69 if state == 'running':
70 title = self._live_title(title)
3550821f
S
71 timestamp = parse_iso8601(broadcast.get('created_at'))
72
73 thumbnails = [{
74 'url': broadcast[image],
75 } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
76
92c27a0d
S
77 stream = self._call_api(
78 'getAccessPublic', {'broadcast_id': token}, token)
1e83741c
S
79
80 formats = []
81 for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
82 video_url = stream.get(format_id + '_url')
83 if not video_url:
84 continue
85 f = {
86 'url': video_url,
87 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
88 }
89 if format_id != 'rtmp':
1a2fbe32 90 f['protocol'] = 'm3u8_native' if state in ('ended', 'timed_out') else 'm3u8'
1e83741c
S
91 formats.append(f)
92 self._sort_formats(formats)
93
3550821f 94 return {
621d6a95 95 'id': broadcast.get('id') or token,
3550821f
S
96 'title': title,
97 'timestamp': timestamp,
98 'uploader': uploader,
99 'uploader_id': uploader_id,
100 'thumbnails': thumbnails,
1e83741c 101 'formats': formats,
3550821f 102 }
6f59aa93
YCH
103
104
92c27a0d 105class PeriscopeUserIE(PeriscopeBaseIE):
92519402 106 _VALID_URL = r'https?://(?:www\.)?periscope\.tv/(?P<id>[^/]+)/?$'
6f59aa93
YCH
107 IE_DESC = 'Periscope user videos'
108 IE_NAME = 'periscope:user'
109
110 _TEST = {
111 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
112 'info_dict': {
113 'id': 'LularoeHusbandMike',
114 'title': 'LULAROE HUSBAND MIKE',
0db9a05f 115 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
6f59aa93
YCH
116 },
117 # Periscope only shows videos in the last 24 hours, so it's possible to
118 # get 0 videos
119 'playlist_mincount': 0,
120 }
121
122 def _real_extract(self, url):
92c27a0d 123 user_name = self._match_id(url)
6f59aa93 124
92c27a0d 125 webpage = self._download_webpage(url, user_name)
6f59aa93 126
0db9a05f
S
127 data_store = self._parse_json(
128 unescapeHTML(self._search_regex(
129 r'data-store=(["\'])(?P<data>.+?)\1',
130 webpage, 'data store', default='{}', group='data')),
92c27a0d 131 user_name)
6f59aa93 132
92c27a0d
S
133 user = list(data_store['UserCache']['users'].values())[0]['user']
134 user_id = user['id']
e1e97c24 135 session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id']
92c27a0d
S
136
137 broadcasts = self._call_api(
138 'getUserBroadcastsPublic',
139 {'user_id': user_id, 'session_id': session_id},
140 user_name)['broadcasts']
0db9a05f 141
92c27a0d
S
142 broadcast_ids = [
143 broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
144
145 title = user.get('display_name') or user.get('username') or user_name
146 description = user.get('description')
35fc3021 147
6f59aa93
YCH
148 entries = [
149 self.url_result(
92c27a0d 150 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
35fc3021 151 for broadcast_id in broadcast_ids]
6f59aa93 152
0db9a05f 153 return self.playlist_result(entries, user_id, title, description)