You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
5.5 KiB

10 years ago
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. HEADRequest,
  8. unified_strdate,
  9. url_basename,
  10. qualities,
  11. int_or_none,
  12. )
  13. class CanalplusIE(InfoExtractor):
  14. IE_DESC = 'canalplus.fr, piwiplus.fr and d8.tv'
  15. _VALID_URL = r'https?://(?:www\.(?P<site>canalplus\.fr|piwiplus\.fr|d8\.tv|itele\.fr)/.*?/(?P<path>.*)|player\.canalplus\.fr/#/(?P<id>[0-9]+))'
  16. _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
  17. _SITE_ID_MAP = {
  18. 'canalplus.fr': 'cplus',
  19. 'piwiplus.fr': 'teletoon',
  20. 'd8.tv': 'd8',
  21. 'itele.fr': 'itele',
  22. }
  23. _TESTS = [{
  24. 'url': 'http://www.canalplus.fr/c-emissions/pid1830-c-zapping.html?vid=1263092',
  25. 'md5': '12164a6f14ff6df8bd628e8ba9b10b78',
  26. 'info_dict': {
  27. 'id': '1263092',
  28. 'ext': 'mp4',
  29. 'title': 'Le Zapping - 13/05/15',
  30. 'description': 'md5:09738c0d06be4b5d06a0940edb0da73f',
  31. 'upload_date': '20150513',
  32. },
  33. }, {
  34. 'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
  35. 'info_dict': {
  36. 'id': '1108190',
  37. 'ext': 'flv',
  38. 'title': 'Le labyrinthe - Boing super ranger',
  39. 'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
  40. 'upload_date': '20140724',
  41. },
  42. 'skip': 'Only works from France',
  43. }, {
  44. 'url': 'http://www.d8.tv/d8-docs-mags/pid6589-d8-campagne-intime.html',
  45. 'info_dict': {
  46. 'id': '966289',
  47. 'ext': 'flv',
  48. 'title': 'Campagne intime - Documentaire exceptionnel',
  49. 'description': 'md5:d2643b799fb190846ae09c61e59a859f',
  50. 'upload_date': '20131108',
  51. },
  52. 'skip': 'videos get deleted after a while',
  53. }, {
  54. 'url': 'http://www.itele.fr/france/video/aubervilliers-un-lycee-en-colere-111559',
  55. 'md5': '38b8f7934def74f0d6f3ba6c036a5f82',
  56. 'info_dict': {
  57. 'id': '1213714',
  58. 'ext': 'mp4',
  59. 'title': 'Aubervilliers : un lycée en colère - Le 11/02/2015 à 06h45',
  60. 'description': 'md5:8216206ec53426ea6321321f3b3c16db',
  61. 'upload_date': '20150211',
  62. },
  63. }]
  64. def _real_extract(self, url):
  65. mobj = re.match(self._VALID_URL, url)
  66. video_id = mobj.groupdict().get('id')
  67. site_id = self._SITE_ID_MAP[mobj.group('site') or 'canal']
  68. # Beware, some subclasses do not define an id group
  69. display_id = url_basename(mobj.group('path'))
  70. if video_id is None:
  71. webpage = self._download_webpage(url, display_id)
  72. video_id = self._search_regex(
  73. [r'<canal:player[^>]+?videoId=(["\'])(?P<id>\d+)', r'id=["\']canal_video_player(?P<id>\d+)'],
  74. webpage, 'video id', group='id')
  75. info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
  76. video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
  77. if isinstance(video_data, list):
  78. video_data = [video for video in video_data if video.get('ID') == video_id][0]
  79. media = video_data['MEDIA']
  80. infos = video_data['INFOS']
  81. preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
  82. fmt_url = next(iter(media.get('VIDEOS')))
  83. if '/geo' in fmt_url.lower():
  84. response = self._request_webpage(
  85. HEADRequest(fmt_url), video_id,
  86. 'Checking if the video is georestricted')
  87. if '/blocage' in response.geturl():
  88. raise ExtractorError(
  89. 'The video is not available in your country',
  90. expected=True)
  91. formats = []
  92. for format_id, format_url in media['VIDEOS'].items():
  93. if not format_url:
  94. continue
  95. if format_id == 'HLS':
  96. formats.extend(self._extract_m3u8_formats(
  97. format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
  98. elif format_id == 'HDS':
  99. formats.extend(self._extract_f4m_formats(
  100. format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
  101. else:
  102. formats.append({
  103. # the secret extracted ya function in http://player.canalplus.fr/common/js/canalPlayer.js
  104. 'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
  105. 'format_id': format_id,
  106. 'preference': preference(format_id),
  107. })
  108. self._sort_formats(formats)
  109. thumbnails = [{
  110. 'id': image_id,
  111. 'url': image_url,
  112. } for image_id, image_url in media.get('images', {}).items()]
  113. titrage = infos['TITRAGE']
  114. return {
  115. 'id': video_id,
  116. 'display_id': display_id,
  117. 'title': '%s - %s' % (titrage['TITRE'],
  118. titrage['SOUS_TITRE']),
  119. 'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
  120. 'thumbnails': thumbnails,
  121. 'description': infos.get('DESCRIPTION'),
  122. 'duration': int_or_none(infos.get('DURATION')),
  123. 'view_count': int_or_none(infos.get('NB_VUES')),
  124. 'like_count': int_or_none(infos.get('NB_LIKES')),
  125. 'comment_count': int_or_none(infos.get('NB_COMMENTS')),
  126. 'formats': formats,
  127. }