| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  | # coding: utf-8 | 
					
						
							|  |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from .common import InfoExtractor | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  | from ..compat import compat_str | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  | from ..utils import ( | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |     ExtractorError, | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  |     int_or_none, | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |     try_get, | 
					
						
							|  |  |  |     unified_timestamp, | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class FreshLiveIE(InfoExtractor): | 
					
						
							|  |  |  |     _VALID_URL = r'https?://freshlive\.tv/[^/]+/(?P<id>\d+)' | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  |     _TEST = { | 
					
						
							|  |  |  |         'url': 'https://freshlive.tv/satotv/74712', | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |         'md5': '9f0cf5516979c4454ce982df3d97f352', | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '74712', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': 'テスト', | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |             'description': 'テスト', | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  |             'thumbnail': r're:^https?://.*\.jpg$', | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |             'duration': 1511, | 
					
						
							|  |  |  |             'timestamp': 1483619655, | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  |             'upload_date': '20170105', | 
					
						
							|  |  |  |             'uploader': 'サトTV', | 
					
						
							|  |  |  |             'uploader_id': 'satotv', | 
					
						
							|  |  |  |             'view_count': int, | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |             'comment_count': int, | 
					
						
							|  |  |  |             'is_live': False, | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         video_id = self._match_id(url) | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  |         webpage = self._download_webpage(url, video_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         options = self._parse_json( | 
					
						
							|  |  |  |             self._search_regex( | 
					
						
							|  |  |  |                 r'window\.__CONTEXT__\s*=\s*({.+?});\s*</script>', | 
					
						
							|  |  |  |                 webpage, 'initial context'), | 
					
						
							|  |  |  |             video_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |         info = options['context']['dispatcher']['stores']['ProgramStore']['programs'][video_id] | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |         title = info['title'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if info.get('status') == 'upcoming': | 
					
						
							|  |  |  |             raise ExtractorError('Stream %s is upcoming' % video_id, expected=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         stream_url = info.get('liveStreamUrl') or info['archiveStreamUrl'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         is_live = info.get('liveStreamUrl') is not None | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         formats = self._extract_m3u8_formats( | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |             stream_url, video_id, ext='mp4', | 
					
						
							|  |  |  |             entry_protocol='m3u8' if is_live else 'm3u8_native', | 
					
						
							|  |  |  |             m3u8_id='hls') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if is_live: | 
					
						
							|  |  |  |             title = self._live_title(title) | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             'id': video_id, | 
					
						
							|  |  |  |             'formats': formats, | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |             'title': title, | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  |             'description': info.get('description'), | 
					
						
							|  |  |  |             'thumbnail': info.get('thumbnailUrl'), | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |             'duration': int_or_none(info.get('airTime')), | 
					
						
							|  |  |  |             'timestamp': unified_timestamp(info.get('createdAt')), | 
					
						
							|  |  |  |             'uploader': try_get( | 
					
						
							|  |  |  |                 info, lambda x: x['channel']['title'], compat_str), | 
					
						
							|  |  |  |             'uploader_id': try_get( | 
					
						
							|  |  |  |                 info, lambda x: x['channel']['code'], compat_str), | 
					
						
							|  |  |  |             'uploader_url': try_get( | 
					
						
							|  |  |  |                 info, lambda x: x['channel']['permalink'], compat_str), | 
					
						
							| 
									
										
										
										
											2017-02-18 17:42:31 +00:00
										 |  |  |             'view_count': int_or_none(info.get('viewCount')), | 
					
						
							| 
									
										
										
										
											2017-02-25 22:56:42 +07:00
										 |  |  |             'comment_count': int_or_none(info.get('commentCount')), | 
					
						
							|  |  |  |             'tags': info.get('tags', []), | 
					
						
							|  |  |  |             'is_live': is_live, | 
					
						
							|  |  |  |         } |