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