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.

127 lines
4.8 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. )
  12. class CanalplusIE(InfoExtractor):
  13. IE_DESC = 'canalplus.fr, piwiplus.fr and d8.tv'
  14. _VALID_URL = r'https?://(?:www\.(?P<site>canalplus\.fr|piwiplus\.fr|d8\.tv)/.*?/(?P<path>.*)|player\.canalplus\.fr/#/(?P<id>[0-9]+))'
  15. _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s'
  16. _SITE_ID_MAP = {
  17. 'canalplus.fr': 'cplus',
  18. 'piwiplus.fr': 'teletoon',
  19. 'd8.tv': 'd8',
  20. }
  21. _TESTS = [{
  22. 'url': 'http://www.canalplus.fr/c-infos-documentaires/pid1830-c-zapping.html?vid=922470',
  23. 'md5': '3db39fb48b9685438ecf33a1078023e4',
  24. 'info_dict': {
  25. 'id': '922470',
  26. 'ext': 'flv',
  27. 'title': 'Zapping - 26/08/13',
  28. 'description': 'Le meilleur de toutes les chaînes, tous les jours.\nEmission du 26 août 2013',
  29. 'upload_date': '20130826',
  30. },
  31. }, {
  32. 'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
  33. 'info_dict': {
  34. 'id': '1108190',
  35. 'ext': 'flv',
  36. 'title': 'Le labyrinthe - Boing super ranger',
  37. 'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
  38. 'upload_date': '20140724',
  39. },
  40. 'skip': 'Only works from France',
  41. }, {
  42. 'url': 'http://www.d8.tv/d8-docs-mags/pid6589-d8-campagne-intime.html',
  43. 'info_dict': {
  44. 'id': '966289',
  45. 'ext': 'flv',
  46. 'title': 'Campagne intime - Documentaire exceptionnel',
  47. 'description': 'md5:d2643b799fb190846ae09c61e59a859f',
  48. 'upload_date': '20131108',
  49. },
  50. 'skip': 'videos get deleted after a while',
  51. }]
  52. def _real_extract(self, url):
  53. mobj = re.match(self._VALID_URL, url)
  54. video_id = mobj.groupdict().get('id')
  55. site_id = self._SITE_ID_MAP[mobj.group('site') or 'canal']
  56. # Beware, some subclasses do not define an id group
  57. display_id = url_basename(mobj.group('path'))
  58. if video_id is None:
  59. webpage = self._download_webpage(url, display_id)
  60. video_id = self._search_regex(
  61. r'<canal:player[^>]+?videoId="(\d+)"', webpage, 'video id')
  62. info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
  63. doc = self._download_xml(info_url, video_id, 'Downloading video XML')
  64. video_info = [video for video in doc if video.find('ID').text == video_id][0]
  65. media = video_info.find('MEDIA')
  66. infos = video_info.find('INFOS')
  67. preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD', 'HLS', 'HDS'])
  68. fmt_url = next(iter(media.find('VIDEOS'))).text
  69. if '/geo' in fmt_url.lower():
  70. response = self._request_webpage(
  71. HEADRequest(fmt_url), video_id,
  72. 'Checking if the video is georestricted')
  73. if '/blocage' in response.geturl():
  74. raise ExtractorError(
  75. 'The video is not available in your country',
  76. expected=True)
  77. formats = []
  78. for fmt in media.find('VIDEOS'):
  79. format_url = fmt.text
  80. if not format_url:
  81. continue
  82. format_id = fmt.tag
  83. if format_id == 'HLS':
  84. hls_formats = self._extract_m3u8_formats(format_url, video_id, 'flv')
  85. for fmt in hls_formats:
  86. fmt['preference'] = preference(format_id)
  87. formats.extend(hls_formats)
  88. elif format_id == 'HDS':
  89. hds_formats = self._extract_f4m_formats(format_url + '?hdcore=2.11.3', video_id)
  90. for fmt in hds_formats:
  91. fmt['preference'] = preference(format_id)
  92. formats.extend(hds_formats)
  93. else:
  94. formats.append({
  95. 'url': format_url,
  96. 'format_id': format_id,
  97. 'preference': preference(format_id),
  98. })
  99. self._sort_formats(formats)
  100. return {
  101. 'id': video_id,
  102. 'display_id': display_id,
  103. 'title': '%s - %s' % (infos.find('TITRAGE/TITRE').text,
  104. infos.find('TITRAGE/SOUS_TITRE').text),
  105. 'upload_date': unified_strdate(infos.find('PUBLICATION/DATE').text),
  106. 'thumbnail': media.find('IMAGES/GRAND').text,
  107. 'description': infos.find('DESCRIPTION').text,
  108. 'view_count': int(infos.find('NB_VUES').text),
  109. 'like_count': int(infos.find('NB_LIKES').text),
  110. 'comment_count': int(infos.find('NB_COMMENTS').text),
  111. 'formats': formats,
  112. }