canalplus.py 6.3 KB

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