[DailyMail] Improve title and description extraction
[youtube-dl] / youtube_dl / extractor / dailymail.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     int_or_none,
7     determine_protocol,
8     unescapeHTML,
9 )
10
11
12 class DailyMailIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?dailymail\.co\.uk/video/[^/]+/video-(?P<id>[0-9]+)'
14     _TEST = {
15         'url': 'http://www.dailymail.co.uk/video/tvshowbiz/video-1295863/The-Mountain-appears-sparkling-water-ad-Heavy-Bubbles.html',
16         'md5': 'f6129624562251f628296c3a9ffde124',
17         'info_dict': {
18             'id': '1295863',
19             'ext': 'mp4',
20             'title': 'The Mountain appears in sparkling water ad for \'Heavy Bubbles\'',
21             'description': 'md5:a93d74b6da172dd5dc4d973e0b766a84',
22         }
23     }
24
25     def _real_extract(self, url):
26         video_id = self._match_id(url)
27         webpage = self._download_webpage(url, video_id)
28         video_data = self._parse_json(self._search_regex(
29             r"data-opts='({.+?})'", webpage, 'video data'), video_id)
30         title = unescapeHTML(video_data['title'])
31         video_sources = self._download_json(video_data.get(
32             'sources', {}).get('url') or 'http://www.dailymail.co.uk/api/player/%s/video-sources.json' % video_id, video_id)
33
34         formats = []
35         for rendition in video_sources['renditions']:
36             rendition_url = rendition.get('url')
37             if not rendition_url:
38                 continue
39             tbr = int_or_none(rendition.get('encodingRate'), 1000)
40             container = rendition.get('videoContainer')
41             is_hls = container == 'M2TS'
42             protocol = 'm3u8_native' if is_hls else determine_protocol({'url': rendition_url})
43             formats.append({
44                 'format_id': ('hls' if is_hls else protocol) + ('-%d' % tbr if tbr else ''),
45                 'url': rendition_url,
46                 'width': int_or_none(rendition.get('frameWidth')),
47                 'height': int_or_none(rendition.get('frameHeight')),
48                 'tbr': tbr,
49                 'vcodec': rendition.get('videoCodec'),
50                 'container': container,
51                 'protocol': protocol,
52                 'ext': 'mp4' if is_hls else None,
53             })
54         self._sort_formats(formats)
55
56         return {
57             'id': video_id,
58             'title': title,
59             'description': unescapeHTML(video_data.get('descr')),
60             'thumbnail': video_data.get('poster') or video_data.get('thumbnail'),
61             'formats': formats,
62         }