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.

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