| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  | # coding: utf-8 | 
					
						
							|  |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 19:57:55 +08:00
										 |  |  | import re | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  | from .common import InfoExtractor | 
					
						
							| 
									
										
										
										
											2016-05-22 02:15:56 +06:00
										 |  |  | from ..utils import ( | 
					
						
							|  |  |  |     parse_iso8601, | 
					
						
							|  |  |  |     unescapeHTML, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-28 02:35:49 +07:00
										 |  |  | class PeriscopeBaseIE(InfoExtractor): | 
					
						
							|  |  |  |     def _call_api(self, method, query, item_id): | 
					
						
							|  |  |  |         return self._download_json( | 
					
						
							|  |  |  |             'https://api.periscope.tv/api/v2/%s' % method, | 
					
						
							|  |  |  |             item_id, query=query) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PeriscopeIE(PeriscopeBaseIE): | 
					
						
							| 
									
										
										
										
											2015-08-08 05:33:53 +06:00
										 |  |  |     IE_DESC = 'Periscope' | 
					
						
							| 
									
										
										
										
											2016-05-06 02:14:39 +08:00
										 |  |  |     IE_NAME = 'periscope' | 
					
						
							| 
									
										
										
										
											2017-04-02 03:20:28 +07:00
										 |  |  |     _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/[^/]+/(?P<id>[^/?#]+)' | 
					
						
							| 
									
										
										
										
											2015-11-05 02:56:44 +06:00
										 |  |  |     # Alive example URLs can be found here http://onperiscope.com/ | 
					
						
							| 
									
										
										
										
											2015-11-05 02:55:53 +06:00
										 |  |  |     _TESTS = [{ | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  |         'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==', | 
					
						
							|  |  |  |         'md5': '65b57957972e503fcbbaeed8f4fa04ca', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '56102209', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': 'Bec Boop - 🚠✈️🇬🇧 Fly above #London in Emirates Air Line cable car at night 🇬🇧✈️🚠 #BoopScope 🎀💗', | 
					
						
							|  |  |  |             'timestamp': 1438978559, | 
					
						
							|  |  |  |             'upload_date': '20150807', | 
					
						
							|  |  |  |             'uploader': 'Bec Boop', | 
					
						
							|  |  |  |             'uploader_id': '1465763', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         'skip': 'Expires in 24 hours', | 
					
						
							| 
									
										
										
										
											2015-11-05 02:55:53 +06:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							| 
									
										
										
										
											2015-11-15 00:20:17 +06:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'https://www.periscope.tv/bastaakanoggano/1OdKrlkZZjOJX', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							| 
									
										
										
										
											2017-04-02 03:20:28 +07:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'https://www.periscope.tv/w/1ZkKzPbMVggJv', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							| 
									
										
										
										
											2015-11-05 02:55:53 +06:00
										 |  |  |     }] | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 19:57:55 +08:00
										 |  |  |     @staticmethod | 
					
						
							|  |  |  |     def _extract_url(webpage): | 
					
						
							|  |  |  |         mobj = re.search( | 
					
						
							| 
									
										
										
										
											2017-07-11 15:35:19 +08:00
										 |  |  |             r'<iframe[^>]+src=([\'"])(?P<url>(?:https?:)?//(?:www\.)?(?:periscope|pscp)\.tv/(?:(?!\1).)+)\1', webpage) | 
					
						
							| 
									
										
										
										
											2016-09-24 19:57:55 +08:00
										 |  |  |         if mobj: | 
					
						
							|  |  |  |             return mobj.group('url') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 04:00:52 +06:00
										 |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         token = self._match_id(url) | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 17:23:37 +07:00
										 |  |  |         stream = self._call_api( | 
					
						
							|  |  |  |             'accessVideoPublic', {'broadcast_id': token}, token) | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 17:23:37 +07:00
										 |  |  |         broadcast = stream['broadcast'] | 
					
						
							|  |  |  |         title = broadcast['status'] | 
					
						
							| 
									
										
										
										
											2016-05-22 02:39:15 +06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 17:23:37 +07:00
										 |  |  |         uploader = broadcast.get('user_display_name') or broadcast.get('username') | 
					
						
							|  |  |  |         uploader_id = (broadcast.get('user_id') or broadcast.get('username')) | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 17:23:37 +07:00
										 |  |  |         title = '%s - %s' % (uploader, title) if uploader else title | 
					
						
							| 
									
										
										
										
											2015-08-08 05:33:53 +06:00
										 |  |  |         state = broadcast.get('state').lower() | 
					
						
							|  |  |  |         if state == 'running': | 
					
						
							|  |  |  |             title = self._live_title(title) | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  |         timestamp = parse_iso8601(broadcast.get('created_at')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         thumbnails = [{ | 
					
						
							|  |  |  |             'url': broadcast[image], | 
					
						
							|  |  |  |         } for image in ('image_url', 'image_url_small') if broadcast.get(image)] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 23:03:42 +07:00
										 |  |  |         video_urls = set() | 
					
						
							| 
									
										
										
										
											2015-08-08 05:33:53 +06:00
										 |  |  |         formats = [] | 
					
						
							| 
									
										
										
										
											2017-08-16 23:03:42 +07:00
										 |  |  |         for format_id in ('replay', 'rtmp', 'hls', 'https_hls', 'lhls', 'lhlsweb'): | 
					
						
							| 
									
										
										
										
											2015-08-08 05:33:53 +06:00
										 |  |  |             video_url = stream.get(format_id + '_url') | 
					
						
							| 
									
										
										
										
											2017-08-16 23:03:42 +07:00
										 |  |  |             if not video_url or video_url in video_urls: | 
					
						
							| 
									
										
										
										
											2015-08-08 05:33:53 +06:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2017-08-16 23:03:42 +07:00
										 |  |  |             video_urls.add(video_url) | 
					
						
							|  |  |  |             if format_id != 'rtmp': | 
					
						
							|  |  |  |                 formats.extend(self._extract_m3u8_formats( | 
					
						
							|  |  |  |                     video_url, token, 'mp4', | 
					
						
							|  |  |  |                     entry_protocol='m3u8_native' | 
					
						
							|  |  |  |                     if state in ('ended', 'timed_out') else 'm3u8', | 
					
						
							|  |  |  |                     m3u8_id=format_id, fatal=False)) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             formats.append({ | 
					
						
							| 
									
										
										
										
											2015-08-08 05:33:53 +06:00
										 |  |  |                 'url': video_url, | 
					
						
							|  |  |  |                 'ext': 'flv' if format_id == 'rtmp' else 'mp4', | 
					
						
							| 
									
										
										
										
											2017-08-16 23:03:42 +07:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											2015-08-08 05:33:53 +06:00
										 |  |  |         self._sort_formats(formats) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2015-08-08 04:00:52 +06:00
										 |  |  |             'id': broadcast.get('id') or token, | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  |             'title': title, | 
					
						
							|  |  |  |             'timestamp': timestamp, | 
					
						
							|  |  |  |             'uploader': uploader, | 
					
						
							|  |  |  |             'uploader_id': uploader_id, | 
					
						
							|  |  |  |             'thumbnails': thumbnails, | 
					
						
							| 
									
										
										
										
											2015-08-08 05:33:53 +06:00
										 |  |  |             'formats': formats, | 
					
						
							| 
									
										
										
										
											2015-08-08 03:38:55 +06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-06 02:14:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-28 02:35:49 +07:00
										 |  |  | class PeriscopeUserIE(PeriscopeBaseIE): | 
					
						
							| 
									
										
										
										
											2017-04-02 03:20:28 +07:00
										 |  |  |     _VALID_URL = r'https?://(?:www\.)?(?:periscope|pscp)\.tv/(?P<id>[^/]+)/?$' | 
					
						
							| 
									
										
										
										
											2016-05-06 02:14:39 +08:00
										 |  |  |     IE_DESC = 'Periscope user videos' | 
					
						
							|  |  |  |     IE_NAME = 'periscope:user' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _TEST = { | 
					
						
							|  |  |  |         'url': 'https://www.periscope.tv/LularoeHusbandMike/', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': 'LularoeHusbandMike', | 
					
						
							|  |  |  |             'title': 'LULAROE HUSBAND MIKE', | 
					
						
							| 
									
										
										
										
											2016-05-22 02:15:56 +06:00
										 |  |  |             'description': 'md5:6cf4ec8047768098da58e446e82c82f0', | 
					
						
							| 
									
										
										
										
											2016-05-06 02:14:39 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         # Periscope only shows videos in the last 24 hours, so it's possible to | 
					
						
							|  |  |  |         # get 0 videos | 
					
						
							|  |  |  |         'playlist_mincount': 0, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							| 
									
										
										
										
											2016-08-28 02:35:49 +07:00
										 |  |  |         user_name = self._match_id(url) | 
					
						
							| 
									
										
										
										
											2016-05-06 02:14:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-28 02:35:49 +07:00
										 |  |  |         webpage = self._download_webpage(url, user_name) | 
					
						
							| 
									
										
										
										
											2016-05-06 02:14:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-22 02:15:56 +06:00
										 |  |  |         data_store = self._parse_json( | 
					
						
							|  |  |  |             unescapeHTML(self._search_regex( | 
					
						
							|  |  |  |                 r'data-store=(["\'])(?P<data>.+?)\1', | 
					
						
							|  |  |  |                 webpage, 'data store', default='{}', group='data')), | 
					
						
							| 
									
										
										
										
											2016-08-28 02:35:49 +07:00
										 |  |  |             user_name) | 
					
						
							| 
									
										
										
										
											2016-05-06 02:14:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-28 02:35:49 +07:00
										 |  |  |         user = list(data_store['UserCache']['users'].values())[0]['user'] | 
					
						
							|  |  |  |         user_id = user['id'] | 
					
						
							| 
									
										
										
										
											2016-10-01 22:50:47 +07:00
										 |  |  |         session_id = data_store['SessionToken']['public']['broadcastHistory']['token']['session_id'] | 
					
						
							| 
									
										
										
										
											2016-08-28 02:35:49 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         broadcasts = self._call_api( | 
					
						
							|  |  |  |             'getUserBroadcastsPublic', | 
					
						
							|  |  |  |             {'user_id': user_id, 'session_id': session_id}, | 
					
						
							|  |  |  |             user_name)['broadcasts'] | 
					
						
							| 
									
										
										
										
											2016-05-22 02:15:56 +06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-28 02:35:49 +07:00
										 |  |  |         broadcast_ids = [ | 
					
						
							|  |  |  |             broadcast['id'] for broadcast in broadcasts if broadcast.get('id')] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         title = user.get('display_name') or user.get('username') or user_name | 
					
						
							|  |  |  |         description = user.get('description') | 
					
						
							| 
									
										
										
										
											2016-07-02 01:35:57 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 02:14:39 +08:00
										 |  |  |         entries = [ | 
					
						
							|  |  |  |             self.url_result( | 
					
						
							| 
									
										
										
										
											2016-08-28 02:35:49 +07:00
										 |  |  |                 'https://www.periscope.tv/%s/%s' % (user_name, broadcast_id)) | 
					
						
							| 
									
										
										
										
											2016-07-02 01:35:57 +07:00
										 |  |  |             for broadcast_id in broadcast_ids] | 
					
						
							| 
									
										
										
										
											2016-05-06 02:14:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-22 02:15:56 +06:00
										 |  |  |         return self.playlist_result(entries, user_id, title, description) |