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.

170 lines
6.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 ..compat import compat_urllib_parse_urlparse
  6. from ..utils import (
  7. ExtractorError,
  8. HEADRequest,
  9. unified_strdate,
  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'''(?x)
  16. https?://
  17. (?:
  18. (?:
  19. (?:(?:www|m)\.)?canalplus\.fr|
  20. (?:www\.)?piwiplus\.fr|
  21. (?:www\.)?d8\.tv|
  22. (?:www\.)?c8\.fr|
  23. (?:www\.)?d17\.tv|
  24. (?:www\.)?itele\.fr
  25. )/(?:(?:[^/]+/)*(?P<display_id>[^/?#&]+))?(?:\?.*\bvid=(?P<vid>\d+))?|
  26. player\.canalplus\.fr/#/(?P<id>\d+)
  27. )
  28. '''
  29. _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
  30. _SITE_ID_MAP = {
  31. 'canalplus': 'cplus',
  32. 'piwiplus': 'teletoon',
  33. 'd8': 'd8',
  34. 'c8': 'd8',
  35. 'd17': 'd17',
  36. 'itele': 'itele',
  37. }
  38. _TESTS = [{
  39. 'url': 'http://www.canalplus.fr/c-emissions/pid1830-c-zapping.html?vid=1192814',
  40. 'md5': '41f438a4904f7664b91b4ed0dec969dc',
  41. 'info_dict': {
  42. 'id': '1192814',
  43. 'ext': 'mp4',
  44. 'title': "L'Année du Zapping 2014 - L'Année du Zapping 2014",
  45. 'description': "Toute l'année 2014 dans un Zapping exceptionnel !",
  46. 'upload_date': '20150105',
  47. },
  48. }, {
  49. 'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
  50. 'info_dict': {
  51. 'id': '1108190',
  52. 'ext': 'flv',
  53. 'title': 'Le labyrinthe - Boing super ranger',
  54. 'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
  55. 'upload_date': '20140724',
  56. },
  57. 'skip': 'Only works from France',
  58. }, {
  59. 'url': 'http://www.d8.tv/d8-docs-mags/pid5198-d8-en-quete-d-actualite.html?vid=1390231',
  60. 'info_dict': {
  61. 'id': '1390231',
  62. 'ext': 'mp4',
  63. 'title': "Vacances pas chères : prix discount ou grosses dépenses ? - En quête d'actualité",
  64. 'description': 'md5:edb6cf1cb4a1e807b5dd089e1ac8bfc6',
  65. 'upload_date': '20160512',
  66. },
  67. 'params': {
  68. 'skip_download': True,
  69. },
  70. }, {
  71. 'url': 'http://www.itele.fr/chroniques/invite-bruce-toussaint/thierry-solere-nicolas-sarkozy-officialisera-sa-candidature-a-la-primaire-quand-il-le-voudra-167224',
  72. 'info_dict': {
  73. 'id': '1398334',
  74. 'ext': 'mp4',
  75. 'title': "L'invité de Bruce Toussaint du 07/06/2016 - ",
  76. 'description': 'md5:40ac7c9ad0feaeb6f605bad986f61324',
  77. 'upload_date': '20160607',
  78. },
  79. 'params': {
  80. 'skip_download': True,
  81. },
  82. }, {
  83. 'url': 'http://m.canalplus.fr/?vid=1398231',
  84. 'only_matching': True,
  85. }, {
  86. 'url': 'http://www.d17.tv/emissions/pid8303-lolywood.html?vid=1397061',
  87. 'only_matching': True,
  88. }]
  89. def _real_extract(self, url):
  90. mobj = re.match(self._VALID_URL, url)
  91. video_id = mobj.groupdict().get('id') or mobj.groupdict().get('vid')
  92. site_id = self._SITE_ID_MAP[compat_urllib_parse_urlparse(url).netloc.rsplit('.', 2)[-2]]
  93. # Beware, some subclasses do not define an id group
  94. display_id = mobj.group('display_id') or video_id
  95. if video_id is None:
  96. webpage = self._download_webpage(url, display_id)
  97. video_id = self._search_regex(
  98. [r'<canal:player[^>]+?videoId=(["\'])(?P<id>\d+)', r'id=["\']canal_video_player(?P<id>\d+)'],
  99. webpage, 'video id', group='id')
  100. info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
  101. video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
  102. if isinstance(video_data, list):
  103. video_data = [video for video in video_data if video.get('ID') == video_id][0]
  104. media = video_data['MEDIA']
  105. infos = video_data['INFOS']
  106. preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
  107. fmt_url = next(iter(media.get('VIDEOS')))
  108. if '/geo' in fmt_url.lower():
  109. response = self._request_webpage(
  110. HEADRequest(fmt_url), video_id,
  111. 'Checking if the video is georestricted')
  112. if '/blocage' in response.geturl():
  113. raise ExtractorError(
  114. 'The video is not available in your country',
  115. expected=True)
  116. formats = []
  117. for format_id, format_url in media['VIDEOS'].items():
  118. if not format_url:
  119. continue
  120. if format_id == 'HLS':
  121. formats.extend(self._extract_m3u8_formats(
  122. format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
  123. elif format_id == 'HDS':
  124. formats.extend(self._extract_f4m_formats(
  125. format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
  126. else:
  127. formats.append({
  128. # the secret extracted ya function in http://player.canalplus.fr/common/js/canalPlayer.js
  129. 'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
  130. 'format_id': format_id,
  131. 'preference': preference(format_id),
  132. })
  133. self._sort_formats(formats)
  134. thumbnails = [{
  135. 'id': image_id,
  136. 'url': image_url,
  137. } for image_id, image_url in media.get('images', {}).items()]
  138. titrage = infos['TITRAGE']
  139. return {
  140. 'id': video_id,
  141. 'display_id': display_id,
  142. 'title': '%s - %s' % (titrage['TITRE'],
  143. titrage['SOUS_TITRE']),
  144. 'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
  145. 'thumbnails': thumbnails,
  146. 'description': infos.get('DESCRIPTION'),
  147. 'duration': int_or_none(infos.get('DURATION')),
  148. 'view_count': int_or_none(infos.get('NB_VUES')),
  149. 'like_count': int_or_none(infos.get('NB_LIKES')),
  150. 'comment_count': int_or_none(infos.get('NB_COMMENTS')),
  151. 'formats': formats,
  152. }