| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  | # coding: utf-8 | 
					
						
							|  |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 22:59:17 +02:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from .common import InfoExtractor | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RTPIE(InfoExtractor): | 
					
						
							| 
									
										
										
										
											2014-12-14 00:13:07 +01:00
										 |  |  |     _VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?' | 
					
						
							|  |  |  |     _TESTS = [{ | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  |         'url': 'http://www.rtp.pt/play/p405/e174042/paixoes-cruzadas', | 
					
						
							| 
									
										
										
										
											2015-02-06 22:59:17 +02:00
										 |  |  |         'md5': 'e736ce0c665e459ddb818546220b4ef8', | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  |         'info_dict': { | 
					
						
							| 
									
										
										
										
											2014-12-21 15:28:40 +01:00
										 |  |  |             'id': 'e174042', | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  |             'ext': 'mp3', | 
					
						
							|  |  |  |             'title': 'Paixões Cruzadas', | 
					
						
							|  |  |  |             'description': 'As paixões musicais de António Cartaxo e António Macedo', | 
					
						
							| 
									
										
										
										
											2017-01-02 20:08:07 +08:00
										 |  |  |             'thumbnail': r're:^https?://.*\.jpg', | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2015-08-21 08:56:05 +06:00
										 |  |  |         'params': { | 
					
						
							|  |  |  |             # rtmp download | 
					
						
							|  |  |  |             'skip_download': True, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2014-12-14 00:13:07 +01:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'http://www.rtp.pt/play/p831/a-quimica-das-coisas', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							|  |  |  |     }] | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         video_id = self._match_id(url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         webpage = self._download_webpage(url, video_id) | 
					
						
							|  |  |  |         title = self._html_search_meta( | 
					
						
							|  |  |  |             'twitter:title', webpage, display_name='title', fatal=True) | 
					
						
							|  |  |  |         description = self._html_search_meta('description', webpage) | 
					
						
							|  |  |  |         thumbnail = self._og_search_thumbnail(webpage) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         player_config = self._search_regex( | 
					
						
							|  |  |  |             r'(?s)RTPPLAY\.player\.newPlayer\(\s*(\{.*?\})\s*\)', webpage, 'player config') | 
					
						
							| 
									
										
										
										
											2015-02-06 22:59:17 +02:00
										 |  |  |         config = self._parse_json(player_config, video_id) | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         path, ext = config.get('file').rsplit('.', 1) | 
					
						
							|  |  |  |         formats = [{ | 
					
						
							| 
									
										
										
										
											2015-02-06 22:59:17 +02:00
										 |  |  |             'format_id': 'rtmp', | 
					
						
							|  |  |  |             'ext': ext, | 
					
						
							|  |  |  |             'vcodec': config.get('type') == 'audio' and 'none' or None, | 
					
						
							|  |  |  |             'preference': -2, | 
					
						
							|  |  |  |             'url': 'rtmp://{streamer:s}/{application:s}'.format(**config), | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  |             'app': config.get('application'), | 
					
						
							|  |  |  |             'play_path': '{ext:s}:{path:s}'.format(ext=ext, path=path), | 
					
						
							|  |  |  |             'page_url': url, | 
					
						
							|  |  |  |             'rtmp_live': config.get('live', False), | 
					
						
							|  |  |  |             'player_url': 'http://programas.rtp.pt/play/player.swf?v3', | 
					
						
							| 
									
										
										
										
											2015-02-06 13:41:58 +02:00
										 |  |  |             'rtmp_real_time': True, | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  |         }] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-06 22:59:17 +02:00
										 |  |  |         # Construct regular HTTP download URLs | 
					
						
							|  |  |  |         replacements = { | 
					
						
							|  |  |  |             'audio': { | 
					
						
							|  |  |  |                 'format_id': 'mp3', | 
					
						
							|  |  |  |                 'pattern': r'^nas2\.share/wavrss/', | 
					
						
							|  |  |  |                 'repl': 'http://rsspod.rtp.pt/podcasts/', | 
					
						
							|  |  |  |                 'vcodec': 'none', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             'video': { | 
					
						
							|  |  |  |                 'format_id': 'mp4_h264', | 
					
						
							|  |  |  |                 'pattern': r'^nas2\.share/h264/', | 
					
						
							|  |  |  |                 'repl': 'http://rsspod.rtp.pt/videocasts/', | 
					
						
							|  |  |  |                 'vcodec': 'h264', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         r = replacements[config['type']] | 
					
						
							|  |  |  |         if re.match(r['pattern'], config['file']) is not None: | 
					
						
							|  |  |  |             formats.append({ | 
					
						
							|  |  |  |                 'format_id': r['format_id'], | 
					
						
							|  |  |  |                 'url': re.sub(r['pattern'], r['repl'], config['file']), | 
					
						
							|  |  |  |                 'vcodec': r['vcodec'], | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self._sort_formats(formats) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-12 20:22:24 +02:00
										 |  |  |         return { | 
					
						
							|  |  |  |             'id': video_id, | 
					
						
							|  |  |  |             'title': title, | 
					
						
							|  |  |  |             'formats': formats, | 
					
						
							|  |  |  |             'description': description, | 
					
						
							|  |  |  |             'thumbnail': thumbnail, | 
					
						
							|  |  |  |         } |