[polskieradio] Add support for downloading whole programmes.
[youtube-dl] / youtube_dl / extractor / wat.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     ExtractorError,
10     unified_strdate,
11     HEADRequest,
12     int_or_none,
13 )
14
15
16 class WatIE(InfoExtractor):
17     _VALID_URL = r'(?:wat:|https?://(?:www\.)?wat\.tv/video/.*-)(?P<id>[0-9a-z]+)'
18     IE_NAME = 'wat.tv'
19     _TESTS = [
20         {
21             'url': 'http://www.wat.tv/video/soupe-figues-l-orange-aux-epices-6z1uz_2hvf7_.html',
22             'md5': '83d882d9de5c9d97f0bb2c6273cde56a',
23             'info_dict': {
24                 'id': '11713067',
25                 'ext': 'mp4',
26                 'title': 'Soupe de figues à l\'orange et aux épices',
27                 'description': 'Retrouvez l\'émission "Petits plats en équilibre", diffusée le 18 août 2014.',
28                 'upload_date': '20140819',
29                 'duration': 120,
30             },
31         },
32         {
33             'url': 'http://www.wat.tv/video/gregory-lemarchal-voix-ange-6z1v7_6ygkj_.html',
34             'md5': '34bdfa5ca9fd3c7eb88601b635b0424c',
35             'info_dict': {
36                 'id': '11713075',
37                 'ext': 'mp4',
38                 'title': 'Grégory Lemarchal, une voix d\'ange depuis 10 ans (1/3)',
39                 'upload_date': '20140816',
40             },
41             'expected_warnings': ["Ce contenu n'est pas disponible pour l'instant."],
42         },
43     ]
44
45     _FORMATS = (
46         (200, 416, 234),
47         (400, 480, 270),
48         (600, 640, 360),
49         (1200, 640, 360),
50         (1800, 960, 540),
51         (2500, 1280, 720),
52     )
53
54     def _real_extract(self, url):
55         video_id = self._match_id(url)
56         video_id = video_id if video_id.isdigit() and len(video_id) > 6 else compat_str(int(video_id, 36))
57
58         # 'contentv4' is used in the website, but it also returns the related
59         # videos, we don't need them
60         video_data = self._download_json(
61             'http://www.wat.tv/interface/contentv4s/' + video_id, video_id)
62         video_info = video_data['media']
63
64         error_desc = video_info.get('error_desc')
65         if error_desc:
66             self.report_warning(
67                 '%s returned error: %s' % (self.IE_NAME, error_desc))
68
69         chapters = video_info['chapters']
70         if chapters:
71             first_chapter = chapters[0]
72
73             def video_id_for_chapter(chapter):
74                 return chapter['tc_start'].split('-')[0]
75
76             if video_id_for_chapter(first_chapter) != video_id:
77                 self.to_screen('Multipart video detected')
78                 entries = [self.url_result('wat:%s' % video_id_for_chapter(chapter)) for chapter in chapters]
79                 return self.playlist_result(entries, video_id, video_info['title'])
80             # Otherwise we can continue and extract just one part, we have to use
81             # the video id for getting the video url
82         else:
83             first_chapter = video_info
84
85         title = first_chapter['title']
86
87         def extract_url(path_template, url_type):
88             req_url = 'http://www.wat.tv/get/%s' % (path_template % video_id)
89             head = self._request_webpage(HEADRequest(req_url), video_id, 'Extracting %s url' % url_type)
90             red_url = head.geturl()
91             if req_url == red_url:
92                 raise ExtractorError(
93                     '%s said: Sorry, this video is not available from your country.' % self.IE_NAME,
94                     expected=True)
95             return red_url
96
97         formats = []
98         try:
99             http_url = extract_url('android5/%s.mp4', 'http')
100             m3u8_url = extract_url('ipad/%s.m3u8', 'm3u8')
101             m3u8_formats = self._extract_m3u8_formats(
102                 m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls')
103             formats.extend(m3u8_formats)
104             formats.extend(self._extract_f4m_formats(
105                 m3u8_url.replace('ios.', 'web.').replace('.m3u8', '.f4m'),
106                 video_id, f4m_id='hds', fatal=False))
107             for m3u8_format in m3u8_formats:
108                 vbr, abr = m3u8_format.get('vbr'), m3u8_format.get('abr')
109                 if not vbr or not abr:
110                     continue
111                 format_id = m3u8_format['format_id'].replace('hls', 'http')
112                 fmt_url = re.sub(r'%s-\d+00-\d+' % video_id, '%s-%d00-%d' % (video_id, round(vbr / 100), round(abr)), http_url)
113                 if self._is_valid_url(fmt_url, video_id, format_id):
114                     f = m3u8_format.copy()
115                     f.update({
116                         'url': fmt_url,
117                         'format_id': format_id,
118                         'protocol': 'http',
119                     })
120                     formats.append(f)
121             self._sort_formats(formats)
122         except ExtractorError:
123             abr = 64
124             for vbr, width, height in self._FORMATS:
125                 tbr = vbr + abr
126                 format_id = 'http-%s' % tbr
127                 fmt_url = 'http://dnl.adv.tf1.fr/2/USP-0x0/%s/%s/%s/ssm/%s-%s-64k.mp4' % (video_id[-4:-2], video_id[-2:], video_id, video_id, vbr)
128                 if self._is_valid_url(fmt_url, video_id, format_id):
129                     formats.append({
130                         'format_id': format_id,
131                         'url': fmt_url,
132                         'vbr': vbr,
133                         'abr': abr,
134                         'width': width,
135                         'height': height,
136                     })
137
138         date_diffusion = first_chapter.get('date_diffusion') or video_data.get('configv4', {}).get('estatS4')
139         upload_date = unified_strdate(date_diffusion) if date_diffusion else None
140         duration = None
141         files = video_info['files']
142         if files:
143             duration = int_or_none(files[0].get('duration'))
144
145         return {
146             'id': video_id,
147             'title': title,
148             'thumbnail': first_chapter.get('preview'),
149             'description': first_chapter.get('description'),
150             'view_count': int_or_none(video_info.get('views')),
151             'upload_date': upload_date,
152             'duration': duration,
153             'formats': formats,
154         }