]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/wat.py
[tf1] try all supported adaptive urls
[yt-dlp.git] / youtube_dl / extractor / wat.py
CommitLineData
8244288d 1# coding: utf-8
e7916255 2from __future__ import unicode_literals
8244288d 3
99afb3dd
JMF
4import re
5
6from .common import InfoExtractor
c5f51551 7from ..compat import compat_str
86916dae
S
8from ..utils import (
9 ExtractorError,
10 unified_strdate,
c5f51551 11 HEADRequest,
57ce8a6d 12 int_or_none,
86916dae 13)
99afb3dd
JMF
14
15
16class WatIE(InfoExtractor):
c5f51551 17 _VALID_URL = r'(?:wat:|https?://(?:www\.)?wat\.tv/video/.*-)(?P<id>[0-9a-z]+)'
99afb3dd 18 IE_NAME = 'wat.tv'
c28df247
S
19 _TESTS = [
20 {
21 'url': 'http://www.wat.tv/video/soupe-figues-l-orange-aux-epices-6z1uz_2hvf7_.html',
c5f51551 22 'md5': '83d882d9de5c9d97f0bb2c6273cde56a',
c28df247
S
23 'info_dict': {
24 'id': '11713067',
c28df247
S
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',
57ce8a6d 34 'md5': '34bdfa5ca9fd3c7eb88601b635b0424c',
c28df247
S
35 'info_dict': {
36 'id': '11713075',
c28df247
S
37 'ext': 'mp4',
38 'title': 'Grégory Lemarchal, une voix d\'ange depuis 10 ans (1/3)',
c28df247 39 'upload_date': '20140816',
c28df247 40 },
57ce8a6d 41 'expected_warnings': ["Ce contenu n'est pas disponible pour l'instant."],
fa800269 42 },
c28df247 43 ]
e7916255 44
57ce8a6d
RA
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
99afb3dd 54 def _real_extract(self, url):
c5f51551 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))
8244288d 57
c5f51551 58 # 'contentv4' is used in the website, but it also returns the related
59 # videos, we don't need them
57ce8a6d
RA
60 video_data = self._download_json(
61 'http://www.wat.tv/interface/contentv4s/' + video_id, video_id)
62 video_info = video_data['media']
a54bda3a 63
86916dae
S
64 error_desc = video_info.get('error_desc')
65 if error_desc:
57ce8a6d
RA
66 self.report_warning(
67 '%s returned error: %s' % (self.IE_NAME, error_desc))
86916dae 68
8244288d 69 chapters = video_info['chapters']
57ce8a6d
RA
70 if chapters:
71 first_chapter = chapters[0]
99afb3dd 72
57ce8a6d
RA
73 def video_id_for_chapter(chapter):
74 return chapter['tc_start'].split('-')[0]
8244288d 75
57ce8a6d
RA
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
c5f51551 84
57ce8a6d 85 title = first_chapter['title']
c5f51551 86
87 def extract_url(path_template, url_type):
88 req_url = 'http://www.wat.tv/get/%s' % (path_template % video_id)
948cd5b7
RA
89 head = self._request_webpage(HEADRequest(req_url), video_id, 'Extracting %s url' % url_type, fatal=False)
90 if head:
91 red_url = head.geturl()
92 if req_url != red_url:
93 return red_url
94 return None
95
96 def remove_bitrate_limit(manifest_url):
97 return re.sub(r'(?:max|min)_bitrate=\d+&?', '', manifest_url)
c5f51551 98
c5f51551 99 formats = []
57ce8a6d 100 try:
948cd5b7
RA
101 manifest_urls = self._download_json(
102 'http://www.wat.tv/get/webhtml/' + video_id, video_id)
103 m3u8_url = manifest_urls.get('hls')
104 if m3u8_url:
105 m3u8_url = remove_bitrate_limit(m3u8_url)
106 m3u8_formats = self._extract_m3u8_formats(
107 m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
108 if m3u8_formats:
109 formats.extend(m3u8_formats)
110 formats.extend(self._extract_f4m_formats(
111 m3u8_url.replace('ios', 'web').replace('.m3u8', '.f4m'),
112 video_id, f4m_id='hds', fatal=False))
113 http_url = extract_url('android5/%s.mp4', 'http')
114 if http_url:
115 for m3u8_format in m3u8_formats:
116 vbr, abr = m3u8_format.get('vbr'), m3u8_format.get('abr')
117 if not vbr or not abr:
118 continue
119 format_id = m3u8_format['format_id'].replace('hls', 'http')
120 fmt_url = re.sub(r'%s-\d+00-\d+' % video_id, '%s-%d00-%d' % (video_id, round(vbr / 100), round(abr)), http_url)
121 if self._is_valid_url(fmt_url, video_id, format_id):
122 f = m3u8_format.copy()
123 f.update({
124 'url': fmt_url,
125 'format_id': format_id,
126 'protocol': 'http',
127 })
128 formats.append(f)
129 mpd_url = manifest_urls.get('mpd')
130 if mpd_url:
131 formats.extend(self._extract_mpd_formats(remove_bitrate_limit(
132 mpd_url), video_id, mpd_id='dash', fatal=False))
57ce8a6d
RA
133 self._sort_formats(formats)
134 except ExtractorError:
135 abr = 64
136 for vbr, width, height in self._FORMATS:
137 tbr = vbr + abr
138 format_id = 'http-%s' % tbr
139 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)
140 if self._is_valid_url(fmt_url, video_id, format_id):
141 formats.append({
142 'format_id': format_id,
143 'url': fmt_url,
144 'vbr': vbr,
145 'abr': abr,
146 'width': width,
147 'height': height,
148 })
149
150 date_diffusion = first_chapter.get('date_diffusion') or video_data.get('configv4', {}).get('estatS4')
151 upload_date = unified_strdate(date_diffusion) if date_diffusion else None
152 duration = None
153 files = video_info['files']
154 if files:
155 duration = int_or_none(files[0].get('duration'))
a54bda3a 156
e7916255 157 return {
c5f51551 158 'id': video_id,
57ce8a6d
RA
159 'title': title,
160 'thumbnail': first_chapter.get('preview'),
161 'description': first_chapter.get('description'),
162 'view_count': int_or_none(video_info.get('views')),
e7916255 163 'upload_date': upload_date,
57ce8a6d 164 'duration': duration,
a54bda3a 165 'formats': formats,
e7916255 166 }