| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  | # coding: utf-8 | 
					
						
							|  |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from .common import InfoExtractor | 
					
						
							|  |  |  | from ..utils import ( | 
					
						
							|  |  |  |     ExtractorError, | 
					
						
							|  |  |  |     clean_html, | 
					
						
							|  |  |  |     compat_str, | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |     float_or_none, | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  |     int_or_none, | 
					
						
							|  |  |  |     parse_iso8601, | 
					
						
							|  |  |  |     try_get, | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |     urljoin, | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  | class BeamProBaseIE(InfoExtractor): | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |     _API_BASE = 'https://mixer.com/api/v1' | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |     _RATINGS = {'family': 0, 'teen': 13, '18+': 18} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _extract_channel_info(self, chan): | 
					
						
							|  |  |  |         user_id = chan.get('userId') or try_get(chan, lambda x: x['user']['id']) | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             'uploader': chan.get('token') or try_get( | 
					
						
							|  |  |  |                 chan, lambda x: x['user']['username'], compat_str), | 
					
						
							|  |  |  |             'uploader_id': compat_str(user_id) if user_id else None, | 
					
						
							|  |  |  |             'age_limit': self._RATINGS.get(chan.get('audience')), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BeamProLiveIE(BeamProBaseIE): | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |     IE_NAME = 'Mixer:live' | 
					
						
							|  |  |  |     _VALID_URL = r'https?://(?:\w+\.)?(?:beam\.pro|mixer\.com)/(?P<id>[^/?#&]+)' | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  |     _TEST = { | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |         'url': 'http://mixer.com/niterhayven', | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '261562', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': 'Introducing The Witcher 3 //  The Grind Starts Now!', | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |             'description': 'md5:0b161ac080f15fe05d18a07adb44a74d', | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  |             'thumbnail': r're:https://.*\.jpg$', | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |             'timestamp': 1483477281, | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  |             'upload_date': '20170103', | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |             'uploader': 'niterhayven', | 
					
						
							|  |  |  |             'uploader_id': '373396', | 
					
						
							|  |  |  |             'age_limit': 18, | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  |             'is_live': True, | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |             'view_count': int, | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  |         }, | 
					
						
							|  |  |  |         'skip': 'niterhayven is offline', | 
					
						
							|  |  |  |         'params': { | 
					
						
							|  |  |  |             'skip_download': True, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |     _MANIFEST_URL_TEMPLATE = '%s/channels/%%s/manifest.%%s' % BeamProBaseIE._API_BASE | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |     @classmethod | 
					
						
							|  |  |  |     def suitable(cls, url): | 
					
						
							|  |  |  |         return False if BeamProVodIE.suitable(url) else super(BeamProLiveIE, cls).suitable(url) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  |     def _real_extract(self, url): | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |         channel_name = self._match_id(url) | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |         chan = self._download_json( | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |             '%s/channels/%s' % (self._API_BASE, channel_name), channel_name) | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |         if chan.get('online') is False: | 
					
						
							|  |  |  |             raise ExtractorError( | 
					
						
							|  |  |  |                 '{0} is offline'.format(channel_name), expected=True) | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |         channel_id = chan['id'] | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |         def manifest_url(kind): | 
					
						
							|  |  |  |             return self._MANIFEST_URL_TEMPLATE % (channel_id, kind) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |         formats = self._extract_m3u8_formats( | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |             manifest_url('m3u8'), channel_name, ext='mp4', m3u8_id='hls', | 
					
						
							|  |  |  |             fatal=False) | 
					
						
							|  |  |  |         formats.extend(self._extract_smil_formats( | 
					
						
							|  |  |  |             manifest_url('smil'), channel_name, fatal=False)) | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |         self._sort_formats(formats) | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |         info = { | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |             'id': compat_str(chan.get('id') or channel_name), | 
					
						
							|  |  |  |             'title': self._live_title(chan.get('name') or channel_name), | 
					
						
							|  |  |  |             'description': clean_html(chan.get('description')), | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |             'thumbnail': try_get( | 
					
						
							|  |  |  |                 chan, lambda x: x['thumbnail']['url'], compat_str), | 
					
						
							| 
									
										
										
										
											2017-01-15 06:07:35 +07:00
										 |  |  |             'timestamp': parse_iso8601(chan.get('updatedAt')), | 
					
						
							|  |  |  |             'is_live': True, | 
					
						
							|  |  |  |             'view_count': int_or_none(chan.get('viewersTotal')), | 
					
						
							|  |  |  |             'formats': formats, | 
					
						
							| 
									
										
										
										
											2017-01-04 01:51:08 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |         info.update(self._extract_channel_info(chan)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return info | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BeamProVodIE(BeamProBaseIE): | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |     IE_NAME = 'Mixer:vod' | 
					
						
							|  |  |  |     _VALID_URL = r'https?://(?:\w+\.)?(?:beam\.pro|mixer\.com)/[^/?#&]+\?.*?\bvod=(?P<id>\d+)' | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |     _TEST = { | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |         'url': 'https://mixer.com/willow8714?vod=2259830', | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |         'md5': 'b2431e6e8347dc92ebafb565d368b76b', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '2259830', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': 'willow8714\'s Channel', | 
					
						
							|  |  |  |             'duration': 6828.15, | 
					
						
							|  |  |  |             'thumbnail': r're:https://.*source\.png$', | 
					
						
							|  |  |  |             'timestamp': 1494046474, | 
					
						
							|  |  |  |             'upload_date': '20170506', | 
					
						
							|  |  |  |             'uploader': 'willow8714', | 
					
						
							|  |  |  |             'uploader_id': '6085379', | 
					
						
							|  |  |  |             'age_limit': 13, | 
					
						
							|  |  |  |             'view_count': int, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |         'params': { | 
					
						
							|  |  |  |             'skip_download': True, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |     @staticmethod | 
					
						
							|  |  |  |     def _extract_format(vod, vod_type): | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |         if not vod.get('baseUrl'): | 
					
						
							|  |  |  |             return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if vod_type == 'hls': | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |             filename, protocol = 'manifest.m3u8', 'm3u8_native' | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |         elif vod_type == 'raw': | 
					
						
							|  |  |  |             filename, protocol = 'source.mp4', 'https' | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |             assert False | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |         data = vod.get('data') if isinstance(vod.get('data'), dict) else {} | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         format_id = [vod_type] | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |         if isinstance(data.get('Height'), compat_str): | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  |             format_id.append('%sp' % data['Height']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [{ | 
					
						
							|  |  |  |             'url': urljoin(vod['baseUrl'], filename), | 
					
						
							|  |  |  |             'format_id': '-'.join(format_id), | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'protocol': protocol, | 
					
						
							|  |  |  |             'width': int_or_none(data.get('Width')), | 
					
						
							|  |  |  |             'height': int_or_none(data.get('Height')), | 
					
						
							|  |  |  |             'fps': int_or_none(data.get('Fps')), | 
					
						
							|  |  |  |             'tbr': int_or_none(data.get('Bitrate'), 1000), | 
					
						
							|  |  |  |         }] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         vod_id = self._match_id(url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         vod_info = self._download_json( | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |             '%s/recordings/%s' % (self._API_BASE, vod_id), vod_id) | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         state = vod_info.get('state') | 
					
						
							|  |  |  |         if state != 'AVAILABLE': | 
					
						
							|  |  |  |             raise ExtractorError( | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |                 'VOD %s is not available (state: %s)' % (vod_id, state), | 
					
						
							|  |  |  |                 expected=True) | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         formats = [] | 
					
						
							|  |  |  |         thumbnail_url = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for vod in vod_info['vods']: | 
					
						
							|  |  |  |             vod_type = vod.get('format') | 
					
						
							|  |  |  |             if vod_type in ('hls', 'raw'): | 
					
						
							|  |  |  |                 formats.extend(self._extract_format(vod, vod_type)) | 
					
						
							|  |  |  |             elif vod_type == 'thumbnail': | 
					
						
							|  |  |  |                 thumbnail_url = urljoin(vod.get('baseUrl'), 'source.png') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self._sort_formats(formats) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         info = { | 
					
						
							|  |  |  |             'id': vod_id, | 
					
						
							|  |  |  |             'title': vod_info.get('name') or vod_id, | 
					
						
							|  |  |  |             'duration': float_or_none(vod_info.get('duration')), | 
					
						
							|  |  |  |             'thumbnail': thumbnail_url, | 
					
						
							|  |  |  |             'timestamp': parse_iso8601(vod_info.get('createdAt')), | 
					
						
							|  |  |  |             'view_count': int_or_none(vod_info.get('viewsTotal')), | 
					
						
							|  |  |  |             'formats': formats, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-05-28 05:39:28 +07:00
										 |  |  |         info.update(self._extract_channel_info(vod_info.get('channel') or {})) | 
					
						
							| 
									
										
										
										
											2017-05-14 10:04:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return info |