]>
Commit | Line | Data |
---|---|---|
3550821f S |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | from .common import InfoExtractor | |
1e83741c | 5 | from ..utils import parse_iso8601 |
3550821f S |
6 | |
7 | ||
8 | class PeriscopeIE(InfoExtractor): | |
1e83741c | 9 | IE_DESC = 'Periscope' |
6f59aa93 | 10 | IE_NAME = 'periscope' |
0c59d02b | 11 | _VALID_URL = r'https?://(?:www\.)?periscope\.tv/[^/]+/(?P<id>[^/?#]+)' |
53472df8 | 12 | # Alive example URLs can be found here http://onperiscope.com/ |
2549e113 | 13 | _TESTS = [{ |
3550821f S |
14 | 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==', |
15 | 'md5': '65b57957972e503fcbbaeed8f4fa04ca', | |
16 | 'info_dict': { | |
17 | 'id': '56102209', | |
18 | 'ext': 'mp4', | |
19 | 'title': 'Bec Boop - 🚠✈️🇬🇧 Fly above #London in Emirates Air Line cable car at night 🇬🇧✈️🚠 #BoopScope 🎀💗', | |
20 | 'timestamp': 1438978559, | |
21 | 'upload_date': '20150807', | |
22 | 'uploader': 'Bec Boop', | |
23 | 'uploader_id': '1465763', | |
24 | }, | |
25 | 'skip': 'Expires in 24 hours', | |
2549e113 S |
26 | }, { |
27 | 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv', | |
28 | 'only_matching': True, | |
0c59d02b S |
29 | }, { |
30 | 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX', | |
31 | 'only_matching': True, | |
2549e113 | 32 | }] |
3550821f | 33 | |
b15c44cd | 34 | def _call_api(self, method, value): |
621d6a95 | 35 | return self._download_json( |
89abf7bf | 36 | 'https://api.periscope.tv/api/v2/%s?broadcast_id=%s' % (method, value), value) |
3550821f | 37 | |
621d6a95 S |
38 | def _real_extract(self, url): |
39 | token = self._match_id(url) | |
3550821f | 40 | |
621d6a95 | 41 | broadcast_data = self._call_api('getBroadcastPublic', token) |
3550821f S |
42 | broadcast = broadcast_data['broadcast'] |
43 | status = broadcast['status'] | |
44 | ||
45 | uploader = broadcast.get('user_display_name') or broadcast_data.get('user', {}).get('display_name') | |
46 | uploader_id = broadcast.get('user_id') or broadcast_data.get('user', {}).get('id') | |
47 | ||
48 | title = '%s - %s' % (uploader, status) if uploader else status | |
1e83741c S |
49 | state = broadcast.get('state').lower() |
50 | if state == 'running': | |
51 | title = self._live_title(title) | |
3550821f S |
52 | timestamp = parse_iso8601(broadcast.get('created_at')) |
53 | ||
54 | thumbnails = [{ | |
55 | 'url': broadcast[image], | |
56 | } for image in ('image_url', 'image_url_small') if broadcast.get(image)] | |
57 | ||
1e83741c S |
58 | stream = self._call_api('getAccessPublic', token) |
59 | ||
60 | formats = [] | |
61 | for format_id in ('replay', 'rtmp', 'hls', 'https_hls'): | |
62 | video_url = stream.get(format_id + '_url') | |
63 | if not video_url: | |
64 | continue | |
65 | f = { | |
66 | 'url': video_url, | |
67 | 'ext': 'flv' if format_id == 'rtmp' else 'mp4', | |
68 | } | |
69 | if format_id != 'rtmp': | |
70 | f['protocol'] = 'm3u8_native' if state == 'ended' else 'm3u8' | |
71 | formats.append(f) | |
72 | self._sort_formats(formats) | |
73 | ||
3550821f | 74 | return { |
621d6a95 | 75 | 'id': broadcast.get('id') or token, |
3550821f S |
76 | 'title': title, |
77 | 'timestamp': timestamp, | |
78 | 'uploader': uploader, | |
79 | 'uploader_id': uploader_id, | |
80 | 'thumbnails': thumbnails, | |
1e83741c | 81 | 'formats': formats, |
3550821f | 82 | } |
6f59aa93 YCH |
83 | |
84 | ||
85 | class PeriscopeUserIE(InfoExtractor): | |
86 | _VALID_URL = r'https?://www\.periscope\.tv/(?P<id>[^/]+)/?$' | |
87 | IE_DESC = 'Periscope user videos' | |
88 | IE_NAME = 'periscope:user' | |
89 | ||
90 | _TEST = { | |
91 | 'url': 'https://www.periscope.tv/LularoeHusbandMike/', | |
92 | 'info_dict': { | |
93 | 'id': 'LularoeHusbandMike', | |
94 | 'title': 'LULAROE HUSBAND MIKE', | |
95 | }, | |
96 | # Periscope only shows videos in the last 24 hours, so it's possible to | |
97 | # get 0 videos | |
98 | 'playlist_mincount': 0, | |
99 | } | |
100 | ||
101 | def _real_extract(self, url): | |
102 | user_id = self._match_id(url) | |
103 | ||
104 | webpage = self._download_webpage(url, user_id) | |
105 | ||
106 | broadcast_data = self._parse_json(self._html_search_meta( | |
107 | 'broadcast-data', webpage, default='{}'), user_id) | |
108 | username = broadcast_data.get('user', {}).get('display_name') | |
109 | user_broadcasts = self._parse_json( | |
110 | self._html_search_meta('user-broadcasts', webpage, default='{}'), | |
111 | user_id) | |
112 | ||
113 | entries = [ | |
114 | self.url_result( | |
115 | 'https://www.periscope.tv/%s/%s' % (user_id, broadcast['id'])) | |
116 | for broadcast in user_broadcasts.get('broadcasts', [])] | |
117 | ||
118 | return self.playlist_result(entries, user_id, username) |