]>
Commit | Line | Data |
---|---|---|
3550821f S |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | from .common import InfoExtractor | |
0db9a05f S |
5 | from ..utils import ( |
6 | parse_iso8601, | |
7 | unescapeHTML, | |
8 | ) | |
3550821f S |
9 | |
10 | ||
11 | class PeriscopeIE(InfoExtractor): | |
1e83741c | 12 | IE_DESC = 'Periscope' |
6f59aa93 | 13 | IE_NAME = 'periscope' |
0c59d02b | 14 | _VALID_URL = r'https?://(?:www\.)?periscope\.tv/[^/]+/(?P<id>[^/?#]+)' |
53472df8 | 15 | # Alive example URLs can be found here http://onperiscope.com/ |
2549e113 | 16 | _TESTS = [{ |
3550821f S |
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', | |
2549e113 S |
29 | }, { |
30 | 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv', | |
31 | 'only_matching': True, | |
0c59d02b S |
32 | }, { |
33 | 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX', | |
34 | 'only_matching': True, | |
2549e113 | 35 | }] |
3550821f | 36 | |
b15c44cd | 37 | def _call_api(self, method, value): |
621d6a95 | 38 | return self._download_json( |
89abf7bf | 39 | 'https://api.periscope.tv/api/v2/%s?broadcast_id=%s' % (method, value), value) |
3550821f | 40 | |
621d6a95 S |
41 | def _real_extract(self, url): |
42 | token = self._match_id(url) | |
3550821f | 43 | |
621d6a95 | 44 | broadcast_data = self._call_api('getBroadcastPublic', token) |
3550821f S |
45 | broadcast = broadcast_data['broadcast'] |
46 | status = broadcast['status'] | |
47 | ||
92d221ad S |
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')) | |
3550821f S |
53 | |
54 | title = '%s - %s' % (uploader, status) if uploader else status | |
1e83741c S |
55 | state = broadcast.get('state').lower() |
56 | if state == 'running': | |
57 | title = self._live_title(title) | |
3550821f S |
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 | ||
1e83741c S |
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 | ||
3550821f | 80 | return { |
621d6a95 | 81 | 'id': broadcast.get('id') or token, |
3550821f S |
82 | 'title': title, |
83 | 'timestamp': timestamp, | |
84 | 'uploader': uploader, | |
85 | 'uploader_id': uploader_id, | |
86 | 'thumbnails': thumbnails, | |
1e83741c | 87 | 'formats': formats, |
3550821f | 88 | } |
6f59aa93 YCH |
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', | |
0db9a05f | 101 | 'description': 'md5:6cf4ec8047768098da58e446e82c82f0', |
6f59aa93 YCH |
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 | ||
0db9a05f S |
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')), | |
6f59aa93 YCH |
117 | user_id) |
118 | ||
0db9a05f S |
119 | user = data_store.get('User', {}).get('user', {}) |
120 | title = user.get('display_name') or user.get('username') | |
121 | description = user.get('description') | |
122 | ||
35fc3021 S |
123 | broadcast_ids = (data_store.get('UserBroadcastHistory', {}).get('broadcastIds') or |
124 | data_store.get('BroadcastCache', {}).get('broadcastIds', [])) | |
125 | ||
6f59aa93 YCH |
126 | entries = [ |
127 | self.url_result( | |
35fc3021 S |
128 | 'https://www.periscope.tv/%s/%s' % (user_id, broadcast_id)) |
129 | for broadcast_id in broadcast_ids] | |
6f59aa93 | 130 | |
0db9a05f | 131 | return self.playlist_result(entries, user_id, title, description) |