[periscope:user] Adapt to layout changes (Closes #9563)
[youtube-dl] / 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         uploader = broadcast.get('user_display_name') or broadcast_data.get('user', {}).get('display_name')
49         uploader_id = broadcast.get('user_id') or broadcast_data.get('user', {}).get('id')
50
51         title = '%s - %s' % (uploader, status) if uploader else status
52         state = broadcast.get('state').lower()
53         if state == 'running':
54             title = self._live_title(title)
55         timestamp = parse_iso8601(broadcast.get('created_at'))
56
57         thumbnails = [{
58             'url': broadcast[image],
59         } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
60
61         stream = self._call_api('getAccessPublic', token)
62
63         formats = []
64         for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
65             video_url = stream.get(format_id + '_url')
66             if not video_url:
67                 continue
68             f = {
69                 'url': video_url,
70                 'ext': 'flv' if format_id == 'rtmp' else 'mp4',
71             }
72             if format_id != 'rtmp':
73                 f['protocol'] = 'm3u8_native' if state == 'ended' else 'm3u8'
74             formats.append(f)
75         self._sort_formats(formats)
76
77         return {
78             'id': broadcast.get('id') or token,
79             'title': title,
80             'timestamp': timestamp,
81             'uploader': uploader,
82             'uploader_id': uploader_id,
83             'thumbnails': thumbnails,
84             'formats': formats,
85         }
86
87
88 class PeriscopeUserIE(InfoExtractor):
89     _VALID_URL = r'https?://www\.periscope\.tv/(?P<id>[^/]+)/?$'
90     IE_DESC = 'Periscope user videos'
91     IE_NAME = 'periscope:user'
92
93     _TEST = {
94         'url': 'https://www.periscope.tv/LularoeHusbandMike/',
95         'info_dict': {
96             'id': 'LularoeHusbandMike',
97             'title': 'LULAROE HUSBAND MIKE',
98             'description': 'md5:6cf4ec8047768098da58e446e82c82f0',
99         },
100         # Periscope only shows videos in the last 24 hours, so it's possible to
101         # get 0 videos
102         'playlist_mincount': 0,
103     }
104
105     def _real_extract(self, url):
106         user_id = self._match_id(url)
107
108         webpage = self._download_webpage(url, user_id)
109
110         data_store = self._parse_json(
111             unescapeHTML(self._search_regex(
112                 r'data-store=(["\'])(?P<data>.+?)\1',
113                 webpage, 'data store', default='{}', group='data')),
114             user_id)
115
116         user = data_store.get('User', {}).get('user', {})
117         title = user.get('display_name') or user.get('username')
118         description = user.get('description')
119
120         entries = [
121             self.url_result(
122                 'https://www.periscope.tv/%s/%s' % (user_id, broadcast['id']))
123             for broadcast in data_store.get('UserBroadcastHistory', {}).get('broadcasts', [])]
124
125         return self.playlist_result(entries, user_id, title, description)