wat.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import hashlib
  5. from .common import InfoExtractor
  6. from ..utils import unified_strdate
  7. class WatIE(InfoExtractor):
  8. _VALID_URL = r'http://www\.wat\.tv/video/(?P<display_id>.*)-(?P<short_id>.*?)_.*?\.html'
  9. IE_NAME = 'wat.tv'
  10. _TESTS = [
  11. {
  12. 'url': 'http://www.wat.tv/video/soupe-figues-l-orange-aux-epices-6z1uz_2hvf7_.html',
  13. 'md5': 'ce70e9223945ed26a8056d413ca55dc9',
  14. 'info_dict': {
  15. 'id': '11713067',
  16. 'display_id': 'soupe-figues-l-orange-aux-epices',
  17. 'ext': 'mp4',
  18. 'title': 'Soupe de figues à l\'orange et aux épices',
  19. 'description': 'Retrouvez l\'émission "Petits plats en équilibre", diffusée le 18 août 2014.',
  20. 'upload_date': '20140819',
  21. 'duration': 120,
  22. },
  23. },
  24. {
  25. 'url': 'http://www.wat.tv/video/gregory-lemarchal-voix-ange-6z1v7_6ygkj_.html',
  26. 'md5': 'fbc84e4378165278e743956d9c1bf16b',
  27. 'info_dict': {
  28. 'id': '11713075',
  29. 'display_id': 'gregory-lemarchal-voix-ange',
  30. 'ext': 'mp4',
  31. 'title': 'Grégory Lemarchal, une voix d\'ange depuis 10 ans (1/3)',
  32. 'description': 'md5:b7a849cf16a2b733d9cd10c52906dee3',
  33. 'upload_date': '20140816',
  34. 'duration': 2910,
  35. },
  36. },
  37. ]
  38. def download_video_info(self, real_id):
  39. # 'contentv4' is used in the website, but it also returns the related
  40. # videos, we don't need them
  41. info = self._download_json('http://www.wat.tv/interface/contentv3/' + real_id, real_id)
  42. return info['media']
  43. def _real_extract(self, url):
  44. def real_id_for_chapter(chapter):
  45. return chapter['tc_start'].split('-')[0]
  46. mobj = re.match(self._VALID_URL, url)
  47. short_id = mobj.group('short_id')
  48. display_id = mobj.group('display_id')
  49. webpage = self._download_webpage(url, display_id or short_id)
  50. real_id = self._search_regex(r'xtpage = ".*-(.*?)";', webpage, 'real id')
  51. video_info = self.download_video_info(real_id)
  52. geo_list = video_info.get('geoList')
  53. country = geo_list[0] if geo_list else ''
  54. chapters = video_info['chapters']
  55. first_chapter = chapters[0]
  56. files = video_info['files']
  57. first_file = files[0]
  58. if real_id_for_chapter(first_chapter) != real_id:
  59. self.to_screen('Multipart video detected')
  60. chapter_urls = []
  61. for chapter in chapters:
  62. chapter_id = real_id_for_chapter(chapter)
  63. # Yes, when we this chapter is processed by WatIE,
  64. # it will download the info again
  65. chapter_info = self.download_video_info(chapter_id)
  66. chapter_urls.append(chapter_info['url'])
  67. entries = [self.url_result(chapter_url) for chapter_url in chapter_urls]
  68. return self.playlist_result(entries, real_id, video_info['title'])
  69. upload_date = None
  70. if 'date_diffusion' in first_chapter:
  71. upload_date = unified_strdate(first_chapter['date_diffusion'])
  72. # Otherwise we can continue and extract just one part, we have to use
  73. # the short id for getting the video url
  74. formats = [{
  75. 'url': 'http://wat.tv/get/android5/%s.mp4' % real_id,
  76. 'format_id': 'Mobile',
  77. }]
  78. fmts = [('SD', 'web')]
  79. if first_file.get('hasHD'):
  80. fmts.append(('HD', 'webhd'))
  81. def compute_token(param):
  82. timestamp = '%08x' % int(self._download_webpage(
  83. 'http://www.wat.tv/servertime', real_id,
  84. 'Downloading server time').split('|')[0])
  85. magic = '9b673b13fa4682ed14c3cfa5af5310274b514c4133e9b3a81e6e3aba009l2564'
  86. return '%s/%s' % (hashlib.md5((magic + param + timestamp).encode('ascii')).hexdigest(), timestamp)
  87. for fmt in fmts:
  88. webid = '/%s/%s' % (fmt[1], real_id)
  89. video_url = self._download_webpage(
  90. 'http://www.wat.tv/get%s?token=%s&getURL=1&country=%s' % (webid, compute_token(webid), country),
  91. real_id,
  92. 'Downloding %s video URL' % fmt[0],
  93. 'Failed to download %s video URL' % fmt[0],
  94. False)
  95. if not video_url:
  96. continue
  97. formats.append({
  98. 'url': video_url,
  99. 'ext': 'mp4',
  100. 'format_id': fmt[0],
  101. })
  102. return {
  103. 'id': real_id,
  104. 'display_id': display_id,
  105. 'title': first_chapter['title'],
  106. 'thumbnail': first_chapter['preview'],
  107. 'description': first_chapter['description'],
  108. 'view_count': video_info['views'],
  109. 'upload_date': upload_date,
  110. 'duration': first_file['duration'],
  111. 'formats': formats,
  112. }