wat.py 6.0 KB

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