[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / rtp.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     determine_ext,
7     js_to_json,
8 )
9
10
11 class RTPIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?'
13     _TESTS = [{
14         'url': 'http://www.rtp.pt/play/p405/e174042/paixoes-cruzadas',
15         'md5': 'e736ce0c665e459ddb818546220b4ef8',
16         'info_dict': {
17             'id': 'e174042',
18             'ext': 'mp3',
19             'title': 'Paixões Cruzadas',
20             'description': 'As paixões musicais de António Cartaxo e António Macedo',
21             'thumbnail': r're:^https?://.*\.jpg',
22         },
23     }, {
24         'url': 'http://www.rtp.pt/play/p831/a-quimica-das-coisas',
25         'only_matching': True,
26     }]
27
28     def _real_extract(self, url):
29         video_id = self._match_id(url)
30
31         webpage = self._download_webpage(url, video_id)
32         title = self._html_search_meta(
33             'twitter:title', webpage, display_name='title', fatal=True)
34
35         config = self._parse_json(self._search_regex(
36             r'(?s)RTPPlayer\(({.+?})\);', webpage,
37             'player config'), video_id, js_to_json)
38         file_url = config['file']
39         ext = determine_ext(file_url)
40         if ext == 'm3u8':
41             file_key = config.get('fileKey')
42             formats = self._extract_m3u8_formats(
43                 file_url, video_id, 'mp4', 'm3u8_native',
44                 m3u8_id='hls', fatal=file_key)
45             if file_key:
46                 formats.append({
47                     'url': 'https://cdn-ondemand.rtp.pt' + file_key,
48                     'preference': 1,
49                 })
50             self._sort_formats(formats)
51         else:
52             formats = [{
53                 'url': file_url,
54                 'ext': ext,
55             }]
56         if config.get('mediaType') == 'audio':
57             for f in formats:
58                 f['vcodec'] = 'none'
59
60         return {
61             'id': video_id,
62             'title': title,
63             'formats': formats,
64             'description': self._html_search_meta(['description', 'twitter:description'], webpage),
65             'thumbnail': config.get('poster') or self._og_search_thumbnail(webpage),
66         }