canalplus.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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|itele\.fr)/.*?/(?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. 'itele.fr': 'itele',
  21. }
  22. _TESTS = [{
  23. 'url': 'http://www.canalplus.fr/c-emissions/pid1830-c-zapping.html?vid=1263092',
  24. 'info_dict': {
  25. 'id': '1263092',
  26. 'ext': 'flv',
  27. 'title': 'Le Zapping - 13/05/15',
  28. 'description': 'md5:09738c0d06be4b5d06a0940edb0da73f',
  29. 'upload_date': '20150513',
  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. 'url': 'http://www.itele.fr/france/video/aubervilliers-un-lycee-en-colere-111559',
  53. 'info_dict': {
  54. 'id': '1213714',
  55. 'ext': 'flv',
  56. 'title': 'Aubervilliers : un lycée en colère - Le 11/02/2015 à 06h45',
  57. 'description': 'md5:8216206ec53426ea6321321f3b3c16db',
  58. 'upload_date': '20150211',
  59. },
  60. }]
  61. def _real_extract(self, url):
  62. mobj = re.match(self._VALID_URL, url)
  63. video_id = mobj.groupdict().get('id')
  64. site_id = self._SITE_ID_MAP[mobj.group('site') or 'canal']
  65. # Beware, some subclasses do not define an id group
  66. display_id = url_basename(mobj.group('path'))
  67. if video_id is None:
  68. webpage = self._download_webpage(url, display_id)
  69. video_id = self._search_regex(
  70. r'<canal:player[^>]+?videoId="(\d+)"', webpage, 'video id')
  71. info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
  72. doc = self._download_xml(info_url, video_id, 'Downloading video XML')
  73. video_info = [video for video in doc if video.find('ID').text == video_id][0]
  74. media = video_info.find('MEDIA')
  75. infos = video_info.find('INFOS')
  76. preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD', 'HLS', 'HDS'])
  77. fmt_url = next(iter(media.find('VIDEOS'))).text
  78. if '/geo' in fmt_url.lower():
  79. response = self._request_webpage(
  80. HEADRequest(fmt_url), video_id,
  81. 'Checking if the video is georestricted')
  82. if '/blocage' in response.geturl():
  83. raise ExtractorError(
  84. 'The video is not available in your country',
  85. expected=True)
  86. formats = []
  87. for fmt in media.find('VIDEOS'):
  88. format_url = fmt.text
  89. if not format_url:
  90. continue
  91. format_id = fmt.tag
  92. if format_id == 'HLS':
  93. hls_formats = self._extract_m3u8_formats(format_url, video_id, 'flv')
  94. for fmt in hls_formats:
  95. fmt['preference'] = preference(format_id)
  96. formats.extend(hls_formats)
  97. elif format_id == 'HDS':
  98. hds_formats = self._extract_f4m_formats(format_url + '?hdcore=2.11.3', video_id)
  99. for fmt in hds_formats:
  100. fmt['preference'] = preference(format_id)
  101. formats.extend(hds_formats)
  102. else:
  103. formats.append({
  104. 'url': format_url,
  105. 'format_id': format_id,
  106. 'preference': preference(format_id),
  107. })
  108. self._sort_formats(formats)
  109. return {
  110. 'id': video_id,
  111. 'display_id': display_id,
  112. 'title': '%s - %s' % (infos.find('TITRAGE/TITRE').text,
  113. infos.find('TITRAGE/SOUS_TITRE').text),
  114. 'upload_date': unified_strdate(infos.find('PUBLICATION/DATE').text),
  115. 'thumbnail': media.find('IMAGES/GRAND').text,
  116. 'description': infos.find('DESCRIPTION').text,
  117. 'view_count': int(infos.find('NB_VUES').text),
  118. 'like_count': int(infos.find('NB_LIKES').text),
  119. 'comment_count': int(infos.find('NB_COMMENTS').text),
  120. 'formats': formats,
  121. }