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.

116 lines
4.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 ..utils import (
  6. # ExtractorError,
  7. # HEADRequest,
  8. int_or_none,
  9. qualities,
  10. unified_strdate,
  11. )
  12. class CanalplusIE(InfoExtractor):
  13. IE_DESC = 'mycanal.fr and piwiplus.fr'
  14. _VALID_URL = r'https?://(?:www\.)?(?P<site>mycanal|piwiplus)\.fr/(?:[^/]+/)*(?P<display_id>[^?/]+)(?:\.html\?.*\bvid=|/p/)(?P<id>\d+)'
  15. _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
  16. _SITE_ID_MAP = {
  17. 'mycanal': 'cplus',
  18. 'piwiplus': 'teletoon',
  19. }
  20. # Only works for direct mp4 URLs
  21. _GEO_COUNTRIES = ['FR']
  22. _TESTS = [{
  23. 'url': 'https://www.mycanal.fr/d17-emissions/lolywood/p/1397061',
  24. 'info_dict': {
  25. 'id': '1397061',
  26. 'display_id': 'lolywood',
  27. 'ext': 'mp4',
  28. 'title': 'Euro 2016 : Je préfère te prévenir - Lolywood - Episode 34',
  29. 'description': 'md5:7d97039d455cb29cdba0d652a0efaa5e',
  30. 'upload_date': '20160602',
  31. },
  32. }, {
  33. # geo restricted, bypassed
  34. 'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
  35. 'info_dict': {
  36. 'id': '1108190',
  37. 'display_id': 'pid1405-le-labyrinthe-boing-super-ranger',
  38. 'ext': 'mp4',
  39. 'title': 'BOING SUPER RANGER - Ep : Le labyrinthe',
  40. 'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
  41. 'upload_date': '20140724',
  42. },
  43. 'expected_warnings': ['HTTP Error 403: Forbidden'],
  44. }]
  45. def _real_extract(self, url):
  46. site, display_id, video_id = re.match(self._VALID_URL, url).groups()
  47. site_id = self._SITE_ID_MAP[site]
  48. info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
  49. video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
  50. if isinstance(video_data, list):
  51. video_data = [video for video in video_data if video.get('ID') == video_id][0]
  52. media = video_data['MEDIA']
  53. infos = video_data['INFOS']
  54. preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
  55. # _, fmt_url = next(iter(media['VIDEOS'].items()))
  56. # if '/geo' in fmt_url.lower():
  57. # response = self._request_webpage(
  58. # HEADRequest(fmt_url), video_id,
  59. # 'Checking if the video is georestricted')
  60. # if '/blocage' in response.geturl():
  61. # raise ExtractorError(
  62. # 'The video is not available in your country',
  63. # expected=True)
  64. formats = []
  65. for format_id, format_url in media['VIDEOS'].items():
  66. if not format_url:
  67. continue
  68. if format_id == 'HLS':
  69. formats.extend(self._extract_m3u8_formats(
  70. format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
  71. elif format_id == 'HDS':
  72. formats.extend(self._extract_f4m_formats(
  73. format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
  74. else:
  75. formats.append({
  76. # the secret extracted from ya function in http://player.canalplus.fr/common/js/canalPlayer.js
  77. 'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
  78. 'format_id': format_id,
  79. 'preference': preference(format_id),
  80. })
  81. self._sort_formats(formats)
  82. thumbnails = [{
  83. 'id': image_id,
  84. 'url': image_url,
  85. } for image_id, image_url in media.get('images', {}).items()]
  86. titrage = infos['TITRAGE']
  87. return {
  88. 'id': video_id,
  89. 'display_id': display_id,
  90. 'title': '%s - %s' % (titrage['TITRE'],
  91. titrage['SOUS_TITRE']),
  92. 'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
  93. 'thumbnails': thumbnails,
  94. 'description': infos.get('DESCRIPTION'),
  95. 'duration': int_or_none(infos.get('DURATION')),
  96. 'view_count': int_or_none(infos.get('NB_VUES')),
  97. 'like_count': int_or_none(infos.get('NB_LIKES')),
  98. 'comment_count': int_or_none(infos.get('NB_COMMENTS')),
  99. 'formats': formats,
  100. }