]> RaphaΓ«l G. Git Repositories - youtubedl/blob - youtube_dl/extractor/periscope.py
c23b314e79df70e6b115fee6fb91386345d00ad4
[youtubedl] / youtube_dl / extractor / periscope.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6 parse_iso8601,
7 unescapeHTML,
8 )
9
10
11 class PeriscopeIE(InfoExtractor):
12 IE_DESC = 'Periscope'
13 IE_NAME = 'periscope'
14 _VALID_URL = r'https?://(?:www\.)?periscope\.tv/[^/]+/(?P<id>[^/?#]+)'
15 # Alive example URLs can be found here http://onperiscope.com/
16 _TESTS = [{
17 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
18 'md5': '65b57957972e503fcbbaeed8f4fa04ca',
19 'info_dict': {
20 'id': '56102209',
21 'ext': 'mp4',
22 'title': 'Bec Boop - πŸš βœˆοΈπŸ‡¬πŸ‡§ Fly above #London in Emirates Air Line cable car at night πŸ‡¬πŸ‡§βœˆοΈπŸš  #BoopScope πŸŽ€πŸ’—',
23 'timestamp': 1438978559,
24 'upload_date': '20150807',
25 'uploader': 'Bec Boop',
26 'uploader_id': '1465763',
27 },
28 'skip': 'Expires in 24 hours',
29 }, {
30 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv',
31 'only_matching': True,
32 }, {
33 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX',
34 'only_matching': True,
35 }]
36
37 def _call_api(self, method, value):
38 return self._download_json(
39 'https://api.periscope.tv/api/v2/%s?broadcast_id=%s' % (method, value), value)
40
41 def _real_extract(self, url):
42 token = self._match_id(url)
43
44 broadcast_data = self._call_api('getBroadcastPublic', token)
45 broadcast = broadcast_data['broadcast']
46 status = broadcast['status']
47
48 user = broadcast_data.get('user', {})
49
50 uploader = broadcast.get('user_display_name') or user.get('display_name')
51 uploader_id = (broadcast.get('username') or user.get('username') or
52 broadcast.get('user_id') or user.get('id'))
53
54 title = '%s - %s' % (uploader, status) if uploader else status
55 state = broadcast.get('state').lower()
56 if state == 'running':
57 title = self._live_title(title)
58 timestamp = parse_iso8601(broadcast.get('created_at'))
59
60 thumbnails = [{
61 'url': broadcast[image],
62 } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
63
64 stream = self._call_api('getAccessPublic', token)
65
66 formats = []
67 for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
68 video_url = stream.get(format_id + '_url')
69 if not video_url:
70 continue
71 f = {
72 'url': video_url,
73 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
74 }
75 if format_id != 'rtmp':
76 f['protocol'] = 'm3u8_native' if state == 'ended' else 'm3u8'
77 formats.append(f)
78 self._sort_formats(formats)
79
80 return {
81 'id': broadcast.get('id') or token,
82 'title': title,
83 'timestamp': timestamp,
84 'uploader': uploader,
85 'uploader_id': uploader_id,
86 'thumbnails': thumbnails,
87 'formats': formats,
88 }
89
90
91 class PeriscopeUserIE(InfoExtractor):
92 _VALID_URL = r'https?://www\.periscope\.tv/(?P<id>[^/]+)/?$'
93 IE_DESC = 'Periscope user videos'
94 IE_NAME = 'periscope:user'
95
96 _TEST = {
97 'url': 'https://www.periscope.tv/LularoeHusbandMike/',
98 'info_dict': {
99 'id': 'LularoeHusbandMike',
100 'title': 'LULAROE HUSBAND MIKE',
101 'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
102 },
103 # Periscope only shows videos in the last 24 hours, so it's possible to
104 # get 0 videos
105 'playlist_mincount': 0,
106 }
107
108 def _real_extract(self, url):
109 user_id = self._match_id(url)
110
111 webpage = self._download_webpage(url, user_id)
112
113 data_store = self._parse_json(
114 unescapeHTML(self._search_regex(
115 r'data-store=(["\'])(?P<data>.+?)\1',
116 webpage, 'data store', default='{}', group='data')),
117 user_id)
118
119 user = data_store.get('User', {}).get('user', {})
120 title = user.get('display_name') or user.get('username')
121 description = user.get('description')
122
123 entries = [
124 self.url_result(
125 'https://www.periscope.tv/%s/%s' % (user_id, broadcast['id']))
126 for broadcast in data_store.get('UserBroadcastHistory', {}).get('broadcasts', [])]
127
128 return self.playlist_result(entries, user_id, title, description)