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.

186 lines
7.4 KiB

10 years ago
  1. # coding: 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. dict_get,
  8. ExtractorError,
  9. HEADRequest,
  10. int_or_none,
  11. qualities,
  12. remove_end,
  13. unified_strdate,
  14. )
  15. class CanalplusIE(InfoExtractor):
  16. IE_DESC = 'canalplus.fr, piwiplus.fr and d8.tv'
  17. _VALID_URL = r'''(?x)
  18. https?://
  19. (?:
  20. (?:
  21. (?:(?:www|m)\.)?canalplus\.fr|
  22. (?:www\.)?piwiplus\.fr|
  23. (?:www\.)?d8\.tv|
  24. (?:www\.)?c8\.fr|
  25. (?:www\.)?d17\.tv|
  26. (?:(?:football|www)\.)?cstar\.fr|
  27. (?:www\.)?itele\.fr
  28. )/(?:(?:[^/]+/)*(?P<display_id>[^/?#&]+))?(?:\?.*\bvid=(?P<vid>\d+))?|
  29. player\.canalplus\.fr/#/(?P<id>\d+)
  30. )
  31. '''
  32. _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
  33. _SITE_ID_MAP = {
  34. 'canalplus': 'cplus',
  35. 'piwiplus': 'teletoon',
  36. 'd8': 'd8',
  37. 'c8': 'd8',
  38. 'd17': 'd17',
  39. 'cstar': 'd17',
  40. 'itele': 'itele',
  41. }
  42. _TESTS = [{
  43. 'url': 'http://www.canalplus.fr/c-emissions/pid1830-c-zapping.html?vid=1192814',
  44. 'info_dict': {
  45. 'id': '1405510',
  46. 'display_id': 'pid1830-c-zapping',
  47. 'ext': 'mp4',
  48. 'title': 'Zapping - 02/07/2016',
  49. 'description': 'Le meilleur de toutes les chaînes, tous les jours',
  50. 'upload_date': '20160702',
  51. },
  52. }, {
  53. 'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
  54. 'info_dict': {
  55. 'id': '1108190',
  56. 'display_id': 'pid1405-le-labyrinthe-boing-super-ranger',
  57. 'ext': 'mp4',
  58. 'title': 'BOING SUPER RANGER - Ep : Le labyrinthe',
  59. 'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
  60. 'upload_date': '20140724',
  61. },
  62. 'skip': 'Only works from France',
  63. }, {
  64. 'url': 'http://www.c8.fr/c8-divertissement/ms-touche-pas-a-mon-poste/pid6318-videos-integrales.html',
  65. 'md5': '4b47b12b4ee43002626b97fad8fb1de5',
  66. 'info_dict': {
  67. 'id': '1420213',
  68. 'display_id': 'pid6318-videos-integrales',
  69. 'ext': 'mp4',
  70. 'title': 'TPMP ! Même le matin - Les 35H de Baba - 14/10/2016',
  71. 'description': 'md5:f96736c1b0ffaa96fd5b9e60ad871799',
  72. 'upload_date': '20161014',
  73. },
  74. 'skip': 'Only works from France',
  75. }, {
  76. 'url': 'http://www.itele.fr/chroniques/invite-michael-darmon/rachida-dati-nicolas-sarkozy-est-le-plus-en-phase-avec-les-inquietudes-des-francais-171510',
  77. 'info_dict': {
  78. 'id': '1420176',
  79. 'display_id': 'rachida-dati-nicolas-sarkozy-est-le-plus-en-phase-avec-les-inquietudes-des-francais-171510',
  80. 'ext': 'mp4',
  81. 'title': 'L\'invité de Michaël Darmon du 14/10/2016 - ',
  82. 'description': 'Chaque matin du lundi au vendredi, Michaël Darmon reçoit un invité politique à 8h25.',
  83. 'upload_date': '20161014',
  84. },
  85. }, {
  86. 'url': 'http://football.cstar.fr/cstar-minisite-foot/pid7566-feminines-videos.html?vid=1416769',
  87. 'info_dict': {
  88. 'id': '1416769',
  89. 'display_id': 'pid7566-feminines-videos',
  90. 'ext': 'mp4',
  91. 'title': 'France - Albanie : les temps forts de la soirée - 20/09/2016',
  92. 'description': 'md5:c3f30f2aaac294c1c969b3294de6904e',
  93. 'upload_date': '20160921',
  94. },
  95. 'params': {
  96. 'skip_download': True,
  97. },
  98. }, {
  99. 'url': 'http://m.canalplus.fr/?vid=1398231',
  100. 'only_matching': True,
  101. }, {
  102. 'url': 'http://www.d17.tv/emissions/pid8303-lolywood.html?vid=1397061',
  103. 'only_matching': True,
  104. }]
  105. def _real_extract(self, url):
  106. mobj = re.match(self._VALID_URL, url)
  107. site_id = self._SITE_ID_MAP[compat_urllib_parse_urlparse(url).netloc.rsplit('.', 2)[-2]]
  108. # Beware, some subclasses do not define an id group
  109. display_id = remove_end(dict_get(mobj.groupdict(), ('display_id', 'id', 'vid')), '.html')
  110. webpage = self._download_webpage(url, display_id)
  111. video_id = self._search_regex(
  112. [r'<canal:player[^>]+?videoId=(["\'])(?P<id>\d+)',
  113. r'id=["\']canal_video_player(?P<id>\d+)',
  114. r'data-video=["\'](?P<id>\d+)'],
  115. webpage, 'video id', default=mobj.group('vid'), group='id')
  116. info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
  117. video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
  118. if isinstance(video_data, list):
  119. video_data = [video for video in video_data if video.get('ID') == video_id][0]
  120. media = video_data['MEDIA']
  121. infos = video_data['INFOS']
  122. preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
  123. fmt_url = next(iter(media.get('VIDEOS')))
  124. if '/geo' in fmt_url.lower():
  125. response = self._request_webpage(
  126. HEADRequest(fmt_url), video_id,
  127. 'Checking if the video is georestricted')
  128. if '/blocage' in response.geturl():
  129. raise ExtractorError(
  130. 'The video is not available in your country',
  131. expected=True)
  132. formats = []
  133. for format_id, format_url in media['VIDEOS'].items():
  134. if not format_url:
  135. continue
  136. if format_id == 'HLS':
  137. formats.extend(self._extract_m3u8_formats(
  138. format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
  139. elif format_id == 'HDS':
  140. formats.extend(self._extract_f4m_formats(
  141. format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
  142. else:
  143. formats.append({
  144. # the secret extracted ya function in http://player.canalplus.fr/common/js/canalPlayer.js
  145. 'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
  146. 'format_id': format_id,
  147. 'preference': preference(format_id),
  148. })
  149. self._sort_formats(formats)
  150. thumbnails = [{
  151. 'id': image_id,
  152. 'url': image_url,
  153. } for image_id, image_url in media.get('images', {}).items()]
  154. titrage = infos['TITRAGE']
  155. return {
  156. 'id': video_id,
  157. 'display_id': display_id,
  158. 'title': '%s - %s' % (titrage['TITRE'],
  159. titrage['SOUS_TITRE']),
  160. 'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
  161. 'thumbnails': thumbnails,
  162. 'description': infos.get('DESCRIPTION'),
  163. 'duration': int_or_none(infos.get('DURATION')),
  164. 'view_count': int_or_none(infos.get('NB_VUES')),
  165. 'like_count': int_or_none(infos.get('NB_LIKES')),
  166. 'comment_count': int_or_none(infos.get('NB_COMMENTS')),
  167. 'formats': formats,
  168. }