]>
Commit | Line | Data |
---|---|---|
3550821f S |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
f0bc5a86 YCH |
4 | import re |
5 | ||
3550821f | 6 | from .common import InfoExtractor |
0db9a05f S |
7 | from ..utils import ( |
8 | parse_iso8601, | |
9 | unescapeHTML, | |
10 | ) | |
3550821f S |
11 | |
12 | ||
92c27a0d S |
13 | class PeriscopeBaseIE(InfoExtractor): |
14 | def _call_api(self, method, query, item_id): | |
15 | return self._download_json( | |
16 | 'https://api.periscope.tv/api/v2/%s' % method, | |
17 | item_id, query=query) | |
18 | ||
19 | ||
20 | class PeriscopeIE(PeriscopeBaseIE): | |
1e83741c | 21 | IE_DESC = 'Periscope' |
6f59aa93 | 22 | IE_NAME = 'periscope' |
b3633fa0 | 23 | _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)' |
53472df8 | 24 | # Alive example URLs can be found here http://onperiscope.com/ |
2549e113 | 25 | _TESTS = [{ |
3550821f S |
26 | 'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==', |
27 | 'md5': '65b57957972e503fcbbaeed8f4fa04ca', | |
28 | 'info_dict': { | |
29 | 'id': '56102209', | |
30 | 'ext': 'mp4', | |
31 | 'title': 'Bec Boop - 🚠✈️🇬🇧 Fly above #London in Emirates Air Line cable car at night 🇬🇧✈️🚠 #BoopScope 🎀💗', | |
32 | 'timestamp': 1438978559, | |
33 | 'upload_date': '20150807', | |
34 | 'uploader': 'Bec Boop', | |
35 | 'uploader_id': '1465763', | |
36 | }, | |
37 | 'skip': 'Expires in 24 hours', | |
2549e113 S |
38 | }, { |
39 | 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv', | |
40 | 'only_matching': True, | |
0c59d02b S |
41 | }, { |
42 | 'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX', | |
43 | 'only_matching': True, | |
b3633fa0 S |
44 | }, { |
45 | 'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv', | |
46 | 'only_matching': True, | |
2549e113 | 47 | }] |
3550821f | 48 | |
f0bc5a86 YCH |
49 | @staticmethod |
50 | def _extract_url(webpage): | |
51 | mobj = re.search( | |
7f176ac4 | 52 | r'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage) |
f0bc5a86 YCH |
53 | if mobj: |
54 | return mobj.group('url') | |
55 | ||
621d6a95 S |
56 | def _real_extract(self, url): |
57 | token = self._match_id(url) | |
3550821f | 58 | |
d2b200ee S |
59 | stream = self._call_api( |
60 | 'accessVideoPublic', {'broadcast_id': token}, token) | |
3550821f | 61 | |
d2b200ee S |
62 | broadcast = stream['broadcast'] |
63 | title = broadcast['status'] | |
92d221ad | 64 | |
d2b200ee S |
65 | uploader = broadcast.get('user_display_name') or broadcast.get('username') |
66 | uploader_id = (broadcast.get('user_id') or broadcast.get('username')) | |
3550821f | 67 | |
d2b200ee | 68 | title = '%s - %s' % (uploader, title) if uploader else title |
1e83741c S |
69 | state = broadcast.get('state').lower() |
70 | if state == 'running': | |
71 | title = self._live_title(title) | |
3550821f S |
72 | timestamp = parse_iso8601(broadcast.get('created_at')) |
73 | ||
74 | thumbnails = [{ | |
75 | 'url': broadcast[image], | |
76 | } for image in ('image_url', 'image_url_small') if broadcast.get(image)] | |
77 | ||
a1aa6596 | 78 | video_urls = set() |
1e83741c | 79 | formats = [] |
a1aa6596 | 80 | for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'): |
1e83741c | 81 | video_url = stream.get(format_id + '_url') |
a1aa6596 | 82 | if not video_url or video_url in video_urls: |
1e83741c | 83 | continue |
a1aa6596 S |
84 | video_urls.add(video_url) |
85 | if format_id != 'rtmp': | |
86 | formats.extend(self._extract_m3u8_formats( | |
87 | video_url, token, 'mp4', | |
88 | entry_protocol='m3u8_native' | |
89 | if state in ('ended', 'timed_out') else 'm3u8', | |
90 | m3u8_id=format_id, fatal=False)) | |
91 | continue | |
92 | formats.append({ | |
1e83741c S |
93 | 'url': video_url, |
94 | 'ext': 'flv' if format_id == 'rtmp' else 'mp4', | |
a1aa6596 | 95 | }) |
1e83741c S |
96 | self._sort_formats(formats) |
97 | ||
3550821f | 98 | return { |
621d6a95 | 99 | 'id': broadcast.get('id') or token, |
3550821f S |
100 | 'title': title, |
101 | 'timestamp': timestamp, | |
102 | 'uploader': uploader, | |
103 | 'uploader_id': uploader_id, | |
104 | 'thumbnails': thumbnails, | |
1e83741c | 105 | 'formats': formats, |
3550821f | 106 | } |
6f59aa93 YCH |
107 | |
108 | ||
92c27a0d | 109 | class PeriscopeUserIE(PeriscopeBaseIE): |
b3633fa0 | 110 | _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$' |
6f59aa93 YCH |
111 | IE_DESC = 'Periscope user videos' |
112 | IE_NAME = 'periscope:user' | |
113 | ||
114 | _TEST = { | |
115 | 'url': 'https://www.periscope.tv/LularoeHusbandMike/', | |
116 | 'info_dict': { | |
117 | 'id': 'LularoeHusbandMike', | |
118 | 'title': 'LULAROE HUSBAND MIKE', | |
0db9a05f | 119 | 'description': 'md5:6cf4ec8047768098da58e446e82c82f0', |
6f59aa93 YCH |
120 | }, |
121 | # Periscope only shows videos in the last 24 hours, so it's possible to | |
122 | # get 0 videos | |
123 | 'playlist_mincount': 0, | |
124 | } | |
125 | ||
126 | def _real_extract(self, url): | |
92c27a0d | 127 | user_name = self._match_id(url) |
6f59aa93 | 128 | |
92c27a0d | 129 | webpage = self._download_webpage(url, user_name) |
6f59aa93 | 130 | |
0db9a05f S |
131 | data_store = self._parse_json( |
132 | unescapeHTML(self._search_regex( | |
133 | r'data-store=(["\'])(?P<data>.+?)\1', | |
134 | webpage, 'data store', default='{}', group='data')), | |
92c27a0d | 135 | user_name) |
6f59aa93 | 136 | |
92c27a0d S |
137 | user = list(data_store['UserCache']['users'].values())[0]['user'] |
138 | user_id = user['id'] | |
e1e97c24 | 139 | session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id'] |
92c27a0d S |
140 | |
141 | broadcasts = self._call_api( | |
142 | 'getUserBroadcastsPublic', | |
143 | {'user_id': user_id, 'session_id': session_id}, | |
144 | user_name)['broadcasts'] | |
0db9a05f | 145 | |
92c27a0d S |
146 | broadcast_ids = [ |
147 | broadcast['id'] for broadcast in broadcasts if broadcast.get('id')] | |
148 | ||
149 | title = user.get('display_name') or user.get('username') or user_name | |
150 | description = user.get('description') | |
35fc3021 | 151 | |
6f59aa93 YCH |
152 | entries = [ |
153 | self.url_result( | |
92c27a0d | 154 | 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id)) |
35fc3021 | 155 | for broadcast_id in broadcast_ids] |
6f59aa93 | 156 | |
0db9a05f | 157 | return self.playlist_result(entries, user_id, title, description) |