[common] extract vbr, abr and fps for Unified Streaming Platform m3u8 manifests
[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 )
13
14
15 class WatIE(InfoExtractor):
16     _VALID_URL = r'(?:wat:|https?://(?:www\.)?wat\.tv/video/.*-)(?P<id>[0-9a-z]+)'
17     IE_NAME = 'wat.tv'
18     _TESTS = [
19         {
20             'url': 'http://www.wat.tv/video/soupe-figues-l-orange-aux-epices-6z1uz_2hvf7_.html',
21             'md5': '83d882d9de5c9d97f0bb2c6273cde56a',
22             'info_dict': {
23                 'id': '11713067',
24                 'ext': 'mp4',
25                 'title': 'Soupe de figues à l\'orange et aux épices',
26                 'description': 'Retrouvez l\'émission "Petits plats en équilibre", diffusée le 18 août 2014.',
27                 'upload_date': '20140819',
28                 'duration': 120,
29             },
30         },
31         {
32             'url': 'http://www.wat.tv/video/gregory-lemarchal-voix-ange-6z1v7_6ygkj_.html',
33             'md5': 'fbc84e4378165278e743956d9c1bf16b',
34             'info_dict': {
35                 'id': '11713075',
36                 'ext': 'mp4',
37                 'title': 'Grégory Lemarchal, une voix d\'ange depuis 10 ans (1/3)',
38                 'description': 'md5:b7a849cf16a2b733d9cd10c52906dee3',
39                 'upload_date': '20140816',
40                 'duration': 2910,
41             },
42             'skip': "Ce contenu n'est pas disponible pour l'instant.",
43         },
44     ]
45
46     def _real_extract(self, url):
47         video_id = self._match_id(url)
48         video_id = video_id if video_id.isdigit() and len(video_id) > 6 else compat_str(int(video_id, 36))
49
50         # 'contentv4' is used in the website, but it also returns the related
51         # videos, we don't need them
52         video_info = self._download_json(
53             'http://www.wat.tv/interface/contentv3/' + video_id, video_id)['media']
54
55         error_desc = video_info.get('error_desc')
56         if error_desc:
57             raise ExtractorError(
58                 '%s returned error: %s' % (self.IE_NAME, error_desc), expected=True)
59
60         chapters = video_info['chapters']
61         first_chapter = chapters[0]
62
63         def video_id_for_chapter(chapter):
64             return chapter['tc_start'].split('-')[0]
65
66         if video_id_for_chapter(first_chapter) != video_id:
67             self.to_screen('Multipart video detected')
68             entries = [self.url_result('wat:%s' % video_id_for_chapter(chapter)) for chapter in chapters]
69             return self.playlist_result(entries, video_id, video_info['title'])
70         # Otherwise we can continue and extract just one part, we have to use
71         # the video id for getting the video url
72
73         date_diffusion = first_chapter.get('date_diffusion')
74         upload_date = unified_strdate(date_diffusion) if date_diffusion else None
75
76         def extract_url(path_template, url_type):
77             req_url = 'http://www.wat.tv/get/%s' % (path_template % video_id)
78             head = self._request_webpage(HEADRequest(req_url), video_id, 'Extracting %s url' % url_type)
79             red_url = head.geturl()
80             if req_url == red_url:
81                 raise ExtractorError(
82                     '%s said: Sorry, this video is not available from your country.' % self.IE_NAME,
83                     expected=True)
84             return red_url
85
86         m3u8_url = extract_url('ipad/%s.m3u8', 'm3u8')
87         http_url = extract_url('android5/%s.mp4', 'http')
88
89         formats = []
90         m3u8_formats = self._extract_m3u8_formats(
91             m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls')
92         formats.extend(m3u8_formats)
93         formats.extend(self._extract_f4m_formats(
94             m3u8_url.replace('ios.', 'web.').replace('.m3u8', '.f4m'),
95             video_id, f4m_id='hds', fatal=False))
96         for m3u8_format in m3u8_formats:
97             vbr, abr = m3u8_format.get('vbr'), m3u8_format.get('abr')
98             if not vbr or not abr:
99                 continue
100             f = m3u8_format.copy()
101             f.update({
102                 'url': re.sub(r'%s-\d+00-\d+' % video_id, '%s-%d00-%d' % (video_id, round(vbr / 100), round(abr)), http_url),
103                 'format_id': f['format_id'].replace('hls', 'http'),
104                 'protocol': 'http',
105             })
106             formats.append(f)
107         self._sort_formats(formats)
108
109         return {
110             'id': video_id,
111             'title': first_chapter['title'],
112             'thumbnail': first_chapter['preview'],
113             'description': first_chapter['description'],
114             'view_count': video_info['views'],
115             'upload_date': upload_date,
116             'duration': video_info['files'][0]['duration'],
117             'formats': formats,
118         }