[tv5mondeplus] Add new extractor(closes #11386)
[youtube-dl] / youtube_dl / extractor / tv5mondeplus.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     extract_attributes,
8     get_element_by_class,
9     int_or_none,
10     parse_duration,
11     parse_iso8601,
12 )
13
14
15 class TV5MondePlusIE(InfoExtractor):
16     IE_DESC = 'TV5MONDE+'
17     _VALID_URL = r'https?://(?:www\.)?tv5mondeplus\.com/toutes-les-videos/[^/]+/(?P<id>[^/?#]+)'
18     _TEST = {
19         'url': 'http://www.tv5mondeplus.com/toutes-les-videos/documentaire/tdah-mon-amour-tele-quebec-tdah-mon-amour-ep001-enfants',
20         'md5': '12130fc199f020673138a83466542ec6',
21         'info_dict': {
22             'id': '0a774110-dc60-4037-f769-996439514f1f',
23             'ext': 'mp4',
24             'title': 'Tdah, mon amour - Enfants',
25             'description': 'md5:b65f0cc50e46947e62e5d352e9916cc4',
26             'upload_date': '20170401',
27             'timestamp': 1491022860,
28         }
29     }
30     _GEO_BYPASS = False
31
32     def _real_extract(self, url):
33         display_id = self._match_id(url)
34         webpage = self._download_webpage(url, display_id)
35
36         if ">Ce programme n'est malheureusement pas disponible pour votre zone géographique.<" in webpage:
37             self.raise_geo_restricted(countries=['FR'])
38
39         series = get_element_by_class('video-detail__title', webpage)
40         title = episode = get_element_by_class(
41             'video-detail__subtitle', webpage) or series
42         if series and series != title:
43             title = '%s - %s' % (series, title)
44         vpl_data = extract_attributes(self._search_regex(
45             r'(<[^>]+class="video_player_loader"[^>]+>)',
46             webpage, 'video player loader'))
47
48         video_files = self._parse_json(
49             vpl_data['data-broadcast'], display_id).get('files', [])
50         formats = []
51         for video_file in video_files:
52             v_url = video_file.get('url')
53             if not v_url:
54                 continue
55             video_format = video_file.get('format') or determine_ext(v_url)
56             if video_format == 'm3u8':
57                 formats.extend(self._extract_m3u8_formats(
58                     v_url, display_id, 'mp4', 'm3u8_native',
59                     m3u8_id='hls', fatal=False))
60             else:
61                 formats.append({
62                     'url': v_url,
63                     'format_id': video_format,
64                 })
65         self._sort_formats(formats)
66
67         return {
68             'id': vpl_data.get('data-guid') or display_id,
69             'display_id': display_id,
70             'title': title,
71             'description': get_element_by_class('video-detail__description', webpage),
72             'thumbnail': vpl_data.get('data-image'),
73             'duration': int_or_none(vpl_data.get('data-duration')) or parse_duration(self._html_search_meta('duration', webpage)),
74             'timestamp': parse_iso8601(self._html_search_meta('uploadDate', webpage)),
75             'formats': formats,
76             'episode': episode,
77             'series': series,
78         }