| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  | # coding: utf-8 | 
					
						
							|  |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-01 19:23:09 +08:00
										 |  |  | import re | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  | from .common import InfoExtractor | 
					
						
							| 
									
										
										
										
											2015-04-16 17:04:53 +08:00
										 |  |  | from ..utils import ( | 
					
						
							| 
									
										
										
										
											2016-06-01 19:23:09 +08:00
										 |  |  |     determine_ext, | 
					
						
							|  |  |  |     int_or_none, | 
					
						
							| 
									
										
										
										
											2015-04-16 17:04:53 +08:00
										 |  |  |     js_to_json, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-04-08 21:39:34 +08:00
										 |  |  | from ..compat import compat_urlparse | 
					
						
							| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UDNEmbedIE(InfoExtractor): | 
					
						
							| 
									
										
										
										
											2015-07-11 00:52:19 +08:00
										 |  |  |     IE_DESC = '聯合影音' | 
					
						
							| 
									
										
										
										
											2015-11-19 22:22:57 +08:00
										 |  |  |     _PROTOCOL_RELATIVE_VALID_URL = r'//video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)' | 
					
						
							|  |  |  |     _VALID_URL = r'https?:' + _PROTOCOL_RELATIVE_VALID_URL | 
					
						
							| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  |     _TESTS = [{ | 
					
						
							|  |  |  |         'url': 'http://video.udn.com/embed/news/300040', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '300040', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': '生物老師男變女 全校挺"做自己"', | 
					
						
							| 
									
										
										
										
											2017-01-02 20:08:07 +08:00
										 |  |  |             'thumbnail': r're:^https?://.*\.jpg$', | 
					
						
							| 
									
										
										
										
											2016-06-01 19:23:09 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         'params': { | 
					
						
							|  |  |  |             # m3u8 download | 
					
						
							|  |  |  |             'skip_download': True, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-12 16:04:41 +00:00
										 |  |  |         'expected_warnings': ['Failed to parse JSON Expecting value'], | 
					
						
							| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  |     }, { | 
					
						
							| 
									
										
										
										
											2015-04-16 17:04:53 +08:00
										 |  |  |         'url': 'https://video.udn.com/embed/news/300040', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |         # From https://video.udn.com/news/303776 | 
					
						
							|  |  |  |         'url': 'https://video.udn.com/play/news/303776', | 
					
						
							| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  |         'only_matching': True, | 
					
						
							|  |  |  |     }] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         video_id = self._match_id(url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         page = self._download_webpage(url, video_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 16:04:41 +00:00
										 |  |  |         options_str = self._html_search_regex( | 
					
						
							|  |  |  |             r'var\s+options\s*=\s*([^;]+);', page, 'options') | 
					
						
							|  |  |  |         trans_options_str = js_to_json(options_str) | 
					
						
							|  |  |  |         options = self._parse_json(trans_options_str, 'options', fatal=False) or {} | 
					
						
							|  |  |  |         if options: | 
					
						
							|  |  |  |             video_urls = options['video'] | 
					
						
							|  |  |  |             title = options['title'] | 
					
						
							|  |  |  |             poster = options.get('poster') | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             video_urls = self._parse_json(self._html_search_regex( | 
					
						
							|  |  |  |                 r'"video"\s*:\s*({.+?})\s*,', trans_options_str, 'video urls'), 'video urls') | 
					
						
							|  |  |  |             title = self._html_search_regex( | 
					
						
							|  |  |  |                 r"title\s*:\s*'(.+?)'\s*,", options_str, 'title') | 
					
						
							|  |  |  |             poster = self._html_search_regex( | 
					
						
							|  |  |  |                 r"poster\s*:\s*'(.+?)'\s*,", options_str, 'poster', default=None) | 
					
						
							| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if video_urls.get('youtube'): | 
					
						
							|  |  |  |             return self.url_result(video_urls.get('youtube'), 'Youtube') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-01 19:23:09 +08:00
										 |  |  |         formats = [] | 
					
						
							|  |  |  |         for video_type, api_url in video_urls.items(): | 
					
						
							|  |  |  |             if not api_url: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-01 19:23:09 +08:00
										 |  |  |             video_url = self._download_webpage( | 
					
						
							| 
									
										
										
										
											2015-04-08 21:39:34 +08:00
										 |  |  |                 compat_urlparse.urljoin(url, api_url), video_id, | 
					
						
							| 
									
										
										
										
											2016-06-01 19:23:09 +08:00
										 |  |  |                 note='retrieve url for %s video' % video_type) | 
					
						
							| 
									
										
										
										
											2015-04-16 17:04:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-01 19:23:09 +08:00
										 |  |  |             ext = determine_ext(video_url) | 
					
						
							|  |  |  |             if ext == 'm3u8': | 
					
						
							|  |  |  |                 formats.extend(self._extract_m3u8_formats( | 
					
						
							|  |  |  |                     video_url, video_id, ext='mp4', m3u8_id='hls')) | 
					
						
							|  |  |  |             elif ext == 'f4m': | 
					
						
							|  |  |  |                 formats.extend(self._extract_f4m_formats( | 
					
						
							|  |  |  |                     video_url, video_id, f4m_id='hds')) | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2017-10-12 16:04:41 +00:00
										 |  |  |                 mobj = re.search(r'_(?P<height>\d+)p_(?P<tbr>\d+)\.mp4', video_url) | 
					
						
							| 
									
										
										
										
											2016-06-01 19:23:09 +08:00
										 |  |  |                 a_format = { | 
					
						
							|  |  |  |                     'url': video_url, | 
					
						
							|  |  |  |                     # video_type may be 'mp4', which confuses YoutubeDL | 
					
						
							|  |  |  |                     'format_id': 'http-' + video_type, | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if mobj: | 
					
						
							|  |  |  |                     a_format.update({ | 
					
						
							|  |  |  |                         'height': int_or_none(mobj.group('height')), | 
					
						
							|  |  |  |                         'tbr': int_or_none(mobj.group('tbr')), | 
					
						
							|  |  |  |                     }) | 
					
						
							|  |  |  |                 formats.append(a_format) | 
					
						
							| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self._sort_formats(formats) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             'id': video_id, | 
					
						
							|  |  |  |             'formats': formats, | 
					
						
							| 
									
										
										
										
											2017-10-12 16:04:41 +00:00
										 |  |  |             'title': title, | 
					
						
							|  |  |  |             'thumbnail': poster, | 
					
						
							| 
									
										
										
										
											2015-04-08 17:26:51 +08:00
										 |  |  |         } |