2 from __future__ import unicode_literals
4 from .common import InfoExtractor
11 class PeriscopeBaseIE(InfoExtractor):
12 def _call_api(self, method, query, item_id):
13 return self._download_json(
14 'https://api.periscope.tv/api/v2/%s' % method,
18 class PeriscopeIE(PeriscopeBaseIE):
21 _VALID_URL = r'https?://(?:www\.)?periscope\.tv/[^/]+/(?P<id>[^/?#]+)'
22 # Alive example URLs can be found here http://onperiscope.com/
24 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
25 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
29 'title': 'Bec Boop - ๐ โ๏ธ๐ฌ๐ง Fly above #London in Emirates Air Line cable car at night ๐ฌ๐งโ๏ธ๐ #BoopScope ๐๐',
30 'timestamp': 1438978559,
31 'upload_date': '20150807',
32 'uploader': 'Bec Boop',
33 'uploader_id': '1465763',
35 'skip': 'Expires in 24 hours',
37 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
38 'only_matching': True,
40 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
41 'only_matching': True,
44 def _real_extract(self, url):
45 token = self._match_id(url)
47 broadcast_data = self._call_api(
48 'getBroadcastPublic', {'broadcast_id': token}, token)
49 broadcast = broadcast_data['broadcast']
50 status = broadcast['status']
52 user = broadcast_data.get('user', {})
54 uploader = broadcast.get('user_display_name') or user.get('display_name')
55 uploader_id = (broadcast.get('username') or user.get('username') or
56 broadcast.get('user_id') or user.get('id'))
58 title = '%s - %s' % (uploader, status) if uploader else status
59 state = broadcast.get('state').lower()
60 if state == 'running':
61 title = self._live_title(title)
62 timestamp = parse_iso8601(broadcast.get('created_at'))
65 'url': broadcast[image],
66 } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
68 stream = self._call_api(
69 'getAccessPublic', {'broadcast_id': token}, token)
72 for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
73 video_url = stream.get(format_id + '_url')
78 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
80 if format_id != 'rtmp':
81 f['protocol'] = 'm3u8_native' if state == 'ended' else 'm3u8'
83 self._sort_formats(formats)
86 'id': broadcast.get('id') or token,
88 'timestamp': timestamp,
90 'uploader_id': uploader_id,
91 'thumbnails': thumbnails,
96 class PeriscopeUserIE(PeriscopeBaseIE):
97 _VALID_URL = r'https?://(?:www\.)?periscope\.tv/(?P<id>[^/]+)/?$'
98 IE_DESC = 'Periscope user videos'
99 IE_NAME = 'periscope:user'
102 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
104 'id': 'LularoeHusbandMike',
105 'title': 'LULAROE HUSBAND MIKE',
106 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
108 # Periscope only shows videos in the last 24 hours, so it's possible to
110 'playlist_mincount': 0,
113 def _real_extract(self, url):
114 user_name = self._match_id(url)
116 webpage = self._download_webpage(url, user_name)
118 data_store = self._parse_json(
119 unescapeHTML(self._search_regex(
120 r'data-store=(["\'])(?P<data>.+?)\1',
121 webpage, 'data store', default='{}', group='data')),
124 user = list(data_store['UserCache']['users'].values())[0]['user']
126 session_id = data_store['SessionToken']['broadcastHistory']['token']['session_id']
128 broadcasts = self._call_api(
129 'getUserBroadcastsPublic',
130 {'user_id': user_id, 'session_id': session_id},
131 user_name)['broadcasts']
134 broadcast['id'] for broadcast in broadcasts if broadcast.get('id')]
136 title = user.get('display_name') or user.get('username') or user_name
137 description = user.get('description')
141 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id))
142 for broadcast_id in broadcast_ids]
144 return self.playlist_result(entries, user_id, title, description)