wat.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. unified_strdate,
  7. HEADRequest,
  8. int_or_none,
  9. )
  10. class WatIE(InfoExtractor):
  11. _VALID_URL = r'(?:wat:|https?://(?:www\.)?wat\.tv/video/.*-)(?P<id>[0-9a-z]+)'
  12. IE_NAME = 'wat.tv'
  13. _TESTS = [
  14. {
  15. 'url': 'http://www.wat.tv/video/soupe-figues-l-orange-aux-epices-6z1uz_2hvf7_.html',
  16. 'info_dict': {
  17. 'id': '11713067',
  18. 'ext': 'mp4',
  19. 'title': 'Soupe de figues à l\'orange et aux épices',
  20. 'description': 'Retrouvez l\'émission "Petits plats en équilibre", diffusée le 18 août 2014.',
  21. 'upload_date': '20140819',
  22. 'duration': 120,
  23. },
  24. 'params': {
  25. # m3u8 download
  26. 'skip_download': True,
  27. },
  28. 'expected_warnings': ['HTTP Error 404'],
  29. },
  30. {
  31. 'url': 'http://www.wat.tv/video/gregory-lemarchal-voix-ange-6z1v7_6ygkj_.html',
  32. 'md5': 'b16574df2c3cd1a36ca0098f2a791925',
  33. 'info_dict': {
  34. 'id': '11713075',
  35. 'ext': 'mp4',
  36. 'title': 'Grégory Lemarchal, une voix d\'ange depuis 10 ans (1/3)',
  37. 'upload_date': '20140816',
  38. },
  39. 'expected_warnings': ["Ce contenu n'est pas disponible pour l'instant."],
  40. },
  41. ]
  42. _FORMATS = (
  43. (200, 416, 234),
  44. (400, 480, 270),
  45. (600, 640, 360),
  46. (1200, 640, 360),
  47. (1800, 960, 540),
  48. (2500, 1280, 720),
  49. )
  50. def _real_extract(self, url):
  51. video_id = self._match_id(url)
  52. video_id = video_id if video_id.isdigit() and len(video_id) > 6 else compat_str(int(video_id, 36))
  53. # 'contentv4' is used in the website, but it also returns the related
  54. # videos, we don't need them
  55. video_data = self._download_json(
  56. 'http://www.wat.tv/interface/contentv4s/' + video_id, video_id)
  57. video_info = video_data['media']
  58. error_desc = video_info.get('error_desc')
  59. if error_desc:
  60. self.report_warning(
  61. '%s returned error: %s' % (self.IE_NAME, error_desc))
  62. chapters = video_info['chapters']
  63. if chapters:
  64. first_chapter = chapters[0]
  65. def video_id_for_chapter(chapter):
  66. return chapter['tc_start'].split('-')[0]
  67. if video_id_for_chapter(first_chapter) != video_id:
  68. self.to_screen('Multipart video detected')
  69. entries = [self.url_result('wat:%s' % video_id_for_chapter(chapter)) for chapter in chapters]
  70. return self.playlist_result(entries, video_id, video_info['title'])
  71. # Otherwise we can continue and extract just one part, we have to use
  72. # the video id for getting the video url
  73. else:
  74. first_chapter = video_info
  75. title = first_chapter['title']
  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, fatal=False)
  79. if head:
  80. red_url = head.geturl()
  81. if req_url != red_url:
  82. return red_url
  83. return None
  84. formats = []
  85. manifest_urls = self._download_json(
  86. 'http://www.wat.tv/get/webhtml/' + video_id, video_id)
  87. m3u8_url = manifest_urls.get('hls')
  88. if m3u8_url:
  89. formats.extend(self._extract_m3u8_formats(
  90. m3u8_url, video_id, 'mp4',
  91. 'm3u8_native', m3u8_id='hls', fatal=False))
  92. mpd_url = manifest_urls.get('mpd')
  93. if mpd_url:
  94. formats.extend(self._extract_mpd_formats(
  95. mpd_url.replace('://das-q1.tf1.fr/', '://das-q1-ssl.tf1.fr/'),
  96. video_id, mpd_id='dash', fatal=False))
  97. self._sort_formats(formats)
  98. date_diffusion = first_chapter.get('date_diffusion') or video_data.get('configv4', {}).get('estatS4')
  99. upload_date = unified_strdate(date_diffusion) if date_diffusion else None
  100. duration = None
  101. files = video_info['files']
  102. if files:
  103. duration = int_or_none(files[0].get('duration'))
  104. return {
  105. 'id': video_id,
  106. 'title': title,
  107. 'thumbnail': first_chapter.get('preview'),
  108. 'description': first_chapter.get('description'),
  109. 'view_count': int_or_none(video_info.get('views')),
  110. 'upload_date': upload_date,
  111. 'duration': duration,
  112. 'formats': formats,
  113. }